Graphql queries: Difference between revisions

From wikinotes
Line 1: Line 1:
= Basics =
= Basics =
<blockquote>
<blockquote>
Some fields may be implied by the REST API path, so no params are required.
<syntaxhighlight lang="graphql">
# https://tracking-system/projects/1/graphql
{
  name
  createdAt
}
</syntaxhighlight>


Other times, you may need to use parameters in your query.
<syntaxhighlight lang="graphql">
{
  tasks($name: String){
    status
    createdAt
  }
}
</syntaxhighlight>
<syntaxhighlight lang="json">
{"name": "clean bathroom"}
</syntaxhighlight>
</blockquote><!-- Basics -->
</blockquote><!-- Basics -->



Revision as of 00:35, 2 September 2021

Basics

Some fields may be implied by the REST API path, so no params are required.

# https://tracking-system/projects/1/graphql
{
  name
  createdAt
}

Other times, you may need to use parameters in your query.

{
  tasks($name: String){
    status
    createdAt
  }
}
{"name": "clean bathroom"}

Without Params

With Params

Fragments

TODO:

Example schema

When a collection can contain multiple types,
fragments let you select different fields for different types.

{
  rentalInventory {
    vehicles {
      wheels
      passengers

      ... on Car {
        numSeatbelts
        numAirbags
      }

      ... on Motorcycle {
        numSaddleBags
      }
    }
  }
}
  • wheels, passengers are queried on all items
  • numSeatbelts,numAirbags is queried on Car items
  • numSaddleBags is queried on Motorcycle items