Lab - Query Practice
In this lab, you will:
- Create a cart
- Get a cart
- Add a cart line item
- Create a Cart Redirect URL
- Get a checkout
- Add checkout billing address
- Add shipping consignment
- Select a shipping option
- Complete checkout
The cart created in this lab activity will be maintained across requests because of the storefront session cookie used in the GraphQL Playground environment.
Create a Cart Using a Simple Product
In this mutation, we will create a cart using a simple product (ID = 81).
- In the GraphQL Playground, click the Query Variables tab in the bottom left corner.
- Create a variable called
createCartInputthat contains the line item in the cart. Note that what you input for the quantity andproductEntityIdwill be different than what is shown below.
- Next, write a mutation that contains the variable.
- Add the
cartfield and specify that we want tocreateCartbased on the variable, and that you want the response to show theentityId,name, andquantityof the physical item added to the cart.
- Click the Play button in the middle of the GraphQL Playground to run the query.
- Observe the response.
- View the storefront and observe that the item has been added to the cart.
When there is customer context for the GraphQL request that creates a cart (whether from a client-side session or a customer access token), the cart will be assigned to and accessible only by that customer.
Get a Cart
In this query, we will ask for the cart ID, currency, and line items. We will further ask for product pricing information and shipping requirements.
- In the GraphQL playground, begin writing the query with the
root,sitefield, andcartfield. Specify next to the cart field that we want theentityId,currencyCode, andamountin the response.
- Next, let’s add lineItem information to the query. Specify that we want to know the
entityId,sku,name, andquantityof thephysicalItemsin the cart.
- Click the Play button in the middle of the GraphQL Playground to run the query.
- Observe the response.
Add Cart Line Item
In this mutation, we will add another physical item to the cart.
- In the GraphQL Playground, click the Query Variables tab in the bottom left corner.
- Create a variable called
addCartLineItemsInputthat contains thecartEntityIdandlineItemsthat will be added to the cart. Note that what you input for thecartEntityIdandproductEntityIdwill be different than what is shown below.
- Write a mutation that contains the variable.
- Add the
cartfield and specify that we want toaddCartLineItemsbased on the variable and want the response to show the cartentityId.
- Click the Play button in the middle of the GraphQL Playground to run the query.
- Observe the response.
- View the storefront and observe that the new item has been added to the cart.
Create a Cart Redirect URL
In order to load the cart that was created in a headless storefront into the BigCommerce storefront’s own session, you will need to create a cart redirect URL. The steps below will walk you through creating a cart redirect URL with the GraphQL Storefront API. You have the option to create a redirect URL for embedded checkout and redirected checkout, but the example below only shows redirectedCheckoutURL.
Before completing the steps below, you will need the cartEntityId from the cart created in previous lab steps.
- In the GraphQL Playground, click the Query Variables tab in the bottom left corner.
- Create a variable called
cartIdthat contains thecartEntityIdof the cart you want to use for the redirect URL. Note that what you input for cartId will be different than what is shown below.
- Next, write a mutation that contains the variable.
- Next, add the
cartfield and specify that we want tocreateCartRedirectUrlsbased on the variable and want the response to show the redirectedCheckoutUrl and embeddedCheckoutUrl.
- Click the play button to submit the request.
- View your response.
Syncing a Customer Session
When a cart belongs to a registered customer, it’s important that the customer’s logged-in session be maintained when redirecting to checkout.
For client-side requests relying on the BigCommerce storefront session cookie, this is taken care of automatically, as the same session that establishes the customer context for the GraphQL requests is also responsible for their logged-in status in Stencil.
For server-side requests, as long as the appropriate customer access token is provided in the createCartRedirectUrls request, the redirect URL will automatically log the user into this customer session in addition to loading the cart.
Optional Step - Delete a Cart
In this mutation, you will delete a cart.
This step is optional because the next lab activity (Get Checkout) uses the information from the cart you created in the previous steps.
- In the GraphQL Playground, click the Query Variables tab in the bottom left corner.
- Create a variable called
deleteCartInputthat contains the entityId of the cart that will be deleted. Note that what you input for thecartEntityIdand will be different than what is shown below.
- Write a mutation that contains the variable.
- Add the
cartfield and specify that we want todeleteCartbased on the variable and want the response to show the cartdeletedCartEntityId.
- Click the Play button in the middle of the GraphQL Playground to run the query.
- Observe the response.
- View the storefront and observe that the cart is empty.
Get a Checkout
In this query, we will request checkout information such as the billing address, order ID, subtotal, grand total, and outstanding balance.
If you completed the optional step above and deleted the cart, you will need to create a new cart. Go back to the beginning of this lab and follow the steps to create a new cart.
- In the GraphQL playground, begin writing the query with the
root,sitefield, andcheckoutfield. Specify next to the checkout field that we want theentityId,billingAddress,orderId,subTotal,grandTotal, andoutstandingBalancein the response.
- Click the Play button in the middle of the GraphQL Playground to run the query.
- Observe the response.
Add Checkout Billing Address
In this mutation, we will add a billing address to the checkout we queried in the previous step.
- In the GraphQL Playground, click the Query Variables tab in the bottom left corner.
- Create a variable called
addCheckoutBillingAddressInputthat contains the billing address information to be added. Note that what you input for thecheckoutEntityIdand in the address fields will be different than what is shown below.
- Next, write a mutation that contains the variable.
- Next, add the
checkoutfield and specify that we want toaddCheckoutBillingAddressbased on the variable and want the response to show the checkoutentityId.
- Click the Play button in the middle of the GraphQL Playground to run the query.
- Observe the response.
Add Shipping Consignment to Checkout
In this mutation, we will add a shipping address to the checkout.
- Before we can add a shipping consignment, we need to get the
lineItementityIds. In the GraphQL playground, begin writing the query with theroot,sitefield, andcartfield. Specify next to the cart field that we want theentityIdfor eachlineItemin the response.
- Create a variable called
addCheckoutShippingConsignmentsInputthat contains the checkout entityId, shipping address, entityId, and quantity for each lineItem. Note that what you input for thecheckoutEntityId,address, andlineItemsfields will be different than what is shown below. Additionally, you can add more or lesslineItemsto this variable if needed.
- Next, write a mutation that contains the variable.
- Next, add the
checkoutfield and specify that we want toaddCheckoutShippingConsignmentsbased on the variable and want the response to show the checkoutentityId, shipping consignmententityId, available shipping options, and selected shipping option.
- Click the Play button in the middle of the GraphQL Playground to run the query.
- Observe the response.
Select Checkout Shipping Option
In this mutation, we will select a shipping option for the checkout.
- Before we can select a shipping option, we need to find what shipping options are available. In the GraphQL playground, begin writing the query with the
root,sitefield,checkoutfield, and shipping consignments field. Specify next to the shipping consignments field that we want theentityId,description, andtypefor eachavailableShippingOptionsin the response.
- Create a variable called
selectCheckoutShippingOptionInputthat contains the checkout entityId, consignment entityId, and shipping option entityId.
- Next, write a mutation that contains the variable.
- Next, add the
checkoutfield and specify that we want toselectCheckoutShippingOptionbased on the variable and want the response to show the checkoutentityId.
- Click the Play button in the middle of the GraphQL Playground to run the query.
- Observe the response.
Complete Checkout
In this mutation, we will complete the checkout that we created in the previous steps.
- Create a variable called
completeCheckoutInputthat contains the checkoutentityId.
- Next, write a mutation that contains the variable.
- Next, add the
checkoutfield and specify that we want tocompleteCheckoutbased on the variable and want the response to show the orderentityIdand payment access token.
- Click the Play button in the middle of the GraphQL Playground to run the query.
- Observe the response.