Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add array input example. close #489 #536

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions example/array_as_argument/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/relay"
"log"
"net/http"
)
pavelnikolov marked this conversation as resolved.
Show resolved Hide resolved

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
]
}
}
*/