-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgql.go
86 lines (72 loc) · 1.92 KB
/
gql.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
package main
import (
"fmt"
"net/http"
"reflect"
"github.com/graphql-go/graphql"
"github.com/graphql-go/handler"
)
func getGraphqlType(u interface{}) *graphql.Object {
reflectedType := reflect.TypeOf(u)
return graphql.NewObject(
graphql.ObjectConfig{
Name: reflectedType.Name(),
Fields: getGraphqlFields(u),
},
)
}
func getGraphqlFields(t interface{}) graphql.Fields {
var fieldMap = make(map[string]*graphql.Field)
reflectedType := reflect.TypeOf(t)
reflectedValue := reflect.ValueOf(t)
for i := 0; i < reflectedType.NumField(); i++ {
valueField := reflectedValue.Field(i)
typeField := reflectedType.Field(i)
switch valueField.Kind() {
case reflect.String:
fieldMap[typeField.Name] = &graphql.Field{
Type: graphql.String,
Description: typeField.Tag.Get("doc"),
}
case reflect.Int:
fieldMap[typeField.Name] = &graphql.Field{
Type: graphql.Int,
Description: typeField.Tag.Get("doc"),
}
case reflect.Struct:
fieldMap[typeField.Name] = &graphql.Field{
Type: getGraphqlType(valueField.Interface()),
Description: typeField.Tag.Get("doc"),
}
}
}
return fieldMap
}
type ContactType struct {
ID int `json:"id" doc:"The id of the Contact"`
Email string `json:"email" doc:"The email of the Contact"`
}
type UserType struct {
ID int `json:"id" doc:"The id of the User"`
Name string `json:"name" doc:"The Name of the User"`
Contact ContactType `json:"contact" doc:"The Contact associated with the User"`
}
type QueryType struct {
User UserType `json:"user" doc:"Query to fetch all users"`
}
var q = QueryType{}
var schema, _ = graphql.NewSchema(
graphql.SchemaConfig{
Query: getGraphqlType(q),
},
)
func main() {
h := handler.New(&handler.Config{
Schema: &schema,
Pretty: true,
GraphiQL: true,
})
fmt.Println("Starting server at port 8080")
http.Handle("/graphql", h)
http.ListenAndServe(":8080", nil)
}