Graphql queries: Difference between revisions

From wikinotes
No edit summary
Line 6: Line 6:
optionally passing through an arbitrary number of child node-fields.
optionally passing through an arbitrary number of child node-fields.


<syntaxhighlight lang="graphql">
<syntaxhighlight lang="python">


</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="graphql">
<syntaxhighlight lang="python">
# select id/name fields on Project
# select id/name fields on Project
{
{
Line 24: Line 24:
<blockquote>
<blockquote>
Some fields may be implied by the REST API path, so no params are required.
Some fields may be implied by the REST API path, so no params are required.
<syntaxhighlight lang="graphql">
<syntaxhighlight lang="python">
# https://tracking-system/projects/1/graphql
# https://tracking-system/projects/1/graphql
{
{
Line 37: Line 37:
<blockquote>
<blockquote>
Other times, you may need to use parameters in your query.
Other times, you may need to use parameters in your query.
<syntaxhighlight lang="graphql">
<syntaxhighlight lang="python">
# https://tracking-system/projects/1/graphql
# https://tracking-system/projects/1/graphql
{
{
Line 65: Line 65:


named fragments
named fragments
<syntaxhighlight lang="graphql">
<syntaxhighlight lang="python">
{
{
   children {
   children {
Line 92: Line 92:


inline fragments
inline fragments
<syntaxhighlight lang="graphql">
<syntaxhighlight lang="python">
{
{
   rentalInventory {
   rentalInventory {

Revision as of 03:32, 2 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.

# select id/name fields on Project
{
  Project {  # <-- a top-level node
    id       # <-- fields on that node
    name
  }
}

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-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
      }
    }
  }
}