Graphql basics
From wikinotes
NOTE:
See notes on python ariadne for a low-abstraction GraphQL implementation that exposes some of the gears in play.
Schema
# the schema, and it's 2x root objects: Query/Mutation schema { query: Query mutation: Mutation } type Query { task(id: ID!): Task! tasks: [Task!]! } type Mutation { addTask(name: String!) } # Your custom types type Task { id: ID! name: String! }GraphQL schemas are written in SDL.
Retrievable objects are exposed as fields on other objects
Two pre-set top-level objects determine the top-level queriable fields:
- Query: requests for information
- Mutation: operations with side-effects
Resolvers
The information returned by queries, and the operations performed by mutations are determined by resolves.
The implementation varies by language and library.
Requests
The GraphQL API is interacted with using HTTP requests.
No files are required to perform queries, but you can download the schema in advance, and validate the types.The return value is a json object.
# query (no params) curl -X POST \ -H 'Content-Type: application/json' \ -d '{"query": "{ tasks{ name } }"}' \ http://127.0.0.1:8000 # query (with params) curl -X POST \ -H 'Content-Type: application/json' \ -d '{"query": "query queryTask($id: ID!){ task(id: $id){ name } }", \ "variables": {"id": "1"}}' \ http://127.0.0.1:8000# mutation curl -X POST \ -H 'Content-Type: application/json' \ -d '{"query": "mutation mutationCreateTask($name: String!){ createTask(name: $desc) { id name } }" \ "variables": {"name": "clean kitchen"}}' \ http://127.0.0.1:8000