File tree Expand file tree Collapse file tree 1 file changed +79
-0
lines changed
example/array_as_argument Expand file tree Collapse file tree 1 file changed +79
-0
lines changed Original file line number Diff line number Diff line change
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
+ */
You can’t perform that action at this time.
0 commit comments