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

TODO: A simple mutation example #60

Closed
sogko opened this issue Nov 6, 2015 · 6 comments
Closed

TODO: A simple mutation example #60

sogko opened this issue Nov 6, 2015 · 6 comments

Comments

@sogko
Copy link
Member

sogko commented Nov 6, 2015

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.

@sogko
Copy link
Member Author

sogko commented Nov 6, 2015

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

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

@sogko
Copy link
Member Author

sogko commented Nov 6, 2015

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"
       }
   }
 }

@EmergentBehavior
Copy link
Contributor

Thanks @sogko!

@EmergentBehavior
Copy link
Contributor

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.

@sogko
Copy link
Member Author

sogko commented Nov 7, 2015

Oops, thanks for catching that @EmergentBehavior 👍🏻

jaredonline added a commit to jaredonline/graphql-1 that referenced this issue Apr 19, 2016
Adds the simple mutation for graphql-go#60

Left to do:
 [ ] add an http based example
jaredonline added a commit to jaredonline/graphql-1 that referenced this issue Apr 19, 2016
Adding an http example for graphql-go#60

Left to do:
 - [ ] clean up the schema so its only in one place
chris-ramon pushed a commit that referenced this issue Feb 20, 2017
Adding an http example for #60

Left to do:
 - [ ] clean up the schema so its only in one place
chris-ramon pushed a commit that referenced this issue Feb 20, 2017
Adds the simple mutation for #60

Left to do:
 [ ] add an http based example
@Fontinalis
Copy link
Collaborator

Closing issue since an example was added in #189 and even got better in #190

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants