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:
- A realistic graphql schema
- An actual Java server that implements that graphql schema
- Using curl to issue graphql commands to that server
It is amazing difficult to find all three of these together in one place.
Process:
- clone git repository https://github.com/kobylynskyi/graphql-java-codegen-gradle-plugin
- cd graphql-java-codegen-gradle-plugin/graphql-codegen-gradle-plugin-example
- echo > settings.gradle
- gradle build
- start mongodb on port 27017 on localhost
- 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" } ] } }