Basic Queries: Open Pet Library API to test or use the link in Postman.
Return a single field.
query {
totalPets
}
Return a list of objects + the single field from the previous query.
query {
allPets {
name
weight
}
totalPets
}
Return from a restricted list of options: category
.
query {
allPets {
name
weight
category
}
totalPets
}
Return a nested object: photo
.
query {
allPets {
name
category
weight
photo {
full
thumb
}
}
}
Pass in a filter or argument for status
.
query {
totalPets(status: AVAILABLE)
allPets {
name
category
weight
photo {
full
thumb
}
}
}
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
}
}
}
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"
}
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.
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
}
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
}
}
}
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
}
}
}