Skip to content

Instantly share code, notes, and snippets.

@eveporcello
Last active January 28, 2019 12:24
Show Gist options
  • Save eveporcello/e3106a14dca245e20d746c5e3d9bd201 to your computer and use it in GitHub Desktop.
Save eveporcello/e3106a14dca245e20d746c5e3d9bd201 to your computer and use it in GitHub Desktop.

Writing Queries

Snowtooth Playground

Practice with these Queries!

Queries

query {
  allLifts {
    name
    status
    capacity
    night
    elevationGain
  }
}

Operation Names, Aliases

query liftsAndTrails {
  lifts: allLifts {
    name
    status
    capacity
    night
    elevationGain
  }
  trails: allTrails {
    trailName:name
    trailDifficulty:difficulty
  }
}

Connections

query liftsAndTrails {
  lifts: allLifts {
    name
    status    
    capacity
    night
    elevationGain
    trailAccess {
      name
      status
    }
  }
  trails: allTrails {
    trailName:name
    trailDifficulty:difficulty
    accessedByLifts {
      name
      night
    }
  }
}

Advanced Queries

Query Arguments

query {
  Trail(id:"grandma") {
    name
    status
    trees
    night
  }
  Lift(id:"panorama") {
    name
    capacity
  }
  liftCount(status:"HOLD") 
  trailCount(status:"OPEN")
}

Query Variables

query openLiftsTrails($status: String!) {
  liftCount(status: $status) 
  trailCount(status: $status)
}
{
  "status": "OPEN"
}

Fragments

query {
  allLifts {
    ...LiftDetails
    elevationGain
    trailAccess {
      ...TrailDetails
    }
  }
  allTrails { 
   ...TrailDetails 
   night
   accessedByLifts {
     ...LiftDetails
   }
  }
}

fragment LiftDetails on Lift {
  name
  status
  capacity
}

fragment TrailDetails on Trail {
  name
  difficulty
  groomed
}

Mutations and Subscriptions

Mutations

  • Run the Mutation
mutation PanoramaUpdate {
  setLiftStatus(id: "panorama" status: "CLOSED") {
    name
    status
  }
}
  • Test the Query
query {
	Lift(id: "panorama") {
    name
    status
  }
}
  • Replace with an input type
mutation PanoramaUpdate($id:ID! $status: String!) {
  setLiftStatus(id: $id status: $status) {
    name
    status
  }
}
  • input JSON
{
  "id": "panorama",
  "status": "HOLD"
}

Student Lab Challenge

Using SWAPI: http://graphql.org/swapi-graphql/

  • List all of the films with id, title, and director
query {
  allFilms {
    films {
      id
      title
      director
    }
  }
}
  • Extra Credit: Send a query to find a person with an personID of 5. Find their name, year they were born, and height.
{
  person(personID:5) {
    name
    birthYear
    height
  }
}
  • Extra, extra credit: Once you know which person has an ID of 5 from the query, name the query with the person's name:
{
  princessLeia:person(personID:5) {
    name
    birthYear
    height
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment