Lab - Query Practice

Plan: Developer Foundations

Lesson 19 of 28 · 30 min

In this lab, you will:

  • Query for products within a specific price range
  • Query for products with a specific search term and sort them
  • Query for banner content

Query for Products Within a Specific Price Range

In this query, we will use a filter to search for the first three products within a price range of 50and50 and 150.

  1. In the GraphQL playground, begin writing the query with the root, site, search field, and searchProducts field.
query{
site{
search{
searchProducts{
}
}
}
}
  1. Next, let’s add a filter to define the information we want. Add the filters field and specify that we’re looking for the price with a minPrice of 50 and maxPrice of 150.
query{
site{
search{
searchProducts(
filters:{
price:{
minPrice:50
maxPrice:150
}
}
)
}
}
}
  1. Next, let’s add the product information that we want. Add the products field and specify that we want the first 3 products plus their entityId, name, and price.
query{
site{
search{
searchProducts(
filters:{
price:{
minPrice:50
maxPrice:150
}
}
){
products(first:3){
edges{
node{
entityId
name
prices{
price{
value
}
}
}
}
}
}
}
}
}
  1. Click the Play button in the middle of the GraphQL Playground to run the query.
  2. Observe the response.

Query for Products with a Specific Search Term and Sort Them

In this query, we will ask for the first five products with the word “Shirt” as a search term and then sort them alphabetically.

  1. In the GraphQL playground, begin writing the query with the root, site, search field, and searchProducts field.
query{
site{
search{
searchProducts{
}
}
}
}
  1. Next, let’s add a filter to define the information we want. Add the filters field and specify that we’re looking for the searchTerm: "shirt".
query{
site{
search{
searchProducts(
filters:{
searchTerm:"shirt"
}
)
}
}
}
  1. Next, add another filter to sort the information we want. Add the sort field and specify that we want to sort the products from A_TO_Z.
query{
site{
search{
searchProducts(
filters:{
searchTerm:"shirt"
}
sort:A_TO_Z
)
}
}
}
  1. Lastly, we need to define what product information we want from the products. Add the products field_ and specify that we want the first 5, entityId, name, and price.
query{
site{
search{
searchProducts(
filters:{
searchTerm:"Shirt"
}
sort:A_TO_Z
){
products(first:5){
edges{
node{
entityId
name
prices{
price{
value
}
}
}
}
}
}
}
}
}
  1. Click the Play button in the middle of the GraphQL Playground to run the query.
  2. Observe the response.

Query for Banner Content

In this query, we will ask to see the entityId, name, content, and location of a banner on the Home Page.

  1. In the GraphQL playground, begin writing the query with the root, site, and content fields.
query{
site{
content{
}
}
}
  1. Next, let’s specify the information we want. Add the banners field and specify that we’re looking for the entityId, name, content, and location.
query{
site{
content{
banners{
homePage{
edges{
node{
entityId
name
content
location
}
}
}
}
}
}
}
  1. Click the Play button in the middle of the GraphQL Playground to run the query.
  2. Observe the response.