More complex queries
Say you run a fantastic movie community like The Movie Database. You have already implemented some basic features (see Basic Queries). Now you want to run sophisticated queries.
Complex sorting
You want to display a list of the three highest rated movies on the homepage.
- GraphQL request
- Response
query BestMovies {
movies(orderBy: { reviews: { rating: { AVG: DESC } } }, first: 3) {
identifier
}
}
{
"data": {
"movies": [
{
"identifier": "11-star-wars"
},
{
"identifier": "550-fight-club"
},
{
"identifier": "12-finding-nemo"
}
]
}
}
Retrieving data from external APIs
You want to display the title of the best two movies and their links on the homepage.
- GraphQL request
- Response
query BestMoviesExternalInformation {
movies(orderBy: { reviews: { rating: { AVG: DESC } } }, first: 3) {
identifier
original_title
homepage
}
}
{
"data": {
"movies": [
{
"identifier": "11-star-wars",
"original_title": "Star Wars",
"homepage": "http://www.starwars.com/films/star-wars-episode-iv-a-new-hope"
},
{
"identifier": "550-fight-club",
"original_title": "Fight Club",
"homepage": "http://www.foxmovies.com/movies/fight-club"
},
{
"identifier": "12-finding-nemo",
"original_title": "Finding Nemo",
"homepage": "http://movies.disney.com/finding-nemo"
}
]
}
}
In your domain model, a movie is considered a target. You do not wish to store its data but reference it instead. original_title
and homepage
come from an external API accessible via the Reactions GraphQL API. In this case, you are running a query on The Movie Database.
Aggregations
You're also interested in the average rating of these movies and how many reviews were written on each movie.
- GraphQL request
- Response
query BestMoviesAverageRating {
movies(orderBy: { reviews: { rating: { AVG: DESC } } }, first: 3) {
identifier
original_title
homepage
reviewsAggregations {
rating {
AVG
}
COUNT
}
}
}
{
"data": {
"movies": [
{
"identifier": "11-star-wars",
"original_title": "Star Wars",
"homepage": "http://www.starwars.com/films/star-wars-episode-iv-a-new-hope",
"reviewsAggregations": {
"rating": {
"AVG": 4.5
},
"COUNT": 4
}
},
{
"identifier": "550-fight-club",
"original_title": "Fight Club",
"homepage": "http://www.foxmovies.com/movies/fight-club",
"reviewsAggregations": {
"rating": {
"AVG": 4.416666666667
},
"COUNT": 12
}
},
{
"identifier": "12-finding-nemo",
"original_title": "Finding Nemo",
"homepage": "http://movies.disney.com/finding-nemo",
"reviewsAggregations": {
"rating": {
"AVG": 4.333333333333
},
"COUNT": 3
}
}
]
}
}
Complex filtered sorting
You're not interested in what people thought 10 years ago: instead, you want to see what movies people enjoyed in 2019. Let's see how to sort the movies by the average rating given by users in 2019.
- GraphQL request
- Response
query BestMovies2019 {
movies(
orderBy: {
reviews: {
rating: { AVG: DESC }
where: { AND: { GTE: { created: "2019-01-01T00:00:00.000Z" }, LT: { created: "2020-01-01T00:00:00.000Z" } } }
}
}
first: 3
) {
identifier
original_title
homepage
reviewsAggregations(
where: { AND: { GTE: { created: "2019-01-01T00:00:00.000Z" }, LT: { created: "2020-01-01T00:00:00.000Z" } } }
) {
rating {
AVG
}
COUNT
}
}
}
{
"data": {
"movies": [
{
"identifier": "12-finding-nemo",
"original_title": "Finding Nemo",
"homepage": "http://movies.disney.com/finding-nemo",
"reviewsAggregations": {
"rating": {
"AVG": 4
},
"COUNT": 1
}
},
{
"identifier": "1637-speed",
"original_title": "Speed",
"homepage": "",
"reviewsAggregations": {
"rating": {
"AVG": 3.5
},
"COUNT": 2
}
},
{
"identifier": "550-fight-club",
"original_title": "Fight Club",
"homepage": "http://www.foxmovies.com/movies/fight-club",
"reviewsAggregations": {
"rating": {
"AVG": 2.5
},
"COUNT": 2
}
}
]
}
}