Graphql queries: Difference between revisions
From wikinotes
Line 37: | Line 37: | ||
= Fragments = | = Fragments = | ||
<blockquote> | <blockquote> | ||
Fragments are a named group of fields to select on a specific object.<br> | |||
Fragments let you: | |||
* Select type-specific fields in heterogenous collections | * Select type-specific fields in heterogenous collections | ||
* Concisely describe a group of fields, DRY out queries | * Concisely describe a group of fields, DRY out queries | ||
* Concisely describe Recursive/Nested nodes | * Concisely describe Recursive/Nested nodes | ||
{{ TODO | | |||
Example schema }} | |||
named fragments | named fragments |
Revision as of 03:13, 2 September 2021
Basics
Without Params
Some fields may be implied by the REST API path, so no params are required.
# https://tracking-system/projects/1/graphql { name createdAt }
With Params
Other times, you may need to use parameters in your query.
# https://tracking-system/projects/1/graphql { tasks($name: String){ status createdAt } }{"name": "clean bathroom"}
Fragments
Fragments are a named group of fields to select on a specific object.
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 } } } }