Graphql queries: Difference between revisions
(→Basics) |
|||
(5 intermediate revisions by the same user not shown) | |||
Line 17: | Line 17: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Most of the time you'll just work with the | Most of the time you'll just work with the query language. | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
query { | query { | ||
Line 27: | Line 27: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
And | And optionally JSON variables | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
{ "search": "al*" } | { "search": "al*" } | ||
Line 51: | Line 51: | ||
<blockquote> | <blockquote> | ||
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 | The value passed to each parameter can be hard-coded, or provided in a JSON object. | ||
In Schema | In Schema | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
type Query { | |||
{ | task(name: String!): Task! | ||
} | |||
type Task { | |||
name: String! | |||
status: String! | |||
id: ID! | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 72: | Line 74: | ||
-d '{"query": "\ | -d '{"query": "\ | ||
{ \ | { \ | ||
task(name: "foo") { \ | |||
status \ | status \ | ||
} \ | } \ | ||
}"' | }"' \ | ||
https://domain.com/graphql | |||
# param values can also be assigned in a JSON object | # param values can also be assigned in a JSON object | ||
# (query's 'getTasks' name here is totally arbitrary, name as you please) | |||
curl -X POST \ | curl -X POST \ | ||
-H 'Content-Type: application/json' \ | -H 'Content-Type: application/json' \ | ||
Line 83: | Line 87: | ||
"query": "\ | "query": "\ | ||
query getTasks($name: String){ \ | query getTasks($name: String){ \ | ||
task(name: $name) { \ | |||
status \ | status \ | ||
} \ | } \ | ||
}", | }", | ||
"variables": {"name": "foo"} \ | "variables": {"name": "foo"} \ | ||
}' | }' \ | ||
https://domain.com/graphql | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- With Params --> | </blockquote><!-- With Params --> |
Latest revision as of 17:23, 4 September 2021
Queries retrieve information without causing side-effects.
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 queryMembers($search: String){ members(search: $search){ firstName lastName } }", \ "variables" { "search": "al*" }}' \ example.com/graphqlMost of the time you'll just work with the query language.
query { members($search: String){ firstName lastName } }And optionally 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 can be hard-coded, or provided in a JSON object.
In Schematype Query { task(name: String!): Task! } type Task { name: String! status: String! id: ID! }In Request
# hard-coded param curl -X POST \ -H 'Content-Type: application/json' \ -d '{"query": "\ { \ task(name: "foo") { \ status \ } \ }"' \ https://domain.com/graphql # param values can also be assigned in a JSON object # (query's 'getTasks' name here is totally arbitrary, name as you please) curl -X POST \ -H 'Content-Type: application/json' \ -d '{ \ "query": "\ query getTasks($name: String){ \ task(name: $name) { \ status \ } \ }", "variables": {"name": "foo"} \ }' \ https://domain.com/graphql
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 } } }