Code Monkey home page Code Monkey logo

Comments (6)

sogko avatar sogko commented on July 21, 2024 1

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

from graphql.

EmergentBehavior avatar EmergentBehavior commented on July 21, 2024 1

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.

from graphql.

Fontinalis avatar Fontinalis commented on July 21, 2024 1

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

from graphql.

sogko avatar sogko commented on July 21, 2024

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

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

from graphql.

EmergentBehavior avatar EmergentBehavior commented on July 21, 2024

Thanks @sogko!

from graphql.

sogko avatar sogko commented on July 21, 2024

Oops, thanks for catching that @EmergentBehavior 👍🏻

from graphql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.