Graphql: TODO: A simple mutation example

Created on 6 Nov 2015  路  6Comments  路  Source: graphql-go/graphql

With PR #21 and PR #59, we now have two basic examples that would benefit new users:

  • Basic Hello World (query)
  • Basic http example

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.

enhancement help wanted

All 6 comments

I've re-written @bbuck's original example here:

https://gist.github.com/sogko/e298f9d1401e0ad736c6

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

How to make GraphQL HTTP request using cUrl

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
  }
}
using GET
$ curl -g -GET 'http://localhost:8080/graphql?query=mutation+M{newTodo:createTodo(text:"This+is+a+todo+mutation+example"){text+done}}'
using POST + Content-Type: application/graphql
$ 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 } }'
using POST + Content-Type: application/json
$ 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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

titanous picture titanous  路  3Comments

yhc44 picture yhc44  路  5Comments

yookoala picture yookoala  路  4Comments

zbintliff picture zbintliff  路  3Comments

schaze picture schaze  路  4Comments