Learn how to create your own food ordering application and by extension any e-commerce app using modern development tools like Strapi, Gridsome & Snipcart. In this episode: Consuming products with Gridsome and GraphQL.
This article is part of a guest series by the great Ekene Eze. He’s leading the Developer Experience team at Flutterwave and wrote this blog post through the Write for the Community program.
This series will walk you through the processes of setting up your own food ordering application and by extension any e-commerce app using modern development tools like Strapi, Gridsome and Snipcart.
Table of Contents: 1. Part 1 - Generating a Strapi app and creating products 2. Part 2 - Setting up a Gridsome project 3. Part 3 - Consuming products with Gridsome and GraphQL 4. Part 4 - Creating single product views with Gridsome templates 5. Part 5 - Implementing cart and checkout with Snipcart 6. Part 6 - Deploying the apps
In the last part, we walked through the process of setting up a Strapi application and creating products for our application. In this part, we will connect our Gridsome app with the Strapi app we created in the first part to fetch products and display for users. https://mealzers.netlify.app
The products we'll display in our Gridsome application will come from the Strapi products endpoint. Luckily for us, Gridsome makes it possible for us to fetch data from the Strapi products endpoint into the GraphQL data layer that manages the data for our Gridsome application.
To do this, we need to install Axios. Axios is a promise-based HTTP client for the browser that makes it possible for you to communicate between different services. Run the following command to install Axios:
npm i axios
With Axios installed, open the project's gridsome.server.js
file and update it with the snippet below:
1//gridsome.server.js
2const axios = require("axios");
3module.exports = function(api) {
4 // whitelist vuetify for webpack code here
5 };
6
7 api.loadSource(async (actions) => {
8 const { data } = await axios.get("http://localhost:1337/products");
9 const collection = actions.addCollection({
10 typeName: "Product",
11 });
12 for (const product of data) {
13 collection.addNode({
14 id: product.id,
15 title: product.title,
16 price: product.price,
17 rating: product.rating,
18 description: product.description,
19 image: product.image.formats.thumbnail.url,
20 instructions: product.instructions,
21 });
22 }
23 });
24 api.createPages(({ createPage }) => {
25 // Use the Pages API here: https://gridsome.org/docs/pages-api/
26 });
Here, we are making a basic API call to fetch a list of products from our Strapi products endpoint which I'm running locally at localhost:1337/products
. You can consider collection in the above snippet as an array where we'll push each individual product returned from the endpoint. Hence, collection.addNode
is the equivalence of the regular array.push
.
Each node contains all the details we want to get from a product, like the title, description, price etc. As a result, we can loop through the entire data returned and get the individual products as well as their respective details.
Having fetched these products from Strapi, we can display them for users to interact with on the frontend. To do that, first let's create a new src/components/Products.vue
component. Within this component, we'll create the template to organize the products using Vuetify's card components. Update it with the snippet below:
1// src/components/Products.vue
2<template>
3 <div>
4 <h1
5 v-if="show"
6 align="center"
7 justify="center"
8 class="py-5 orange white--text"
9 >
10 {{ banner }}
11 </h1>
12 <v-container class="mb-3">
13 <v-row :align="align" no-gutters>
14 <v-col md="4" v-for="edge in $page.products.edges" :key="edge.node.id">
15 <g-link
16 :to="`/products/${edge.node.id}`"
17 style="text-decoration: none; color: inherit"
18 >
19 <v-card :loading="loading" class="ma-3" shaped>
20 <v-img
21 height="250"
22 :src="`http://localhost:1337${edge.node.image}`"
23 ></v-img>
24 <v-card-title>{{ edge.node.title }}</v-card-title>
25 <v-card-text>
26 <v-row align="center" class="mx-0">
27 <v-rating
28 :value="edge.node.rating"
29 color="amber"
30 dense
31 half-increments
32 readonly
33 size="14"
34 ></v-rating>
35
36 </v-row>
37 <div class="my-4 subtitle-1">$ {{ edge.node.price }}</div>
38 <div>
39 {{ `${edge.node.description.slice(0, 120)}...` }}
40 </div>
41 </v-card-text>
42 <v-card-actions>
43 <v-btn
44 rounded
45 outlined
46 color="orange"
47 text
48 >
49 Add to cart
50 </v-btn>
51 </v-card-actions>
52 </v-card>
53 </g-link>
54 </v-col>
55 </v-row>
56 </v-container>
57 </div>
58</template>
59<script>
60export default {
61 props: ["banner", "show"],
62};
63</script>
At the moment, all we've done is create a Vuetify template that will hold the product data coming from our GraphQL data layer. We have provisioned fields for product description, price, title, rating, and image. To show this component to the users, we'll have to render it in the Index page. So let's do minor updates to the index.vue
file.
In the script section, import the Products component like so:
1//index.vue
2import Products from "@/components/Products.vue";
In the template section, just after the
1//index.vue
2<Products show="false" banner="Best Sellers" />
Finally to get all the products data we need to render here from our data layer, let's define a GraphQL query on our Product type:
1// src/pages/index.vue
2<page-query>
3query{
4 products: allProduct{
5 edges{
6 node{
7 id,
8 title,
9 description,
10 rating,
11 price,
12 image
13 }
14 }
15 }
16}
17</page-query>
Here, we are querying for the product details from our GraphQL data layer to use in the template. In the query above, consider edges to be a regular array of products and node to be the individual product. Before we proceed, let's run this query in the GraphQL explorer and see what the result look like:
1{
2 "data": {
3 "products": {
4 "edges": [
5 {
6 "node": {
7 "id": "7",
8 "title": "Seafood",
9 "description": "Seafood is any form of sea life regarded as food by humans. Seafood prominently includes fish and shellfish. Shellfish include various species of molluscs, crustaceans, and echinoderms. Historically, sea mammals such as whales and dolphins have been consumed as food, though that happens to a lesser extent in modern times",
10 "price": 300,
11 "rating": 5,
12 "image": "/uploads/thumbnail_seafood_d3e69aad71.png"
13 }
14 },
15 {
16 "node": {
17 "id": "6",
18 "title": "Sandwich",
19 "description": "Spread mayo, butter or cream cheese all the way to the edges of each slice of bread to create a seal against wet sandwich fillings. Also, try packing high moisture ingredients, like tomatoes, pickles, and cucumbers, separately",
20 "price": 150,
21 "rating": 3,
22 "image": "/uploads/thumbnail_miscellaneous_e0812098cf.png"
23 }
24 },
25 {
26 "node": {
27 "id": "5",
28 "title": "Dessert",
29 "description": "Dessert is a course that concludes a meal. The course usually consists of sweet foods, such as confections dishes or fruit, and possibly a beverage such as dessert wine or liqueur, however in the United States it may include coffee, cheeses, nuts, or other savory items regarded as a separate course elsewhere",
30 "price": 100,
31 "rating": 4,
32 "image": "/uploads/thumbnail_dessert_4fdacb1066.png"
33 }
34 },
35 {
36 "node": {
37 "id": "4",
38 "title": "Lamb",
39 "description": "Lamb, hogget, and mutton are the meat of domestic sheep (species Ovis aries) at different ages. A sheep in its first year is called a lamb, and its meat is also called lamb",
40 "price": 250,
41 "rating": 3,
42 "image": "/uploads/thumbnail_lamb_8f452bb9a3.png"
43 }
44 },
45 {
46 "node": {
47 "id": "3",
48 "title": "Chicken wings",
49 "description": "Chicken is a type of domesticated fowl, a subspecies of the red junglefowl. It is one of the most common and widespread domestic animals",
50 "price": 400,
51 "rating": 3,
52 "image": "/uploads/thumbnail_chicken_c6742f8df3.png"
53 }
54 },
55 {
56 "node": {
57 "id": "2",
58 "title": "Homemade pasta",
59 "description": "Pasta is a staple food of traditional Italian cuisine, with the first reference dating to 1154 in Sicily. Humans have been eating beef since prehistoric times",
60 "price": 400,
61 "rating": 5,
62 "image": "/uploads/thumbnail_pasta_1e163eb9f7.png"
63 }
64 },
65 {
66 "node": {
67 "id": "1",
68 "title": "Grilled Beef",
69 "description": "Beef is the culinary name for meat from cattle, particularly skeletal muscle. Humans have been eating beef since prehistoric times. Beef is a source of high-quality protein and essential nutrients. Check below for preparation instructions",
70 "price": 300,
71 "rating": 4,
72 "image": "/uploads/thumbnail_beef_b95f539dc8.png"
73 }
74 }
75 ]
76 }
77 }
78}
At this point, if you save all the updates made to the index.vue
file, we should get an updated view on the browser at localhost:8080
:
At the moment, we've achieved our goal of rendering the products from our Strapi application on the Gridsome frontend app.
You might have noticed that when you click on any of the products, it opens a new route http://localhost:8080/products/{id}
which represents the single page route for that particular product. This is possible because we wrapped each individual product card in a <g-link>
tag which basically provides client-side routing capabilities to our Gridsome application:
1<g-link
2 :to="`/products/${edge.node.id}`"
3 style="text-decoration: none; color: inherit"
4>
5// product card here
6</g-link>
So quick recap. In this part, we've consumed the product endpoint we created with Strapi in the first part of this series. Gridsome's GraphQL data layer was instrumental in this process as we have demonstrated. It made it easy to access the data we need wherever we need. We also got our hands dirty with Vuetify, creating components and styling the product cards was equally just as fun. In the next part, we'll look at how we can display product details in a single product view page where customers can place orders.
Ekene is a Developer Experience Engineer and Technical Writer. He is currently working with the Developer Experience team at Netlify where they build tools, create content, open-source repos, and demos to teach and also help developers build a better web with Netlify's products and services.