-
Notifications
You must be signed in to change notification settings - Fork 493
/
example_input_array_test.go
83 lines (69 loc) · 1.43 KB
/
example_input_array_test.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
package graphql_test
import (
"context"
"encoding/json"
"os"
"github.com/graph-gophers/graphql-go"
)
type query struct{}
type IntTuple struct {
A int32
B int32
}
func (*query) Reversed(args struct{ Values []string }) []string {
result := make([]string, len(args.Values))
for i, value := range args.Values {
for _, v := range value {
result[i] = string(v) + result[i]
}
}
return result
}
func (*query) Sums(args struct{ Values []IntTuple }) []int32 {
result := make([]int32, len(args.Values))
for i, value := range args.Values {
result[i] = value.A + value.B
}
return result
}
// Example_inputArray shows a simple GraphQL schema which defines a custom input type.
// Then it executes a query against it passing array arguments.
func Example_inputArray() {
s := `
input IntTuple {
a: Int!
b: Int!
}
type Query {
reversed(values: [String!]!): [String!]!
sums(values: [IntTuple!]!): [Int!]!
}
`
schema := graphql.MustParseSchema(s, &query{})
query := `
query{
reversed(values:["hello", "hi"])
sums(values:[{a:2,b:3},{a:-10,b:-1}])
}
`
res := schema.Exec(context.Background(), query, "", nil)
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
err := enc.Encode(res)
if err != nil {
panic(err)
}
// output:
// {
// "data": {
// "reversed": [
// "olleh",
// "ih"
// ],
// "sums": [
// 5,
// -11
// ]
// }
// }
}