GraphQL

 

Queries:

query {
  allLifts {
    name
    capacity
  }
  liftCount
}

Inline filtering w an enum

query {
  liftCount(status: OPEN)
}

Two of the same query require aliasing to namespace them

query {
  openLifts: liftCount(status: OPEN)
  closedLifts: liftCount(status: CLOSED)
}

Aliasing can simply be a way to rename a field (liftName)

query {
  allLifts {
    liftName: name
    capacity
  }
}

Pass variables into filters. They must be typed and prefixed with $.

query ($status: LiftStatus) {
  liftCount: liftCount(status: $status)
}

Note: you must add the variable value in the QUERY VARIABLES section below the query input box in sandbox – in a production use case it would be passed in the HTTP Header.


Associating data

query () {
  allLifts {
    liftName: name
    capacity
    status
    night
    trailAccess{
      name
      status
      accessedByLifts {
        name
      }
    }
  }
}

In this example the lift has a trailAccess. The trailAccess has accessedByLifts. A trail could be accessed by more than one lift, but accessedByLifts will definitely contain the lift identified as liftName in the result, as in the following result fragment:

          {
            "name": "Blackhawk",
            "status": "OPEN",
            "accessedByLifts": [
              {
                "name": "Astra Express"
              },
              {
                "name": "Panorama"
              }
            ]
          },

Operation Names – you need names in order to add multiple queries to a request. Use PascalCase. You can still only send one at a time.

query AllLifts {
  allLifts {
    liftName: name
    capacity
    status
    night
  }
}

query AllTrails {
  AllTrails {
    name
    status
  }
}

Mutations

mutation {
setTrailStatus(id: "goosebumps" status:CLOSED) {
  name
  status
}}

Fragments (composition via fragments) LiftDetails included via spread operator.

query {
  allLifts {
    ...LiftDetails
  }
  Lift(id: "panorama") {
    ...LiftDetails
  }
}

fragment LiftDetails on Lift {
	id
  name
  status
  capacity
  trailAccess {
    name
  }
  elevationGain
  night
}

Subscriptions. Waits for a change to status and sends the name and new status of the trail that changed right when it happens.

subscription {
  trailStatusChange {
    name
    status
  }
}

Scalars

Int, Float, String, Boolean, ID. ! == required/not nullable.

 





Recently Updated

GraphQL Logo

You may also like...

Leave a Reply

Your email address will not be published.


one − = 0

This site uses Akismet to reduce spam. Learn how your comment data is processed.