Building Composable Commerce → Shopify
In this section, we will replace current mocks for both Home Page and Product Page with actual data from the Shopify. To integrate with Apollo, we will be using the official Apollo module for Nuxt.
The installation is very similar to other modules that we have already added to our storefront. Let’s install the module with the following command:
Next, let’s add it to the modules
array in nuxt.config.ts
file:
But that is not it yet as we also need to configure Apollo to fetch the data from Shopify. We can do so by registering a new client with a host and by passing a X-Shopify-Storefront-Access-Token
as a header.
Let’s add the following apollo
configuration object in our nuxt.config.ts
file:
Now, we also need to create a .env
file with these environment variables. For this tutorial, I used the publicly available credentials from Shopify demo playground so no need to worry about them:
For your project, remember to replace them to fetch the data from your store instead.
Fetching data from Shopify
As we have already configured our Apollo GraphQL client to work with Shopify, we can now start fetching the actual data from the platform and populate our storefront application with it.
Let’s create a new folder graphql
and inside of it a new file called getProductsQuery.ts
. In here, we will write a GraphQL query that will be responsible for fetching the data about our products and will also accept some variables like the number of products or query.
In this query, we will be fetching data about products that will be used later on in our Vue component as product title, image, price, etc. Apart from mandatory param of first
which will be used to determine how many products we want to fetch, we will also add here an optional query
parameter that will be used later in the Product page to fetch related products. To use it, we will modify the index.vue
page in a following way:
Let’s stop for a second to explain each step individually:
- We are importing previously created GraphQL query.
- We are registering a new variable called
variables
where we could pass the amount of products o query. - We are calling a
useAsyncQuery
composable that will under the hood send this query to Shopify (with variables as well). - We have access to the response from a
data
variable. - We are using the response data in the template to display a list of products (the complexity of nested properties is caused by nesting in Shopify, we cannot do anything about it unfortunately 😞).
If we did everything properly, we should see the following result in the browser:
The images are not yet properly optimized as we need to add them to the image.domains
array in nuxt.config.ts
. Let’s do this now:
Our Home Page looks good so far. Let’s focus right now on the Product Page so that we could navigate from the Home Page and have real data here as well!
In the graphql
folder, create a new file called getProductQuery
and add the following code:
n here, we are fetching the data about our product that we will display in the Product Page. We will be using handle as the unique identifier for each product. Apart from the regular data, we will be also fetching the variant ID that will be used later on for redirecting to checkout.
Now, let’s use it in the pages/products/[handle].vue
page to fetch the data about the product after visiting the Product Page with a certain handle:
A lot of things have happened here so let’s stop for a second to discuss each meaningful part:
- Import
getProductQuery
GraphQL query. - Utilize
useRoute
composable to get access to route params where our handle lives. - Call
useAsyncQuery
composable withgetProductQuery
query and pass handle as variables. - Create a helper computed property that will create a price of the product in a form
5 $
- Use the data in the template.
If we did everything correctly, we should see the following result in the browser:
To not put all the logic in the same time, I decided to split fetching data about the product from fetching data about the related products. Let’s do it right now.
We will add another call to the Shopify to fetch related products (I intentionally removed some of the code so that it will be easier to understand):
Let’s stop for a second here to explain what was done:
- Import
getProductsQuery
GraphQL query. - Call useAsyncQuery composable where we are passing variables to get first three products and as a query we are passing
product_type:${product.value.productByHandle.productType}
→ this is Shopify-specific way of fetching data based on some query conditions. - We use the related products data in the template
If we did everything correctly, we should see the following result in the browser:
Uff that was a lot! There is only one thing left to do here in terms of Shopify itself and it is a mutation that will create a Check out session once we click Pay button. Let’s add it now.
We will create a new file in graphql
folder called createCheckoutMutation.ts
:
As a required parameter we will pass a variantId that we will get from fetching the data about the product in Product Page. As a return value we will get the url of checkout that we will redirect the user to.
Now, let’s use it in thepages/products/[handle].vue
page
Let’s stop for a second here to explain what was done:
- Import
createCheckoutMutation
GraphQL mutation. - Create a new method called
redirectToPayment
. It will send the mutation to Shopify with the parameter ofvariantId
. - We are redirecting the user to the webUrl returned by the previous GraphQL mutation.
If we did everything correctly, after clicking a Pay
button, after a while we should be redirected to the Shopify checkout page like the following (the url could be something like https://graphql.myshopify.com/checkouts/co/8201a00b239e6d9bd081b0ee9fdaaa38/information
:
It was a lot of stuff but together we have managed to make through it! Now, we will move into Storyblok to see how we can add the dynamic content and also, use the Shopify plugin for Storyblok 🚀
In the next part of this series, we will see how to implement Storyblok in our project and deploy it to Vercel.