Skip to content

Commit 0135d51

Browse files
add array input example. close #489 (#536)
add an array input example
1 parent 6d71ad7 commit 0135d51

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

example/array_as_argument/server.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"github.com/graph-gophers/graphql-go"
8+
"github.com/graph-gophers/graphql-go/relay"
9+
)
10+
11+
type query struct{}
12+
13+
type IntInput struct {
14+
A int32
15+
B int32
16+
}
17+
18+
func (_ *query) Reversed(args struct{ Values []string }) []string {
19+
result := make([]string, len(args.Values))
20+
21+
for i, value := range args.Values {
22+
for _, v := range value {
23+
result[i] = string(v) + result[i]
24+
}
25+
}
26+
return result
27+
}
28+
29+
func (_ *query) Sums(args struct{ Values []IntInput }) []int32 {
30+
result := make([]int32, len(args.Values))
31+
32+
for i, value := range args.Values {
33+
result[i] = value.A + value.B
34+
}
35+
return result
36+
}
37+
38+
func main() {
39+
s := `
40+
input IntInput {
41+
a: Int!
42+
b: Int!
43+
}
44+
45+
type Query {
46+
reversed(values: [String!]!): [String!]!
47+
sums(values: [IntInput!]!): [Int!]!
48+
}
49+
`
50+
schema := graphql.MustParseSchema(s, &query{})
51+
http.Handle("/query", &relay.Handler{Schema: schema})
52+
53+
log.Println("Listen in port :8080")
54+
log.Fatal(http.ListenAndServe(":8080", nil))
55+
}
56+
57+
/*
58+
* The following query
59+
60+
query{
61+
reversed(values:["hello", "hi"])
62+
sums(values:[{a:2,b:3},{a:-10,b:-1}])
63+
}
64+
65+
* will return
66+
67+
{
68+
"data": {
69+
"reversed": [
70+
"olleh",
71+
"ih"
72+
],
73+
"sums": [
74+
5,
75+
-11
76+
]
77+
}
78+
}
79+
*/

0 commit comments

Comments
 (0)