-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.go
132 lines (120 loc) · 2.8 KB
/
graphql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"fmt"
"net/http"
"time"
"github.com/graphql-go/graphql"
)
// Response of Google Maps request
type Response struct {
Results []struct {
AddressComponents []struct {
LongName string `json:"long_name"`
ShortName string `json:"short_name"`
Types []string `json:"types"`
} `json:"address_components"`
FormattedAddress string `json:"formatted_address"`
Geometry struct {
Bounds struct {
Northeast struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"northeast"`
Southwest struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"southwest"`
} `json:"bounds"`
Location struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"location"`
LocationType string `json:"location_type"`
Viewport struct {
Northeast struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"northeast"`
Southwest struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"southwest"`
} `json:"viewport"`
} `json:"geometry"`
PlaceID string `json:"place_id"`
Types []string `json:"types"`
} `json:"results"`
Status string `json:"status"`
}
// GeoPoint : this is a geographic point
type GeoPoint struct {
latitude float64
longitude float64
}
var geoPointType = graphql.NewObject(
graphql.ObjectConfig{
Name: "GeoPoint",
Fields: graphql.Fields{
"latitude": &graphql.Field{
Type: graphql.Float,
},
"longitude": &graphql.Field{
Type: graphql.String,
},
},
},
)
// Country : this is a country
type Country struct {
Name string `json:"name"`
}
var countryType = graphql.NewObject(
graphql.ObjectConfig{
Name: "Country",
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
},
},
},
)
var myClient = &http.Client{Timeout: 10 * time.Second}
var queryType = graphql.NewObject(
graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"point": &graphql.Field{
Type: countryType,
Args: graphql.FieldConfigArgument{
"latitude": &graphql.ArgumentConfig{
Type: graphql.Float,
}, "longitude": &graphql.ArgumentConfig{
Type: graphql.Float,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
point := GeoPoint{
latitude: p.Args["latitude"].(float64),
longitude: p.Args["longitude"].(float64),
}
return getCountry(point)
},
},
},
},
)
var schema, _ = graphql.NewSchema(
graphql.SchemaConfig{
Query: queryType,
},
)
func executeQuery(query string, schema graphql.Schema) *graphql.Result {
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
})
if len(result.Errors) > 0 {
fmt.Printf("wrong result, unexpected errors: %v", result.Errors)
}
return result
}