forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
141 lines (132 loc) · 2.7 KB
/
main.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
133
134
135
136
137
138
139
140
141
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
)
type CustomID struct {
value string
}
func (id *CustomID) String() string {
return id.value
}
func NewCustomID(v string) *CustomID {
return &CustomID{value: v}
}
var CustomScalarType = graphql.NewScalar(graphql.ScalarConfig{
Name: "CustomScalarType",
Description: "The `CustomScalarType` scalar type represents an ID Object.",
// Serialize serializes `CustomID` to string.
Serialize: func(value interface{}) interface{} {
switch value := value.(type) {
case CustomID:
return value.String()
case *CustomID:
v := *value
return v.String()
default:
return nil
}
},
// ParseValue parses GraphQL variables from `string` to `CustomID`.
ParseValue: func(value interface{}) interface{} {
switch value := value.(type) {
case string:
return NewCustomID(value)
case *string:
return NewCustomID(*value)
default:
return nil
}
},
// ParseLiteral parses GraphQL AST value to `CustomID`.
ParseLiteral: func(valueAST ast.Value) interface{} {
switch valueAST := valueAST.(type) {
case *ast.StringValue:
return NewCustomID(valueAST.Value)
default:
return nil
}
},
})
type Customer struct {
ID *CustomID `json:"id"`
}
var CustomerType = graphql.NewObject(graphql.ObjectConfig{
Name: "Customer",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: CustomScalarType,
},
},
})
func main() {
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"customers": &graphql.Field{
Type: graphql.NewList(CustomerType),
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: CustomScalarType,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
// id := p.Args["id"]
// log.Printf("id from arguments: %+v", id)
customers := []Customer{
Customer{ID: NewCustomID("fb278f2a4a13f")},
}
return customers, nil
},
},
},
}),
})
if err != nil {
log.Fatal(err)
}
query := `
query {
customers {
id
}
}
`
/*
queryWithVariable := `
query($id: CustomScalarType) {
customers(id: $id) {
id
}
}
`
*/
/*
queryWithArgument := `
query {
customers(id: "5b42ba57289") {
id
}
}
`
*/
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
VariableValues: map[string]interface{}{
"id": "5b42ba57289",
},
})
if len(result.Errors) > 0 {
log.Fatal(result)
}
b, err := json.Marshal(result)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}