Skip to content

Instantly share code, notes, and snippets.

@eveporcello
Last active August 9, 2024 21:02
Show Gist options
  • Save eveporcello/e8e910e6ee605aff0a3e7d00a33e31ea to your computer and use it in GitHub Desktop.
Save eveporcello/e8e910e6ee605aff0a3e7d00a33e31ea to your computer and use it in GitHub Desktop.
GraphQL Query Language Cheatsheet

GraphQL Query Language Cheatsheet

Basic Queries: Open Pet Library API to test or use the link in Postman.

Single Field Query

Return a single field.

query {
  totalPets
}

List of Objects

Return a list of objects + the single field from the previous query.

query {
  allPets {
    name
    weight
  }
  totalPets
}

Enums

Return from a restricted list of options: category.

query {
  allPets {
    name
    weight
    category
  }
  totalPets
}

Nested Queries

Return a nested object: photo.

query {
  allPets {
    name
    category
    weight
    photo {
      full
      thumb
    }
  }
}

Arguments

Pass in a filter or argument for status.

query {
  totalPets(status: AVAILABLE)
  allPets {
    name
    category
    weight
    photo {
      full
      thumb
    }
  }
}

Aliases

Send the same field with different arguments (totalPets) or just rename a field dogs.

query PetFields {
  availablePets: totalPets(status: AVAILABLE)
  checkedOutPets: totalPets(status: CHECKEDOUT)
  dogs: allPets(category: DOG, status: AVAILABLE) {
    name
    weight
    status
    category
    photo {
      full
      thumb
    }
  }
}

Variables

Pass in dynamic variables as arguments or filters.

query AvailableDogs($category: PetCategory, $status: PetStatus) {
  allPets(category: $category, status: $status) {
    id
    name
    status
    category
  }
}

Query Variables Passed as JSON

{
  "category": "DOG",
  "status": "AVAILABLE"
}

Mutations

Change data with a mutation.

mutation CreateAccount ($input: CreateAccountInput!) {
  createAccount(input: $input) {
    name
    username
  }
}

Variables

{
  "input": {
    "name": "John Smith",
    "password": "12345",
    "username": "jsmith24"
  }
}

Advanced Queries -- Open https://funded-pet-library.moonhighway.com to test or use the link in Postman.

Fragments

Create a wrapper around a selection set and pass to the query.

query MyPets {
  allPets {
    ...PetFields
  }
  petById(id: "C-1") {
    ...PetFields
  }
}

fragment PetFields on Pet {
  id
  name
  weight
}

Union Types

Return a list that contains multiple types - a union. A FamilyPet is either a Cat or a Dog. An ExoticPet is either a Rabbit or a Stingray.

query SpecificPets {
  familyPets {
    ... on Cat {
      name
      curious
    }
    ... on Dog {
      name
      good
    }
  }
  exoticPets {
    ... on Stingray {
      name
      chill
    }
    ... on Rabbit {
      name
      floppy
    }
  }
}

Interfaces

The Pet is now an interface. A Pet has a certain list of fields and then we extend the interface to add custom fields for different types: Cat, Dog, etc,

query AvailablePets {
  totalPets
  availablePets
  allAvailablePets {
    __typename
    name
    weight
    photo {
      thumb
    }
    ...on Cat {
      curious
    }
    ...on Dog {
      good
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment