Skip to content

Instantly share code, notes, and snippets.

@dmitry-saritasa
Created June 6, 2017 22:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitry-saritasa/b45a0857622f49179ee3dc73c2265d68 to your computer and use it in GitHub Desktop.
Save dmitry-saritasa/b45a0857622f49179ee3dc73c2265d68 to your computer and use it in GitHub Desktop.
GraphQL - Basic queries example
Test in : http://graphql.org/swapi-graphql
# ---------------------------------------------
# Basic query
# ---------------------------------------------
query all {
allFilms {
films {
episodeID
created
id
title
}
}
}
# ---------------------------------------------
# Let's add some references
# ---------------------------------------------
query all {
allFilms {
films {
episodeID
created
id
title
planetConnection {
planets {
id
diameter
name
}
}
}
}
}
# ---------------------------------------------
# Let's add some pagination
# ---------------------------------------------
query all {
allFilms (first: 3) {
totalCount
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
films {
episodeID
created
id
title
planetConnection {
planets {
id
diameter
name
}
}
}
}
}
# ---------------------------------------------
# Continuous pagination using 'after' keyword
# ---------------------------------------------
query all {
allFilms (first: 3 after:"YXJyYXljb25uZWN0aW9uOjI=") {
totalCount
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
films {
episodeID
created
id
title
planetConnection {
planets {
id
diameter
name
}
}
}
}
}
# ---------------------------------------------
# Let's union some data
# this is perfect example how to avoid
# multiple queries to different REST API
# endpoints representing different entities
# so below we combine two different data
# sets - people and films together in a single
# graphql query
# ---------------------------------------------
query all {
allPeople {
people {
id
name
homeworld {
name
}
species {
name
averageLifespan
}
}
}
allFilms (first: 3 after:"YXJyYXljb25uZWN0aW9uOjI=") {
totalCount
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
films {
episodeID
created
id
title
planetConnection (first:1) {
planets {
id
diameter
name
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment