Graphql queries: Difference between revisions

From wikinotes
No edit summary
Line 47: Line 47:
   rentalInventory {
   rentalInventory {
     vehicles {
     vehicles {
       wheels
       wheels               # on both Car and Motorcycle
       passengers
       passengers


       ... on Car {
       ... on Car {         # on Car only
         numSeatbelts
         numSeatbelts
         numAirbags
         numAirbags
       }
       }


       ... on Motorcycle {
       ... on Motorcycle { # on Motorcycle only
         numSaddleBags
         numSaddleBags
       }
       }

Revision as of 02:24, 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

TODO:

Example schema

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

{
  rentalInventory {
    vehicles {
      wheels               # on both Car and Motorcycle
      passengers

      ... on Car {         # on Car only
        numSeatbelts
        numAirbags
      }

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