Graphql queries: Difference between revisions
Line 50: | Line 50: | ||
Other times, you may need to use parameters in your query.<br> | Other times, you may need to use parameters in your query.<br> | ||
The value passed to each parameter is provided in a JSON object. | The value passed to each parameter is provided in a JSON object. | ||
{{ TODO | | |||
do you need a named query to pass in params? }} | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
# https://tracking-system/projects/1/graphql | # https://tracking-system/projects/1/graphql |
Revision as of 03:43, 3 September 2021
Basics
Graphql objects are arranged in a DAG.
Top-level nodes can be queried directly,
other objects are accessed through fields on top-level items, optionally passing through an arbitrary number of child node-fields.Queries are issued as POST requests with a json object as the payload.
curl -X POST \ -H 'Content-Type: application/json' \ -d '{"query": "query { members($search: String){ firstName lastName } }", \ "variables" { "search": "al*" }}' \ example.com/graphqlMost of the time you'll just work with the SDL part.
query { members($search: String){ firstName lastName } }And the JSON variables
{ "search": "al*" }
Without Params
Some fields may be implied by the REST API path, so no params are required.
This URL probably refers to project 1.# https://tracking-system/projects/1/graphql { name createdAt }
With Params
Other times, you may need to use parameters in your query.
The value passed to each parameter is provided in a JSON object.TODO:
do you need a named query to pass in params?
# https://tracking-system/projects/1/graphql { tasks($name: String){ status createdAt } }// json object of params {"name": "clean bathroom"}
Fragments
Fragments are a named group of fields to select on a specific object-type.
Fragments let you:
- Select type-specific fields in heterogenous collections
- Concisely describe a group of fields, DRY out queries
- Concisely describe Recursive/Nested nodes
TODO:
Example schema
named fragments
{ children { ...nodeFields children { ...nodeFields { children { ...nodeFields } } } } } fragment nodeFields on Node { id text colour background-colour }inline fragments
{ rentalInventory { vehicles { wheels # select on both Car and Motorcycle passengers ... on Car { # select on Car items only numSeatbelts numAirbags } ... on Motorcycle { # select on Motorcycle items only numSaddleBags } } } }
Connections/Edges
Directives
Directives are conditionals. You can select fields if a boolean expression evaluates to true. https://graphql.org/learn/queries/#directives
@include(if: Boolean) @skip(if: Boolean)
Introspection
List Types
{ __schema { types { name } } } # list all types { __schema { mutationType { fields { name } } } } # list all mutations { __schema { queryType { fields { name } } } } # list all queriesQuery Available Fields
{ __type(name: "Droid") { # GraphQL type we want fields from name fields { name type { name kind } } } }Returns all fields, and their type info.
Query Type info
{ __type(name: "Shop") { fields { name description } } }