Simply copy and paste the following command line in your terminal to create your first Strapi project.
npx create-strapi-app
my-project
This tutorial is part of the « E-commerce website with Strapi, Nuxt.js, GraphQL and Stripe
Note: The source code is available on Github
The installation script has just created an empty project. We will now start configuring it using the Content Types Builder, which allows you to create your content architecture using single or collection types. It is a core plugin of Strapi, only accessible when the application is in a development environment.
You will display a list of restaurants in your web app. The list is going to be managed through your Strapi API. At the end of this section, you should have a Homepage that looks like this:
A Strapi API includes, by default, the user
Content Type. Right now, you need restaurants, so your new Content Type is going to be, as you already guessed, restaurant
.
Restaurant
for the Display name, and click Continue.name
in the Name field.image
under the Name field, and check the Single media option then click Finish.Well done! You created your first Content Type. The next step is to add some restaurants to your database.
Let's create a restaurant.
Create as many restaurants as you would like to see in your app. If you're lacking some inspiration, you can check Deliveroo 🙈 . Having the items in database is great. Being able to request them from the Strapi API is even better.
When you were creating your restaurant
Content Type, Strapi created, behind the scenes, a set of files located in api/restaurant
. These files include the logic to expose a fully customizable CRUD API. The find
route is available at http://localhost:1337/api/restaurants. Try to visit this URL and will be surprised to be blocked by a 403 forbidden error. This is actually totally normal, new Strapi APIs are secured by design.
Don't worry, making this route accessible is actually super intuitive:
We have just added some new restaurants. We now have enough content to consume. But first, we need to make sure that the content is publicly accessible through the API:
Now go back to http://localhost:1337/api/restaurants. At this point, you should be able to see your list of restaurants. By default, the APIs generated with Strapi use REST conventions. In the next step, you will transform them into GraphQL.
We will use the GraphQL plugin in our Strapi project to fetch and mutate your content. To use the plugin, run the command below.
1
2
3
# Ctrl + C to close process
cd backend
npm run strapi install graphql
This plugin will add GraphQL functionality to your app. After the installation is complete:
npm run develop
to start the development server.[http://localhost:1337/graphql](http://localhost:1337/graphql)
to access GraphQL Playground.The GraphQL Playground has an inbuilt text editor for you to enter your GraphQL commands, a play button for you to run your code and a screen to display the return values, error, or success message. Try the following query in your GraphQL Playground:
1
2
3
4
5
6
7
query Restaurants {
restaurants {
data {
id
}
}
}
You should see the restaurants. If you did, you are ready to go onto the next step.
Apollo Client is a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL. Use it to fetch, cache, and modify application data, all while automatically updating your UI. Let's install the packages we need. Open a new terminal:
1
2
cd frontend
npm install @nuxtjs/apollo graphql-tag
Now that we have the dependencies we need, let's import the @nuxtjs/apollo
. Add the following module and configurations to your nuxt.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// nuxt.config.js
const strapiBaseUri = process.env.API_URL || "http://localhost:1337";
export default {
// Modules: https://go.nuxtjs.dev/config-modules
modules: ['@nuxtjs/apollo'],
// Apollo: https://github.com/nuxt-community/apollo-module#usage
apollo: {
clientConfigs: {
default: {
httpEndpoint: `${strapiBaseUri}/graphql`,
}
}
}
}
It looks you are going to the right direction. In the next step, you will display these restaurants in your Nuxt.js app.
Open pages/index.vue
with your text editor, and copy/paste the following code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// pages/index.vue
<template>
<div class="uk-container uk-container-xsmall">
<h1 class="uk-heading-small">
<span class="uk-invisible">Restaurants</span>
<input
v-model="query"
class="uk-search-input"
type="search"
placeholder="Type to search"
/>
</h1>
<div
v-for="restaurant in filteredList"
:key="restaurant.id"
class="
uk-card uk-card-default uk-grid-collapse uk-child-width-1-2 uk-margin
"
uk-grid
>
<figure class="uk-flex-last uk-card-media-right uk-cover-container">
<img
:src="getStrapiMedia(restaurant.attributes.image.data.attributes.url)"
:alt="restaurant.attributes.image.data.attributes.alternativeText"
uk-cover
/>
</figure>
<div>
<div class="uk-card-body uk-card-small">
<h2 class="uk-card-title">{{ restaurant.attributes.name }}</h2>
<NuxtLink
class="uk-button uk-button-text"
:to="{ name: 'restaurants-id', params: { id: restaurant.id } }"
>
See dishes
</NuxtLink>
</div>
</div>
</div>
<div v-if="filteredList.length == 0" class="uk-heading-small">
<p>No restaurants found</p>
</div>
</div>
</template>
<script>
import { getStrapiMedia } from '@/utils/media'
import restaurantsQuery from '@/apollo/queries/restaurants'
export default {
data() {
return {
restaurants: [],
query: '',
}
},
apollo: {
restaurants: {
prefetch: true,
query: restaurantsQuery,
},
},
computed: {
filteredList() {
if (!this.restaurants?.data) return []
return this.restaurants?.data?.filter((restaurant) => {
return restaurant.attributes.name
.toLowerCase()
.includes(this.query.toLowerCase())
})
},
},
methods: {
getStrapiMedia,
},
}
</script>
We’re using UIkit a lightweight and modular front-end framework for developing fast and powerful web interfaces, it contains a set of layout components like Card
and Grid
that make it easy to style your website.
Let’s run the following command to install uikit:
1
npm install uikit
To import UIkit, create a new plugins/uikit.js
and copy/paste the following code.
1
2
3
4
5
6
7
8
9
10
// plugins/uikit.js
import Vue from 'vue'
import UIkit from 'uikit/dist/js/uikit-core'
import Icons from 'uikit/dist/js/uikit-icons'
UIkit.use(Icons)
UIkit.container = '#__nuxt'
Vue.prototype.$uikit = UIkit
Reference the new UIkit in your nuxt.config.js
like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
// nuxt.config.js
export default {
// Global CSS: https://go.nuxtjs.dev/config-css
css: [
"uikit/dist/css/uikit.min.css",
"uikit/dist/css/uikit.css",
],
// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
plugins: [
{ src: '~/plugins/uikit.js', ssr: false }
]
}
We can load GraphQL queries over .gql
files. This enable queries to be separated from logic.
Let’s create a new graphql
template. Create a new file apollo/queries/restaurants.gql
, and copy/paste the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
query Restaurants {
restaurants {
data {
id
attributes {
name
image {
data {
attributes {
url
alternativeText
}
}
}
}
}
}
}
Finally, let’s create a new folder utils
inside the frontend
directory, where we will put our commons helpers.
Then create a new empty file media.js
in the utils
directory, media.js
will provide us with a tiny helper to obtain images absolute URL.
Let’s add our getStrapiMedia
util to the utils/media.js
file:
1
2
3
4
5
6
7
8
9
10
// utils/media.js
export function getStrapiMedia(url) {
// Check if URL is a local path
if (url.startsWith("/")) {
// Prepend Strapi address
return `${process.env.strapiBaseUri}${url}`;
}
// Otherwise return full URL
return url;
}
And create a new environment variable in your nuxt.config.js
:
1
2
3
4
5
6
7
// nuxt.config.js
export default {
// ENV: https://nuxtjs.org/docs/configuration-glossary/configuration-env
env: {
strapiBaseUri,
},
}
This will append our Strapi Base URI to the image source. Great! After completing the homepage:
npm run dev
to start the development server.http://localhost:3000
to view your application.Well done! You can now see your restaurants! 🍔 In the next section, you will learn how to display the list of dishes.
Pierre created Strapi with Aurélien and Jim back in 2015. He's a strong believer in open-source, remote and people-first organizations. You can also find him regularly windsurfing or mountain-biking!