Lab - Building Queries
In this lab, you will:
- Query for customer-specific information
- Use an Alias to rename a field in a query
- Use a fragment to query for pricing information
- Use cursors to find page 2 of a list of products
Follow these steps:
- In the GraphQL Playground, type the root (
query) and the first field under the root (customer).
- Type option+space bar (Mac OS) or control+space bar (Windows OS) to view the fields you can query under
customer. We will query forfirstName,lastName, andemail.
- Click the Play button in the middle of the GraphQL Playground to run the query.
- Observe the response.
If you successfully used the login mutation in the previous labs, you should see customer-specific information returned. If you are not currently in a registered customer section or are querying anonymously, you will get a response of null.
In this next query, we will create an alias in a query for product details.
- In the GraphQL Playground, type the root (
query) and the first field under the root (site).
- Type option+space bar (Mac OS) or control+space bar (Windows OS) to view the fields you can query under
site. We will query forproductsand includeedgesandnodesso that we can continue to define the product information we want to query in the next step.
- Type option+space bar (Mac OS) or control+space bar (Windows OS) to view the fields you can query under
products. We will query forentityId,name, anddescription.
- We want to see “name” as “title” in the response, so we will have to create an alias. Type
title:before “name” in the query to create an alias.
- Click the Play button in the middle of the GraphQL Playground to run the query.
- Observe the response.
In this query, we will ask for the price and salesPrice for a specific product. To avoid repetitive text in the query, let’s create two fragments called PriceFields and MoneyFields.
- In the GraphQL Playground, type the root (
query) and the first field under the root (site).
- Under site, type
product. We want to query for a specific product, so type the product ID next to the product field.
The examples below use a product ID of “81.” Insert the product ID for a product on your store in place of “81”.
- We want to query for pricing information. Type option+space bar (Mac OS) or control+space bar (Windows OS) to view the fields you can query under
product. We will query forprices.
- Type option+space bar (Mac OS) or control+space bar (Windows OS) to view the fields you can query under
prices. You will see that both price andsalesPriceare possible fields. This is where the fragment name will belong. Type...PriceFieldsunderprices.
- Now we need to specify what
...PriceFieldsmeans. At the end of the query, start a new line and create a fragment with the namePriceFieldsand the typePrices. Then type the fields for which the fragment applies (priceandsalePrice). In this example, we are creating another fragment calledMoneyFieldswhich will be defined in the next step.
- At the end of the
PriceFieldsfragment, start a new line and create a fragment with the nameMoneyFieldsand the typeMoney. The fields forMoneyFieldswill includevalueandcurrencyCode.
- Click the Play button in the middle of the GraphQL Playground to run the query.
- Observe the response.
In this query, we will query for the first 2 products, plus their IDs, and names. Then we will use pagination to view the next 2 products in the catalog after the previous results.
- In the GraphQL Playground, type the root (
query), query name (paginateProducts), and the first field under the root (site).
- Type
productsin the next field and include how many products we want to see in the response. Then include the page information, cusors, edges, and nodes (entityIdandname).
- Click the Play button in the middle of the GraphQL Playground to run the query.
- Observe the response.
- Copy the end cursor value.
- Next, we want to see the 2 products that come after the products we just queried. Paste the
cursorvalue from the previous step next tofirst:2.
- Click the Play button in the middle of the GraphQL Playground to run the query.
- Observe the response.