GraphQL with curl examples

GraphQL curl command examples, showing you both the curl command and the graphql schema.

Project link: graphql-java-codegen-gradle-plugin

This is documentation for the combination of:

  1. A realistic graphql schema
  2. An actual Java server that implements that graphql schema
  3. Using curl to issue graphql commands to that server

It is amazing difficult to find all three of these together in one place.

Process:

  1. clone git repository https://github.com/kobylynskyi/graphql-java-codegen-gradle-plugin
  2. cd graphql-java-codegen-gradle-plugin/graphql-codegen-gradle-plugin-example
  3. echo > settings.gradle
  4. gradle build
  5. start mongodb on port 27017 on localhost
  6. gradle run
    — this starts a server on port localhost:8080

Then, in a different window, run these sample curl commands:

ADD:
Notes:
* Shows syntax for types: String, Int, BigDecimal, and enum
* Only outputs ‘size’ because that is what the query asked to get
* Shows that “mutation” is still a “query”
* Shows the correct quotation mark escaping, and where quotes are required and where they are not required

curl  \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{ "query":  "mutation {  newBike(bike : { type: ROAD, brand: \"foo\", size: \"big\", year: 2000, price: 123 }) { size } }" }' \
  http://localhost:8080/graphql

Result:

{
  "data": {
    "newBike": {
      "size":"big"
    }
  }
}

FETCH:
Notes:
* shows formatting of “DateTime” field

curl  \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{ "query":  "query {  bikes { id type brand size year price addedDateTime} }"  }' \
  http://localhost:8080/graphql

Result:

{
  "data": {
    "bikes": [
      {
        "id": "5e28ac4d64f0e8088f8bce47",
        "type": "ROAD",
        "brand": "foo",
        "size": "big",
        "year": 2000,
        "price": 123,
        "addedDateTime": "2020-01-22 14:10:53 -0600"
      }
    ]
  }
}
This entry was posted in Software Engineering. Bookmark the permalink.

Comments are closed.