With PR #21 and PR #59, we now have two basic examples that would benefit new users:
Hello World (query)Several people had asked for a working example of a mutation query, using purely GraphQL, (i.e. without Relay).
Currently the only example floating around is written by @bbuck in his gist here; albeit using the old API:
https://gist.github.com/bbuck/74cb8446cdb49bf8ac22
Putting this out there to invite anyone to contribute! 馃憤馃徎
Update: For #46 , I've added a mutation example there to address the question. May not be the best example for introduce someone to mutations, though.
I've re-written @bbuck's original example here:
This is another version of the above example, but using graphql-go/handler to create a server that handles GraphQL HTTP requests. (This probably should be in graphql-go/handler)
https://gist.github.com/sogko/7debd336118e5e7c7f65
In graphql-go-handler, based on the GET/POST and the Content-Type header, it expects the input params differently.
This behaviour was ported from express-graphql.
So given the following query:
mutation M {
newTodo: createTodo(text: "This is a todo mutation example") {
text
done
}
}
$ curl -g -GET 'http://localhost:8080/graphql?query=mutation+M{newTodo:createTodo(text:"This+is+a+todo+mutation+example"){text+done}}'
$ curl -XPOST http://localhost:8080/graphql -H 'Content-Type: application/graphql' -d 'mutation M { newTodo: createTodo(text: "This is a todo mutation example") { text done } }'
$ curl -XPOST http://localhost:8080/graphql -H 'Content-Type: application/json' -d '{"query": "mutation M { newTodo: createTodo(text: \"This is a todo mutation example\") { text done } }"}'
Any of the above would return the same output:
{
"data": {
"newTodo": {
"done": false,
"text": "This is a todo mutation example"
}
}
}
Thanks @sogko!
Also helpful to note, if there are "optional" params that you don't send in the query you'll have an issue trying to convert nil to string. I found this to be a suitable workaround:
optionalVal, _ := params.Args["optionalVal"].(string)
Prevents panicking. If I'm correct, it will default optionVal to the nil for string.
Oops, thanks for catching that @EmergentBehavior 馃憤馃徎
Closing issue since an example was added in #189 and even got better in #190