Skip to content

Commit

Permalink
add array input example. close graph-gophers#489 (graph-gophers#536)
Browse files Browse the repository at this point in the history
add an array input example
  • Loading branch information
MangioneAndrea authored and KNiepok committed Feb 28, 2023
1 parent 46e41ce commit 92d22a8
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions example/array_as_argument/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"log"
"net/http"

"github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/relay"
)

type query struct{}

type IntInput 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 []IntInput }) []int32 {
result := make([]int32, len(args.Values))

for i, value := range args.Values {
result[i] = value.A + value.B
}
return result
}

func main() {
s := `
input IntInput {
a: Int!
b: Int!
}
type Query {
reversed(values: [String!]!): [String!]!
sums(values: [IntInput!]!): [Int!]!
}
`
schema := graphql.MustParseSchema(s, &query{})
http.Handle("/query", &relay.Handler{Schema: schema})

log.Println("Listen in port :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}

/*
* The following query
query{
reversed(values:["hello", "hi"])
sums(values:[{a:2,b:3},{a:-10,b:-1}])
}
* will return
{
"data": {
"reversed": [
"olleh",
"ih"
],
"sums": [
5,
-11
]
}
}
*/

0 comments on commit 92d22a8

Please sign in to comment.