Strapi blog logo
  • Product

      Why Strapi?Content ArchitectureRoadmapIntegrationsTry live demo
      OverviewContent Types BuilderCustomizable APIMedia LibraryRoles & Permissions
      Discover Strapi Enterprise EditionDiscover our partners
  • Pricing

  • Solutions

      Static WebsitesMobile ApplicationsCorporate websitesEditorial SitesEcommerce
      Delivery HeroL'EquipeSociete GeneralePixelDustBanco International
      Discover all our user stories
  • Community

      CommunityWrite for the communityWall of LoveStrapi Conf 2021
      SlackGitHubYoutubeCommunity Forum
      Meet the Strapi Community StarsDiscover the Strapi Showcase
  • Resources

      BlogStartersNewsroomSupport
      Strapi AcademyTutorialsVideos GuidesWebinars
      The Guide to Headless CMS Strapi Community Forum
  • Docs

      Getting StartedInstallationConfigurationsDeploymentUpdate versionContent API
      Getting StartedContent ManagerContent-Types BuilderUsers, Roles & PermissionsPlugins
      Developer DocumentationStrapi User Guide

Looking for our logo ?

Logo Brand download
Download Logo Pack
See more Strapi assets
Strapi blog logo
  • Product

    Product

    • Why Strapi?
    • Content Architecture
    • Roadmap
    • Integrations
    • Try live demo

    Features

    • Overview
    • Content Types Builder
    • Customizable API
    • Media Library
    • Roles & Permissions
    • Discover Strapi Enterprise Edition
    • Discover our partners
    Features cover

    Unlock the full potential of content management

    See all features
    Strapi Enterprise Edition

    Discover the advanced features included in Strapi Enterprise Edition.

    Get Started
  • Pricing
  • Solutions

    Solutions

    • Static Websites
    • Mobile Applications
    • Corporate websites
    • Editorial Sites
    • Ecommerce

    Stories

    • Delivery Hero
    • L'Equipe
    • Societe Generale
    • PixelDust
    • Banco International
    • Discover all our user stories
    Delivery Hero team

    Delivery Hero manages their partner portal with Strapi.

    Read their story
    turn 10 studios website

    How 1minus1 delivered a creative website for Turn10 Studios 25% faster with Strapi

    Discover their story
  • Community

    Community

    • Community
    • Write for the community
    • Wall of Love
    • Strapi Conf 2021

    Resources

    • Slack
    • GitHub
    • Youtube
    • Community Forum
    • Meet the Strapi Community Stars
    • Discover the Strapi Showcase
    Strapi Conf

    The first Strapi Global User Conference.

    Register now
    Write for the community

    Contribute on educational content for the community

    Discover the program
  • Resources

    Resources

    • Blog
    • Starters
    • Newsroom
    • Support

    Learning

    • Strapi Academy
    • Tutorials
    • Videos Guides
    • Webinars
    • The Guide to Headless CMS
    • Strapi Community Forum
    Introducing Strapi Academy

    Everything you need to know to master Strapi.

    Go to the academy
    Strapi Repository on GitHub

    Get started with the Strapi repository

    Go to repository
  • Docs

    Developers

    • Getting Started
    • Installation
    • Configurations
    • Deployment
    • Update version
    • Content API

    User Guide

    • Getting Started
    • Content Manager
    • Content-Types Builder
    • Users, Roles & Permissions
    • Plugins
    • Developer Documentation
    • Strapi User Guide
    Install Strapi

    Install Strapi locally or wherever you need.

    Get Started
    Migration Guides Strapi

    Using a previous version of Strapi? Migrate to the latest.

    Read Guides
Get Started
Back to articles

Deliveroo clone with Next.js, GraphQL, Strapi and Stripe (2/7)

strapi-next-restaurants
  • Share on facebook
  • Share on linkedin
  • Share on twitter
  • Share by email
Ryan Belke

Ryan Belke

November 12, 2018

Strapi Next.js tutorial

Disclaimer You can find the updated version of this tutorial here

This tutorial is part of the « Cooking a Deliveroo clone with Next.js (React), GraphQL, Strapi and Stripe » tutorial series.

Table of contents

  • 🏗️ Setup (part 1)
  • 🏠 Restaurants (part 2) - current
  • 🍔 Dishes (part 3)
  • 🔐 Authentication (part 4)
  • 🛒 Shopping Cart (part 5)
  • 💵 Order and Checkout (part 6)
  • 🚀 Bonus: Deploy (part 7)

Note: the source code is available on GitHub: https://github.com/strapi/strapi-examples/tree/master/nextjs-react-strapi-deliveroo-clone-tutorial.

🏠 Restaurants list

First of all, we need to display the list of restaurants in our web app. Of course, this list is going to be managed through our API. So, we are going to start configuring it.

Define Content Type

A Content Type, also called a model, is a type of data. A Strapi API includes, by default, the user Content Type. Right now, we need restaurants, so our new Content Type is going to be, as you already guessed, restaurant (Content Types are always singular).

Here are the required steps:

  • Navigate to the Content Type Builder (http://localhost:1337/admin/plugins/content-type-builder).
  • Click on Add Content Type.
  • Set restaurant as name.
  • Click on Add New Field and create the followings fields:
    • name with type String.
    • description with type Text with Rich Text Editor (in the Advanced Settings section of the modal, select Display as a WYSIWYG).
    • image with type Media.
  • Click on Save.

Content Type Builder

At this point, your server should have automatically restarted and a new link Restaurant appears in the left menu.

Add some entries

Well done! You created your first Content Type. The next step is to add some restaurants in your database. To do so, click on "Restaurant" in the left menu (http://localhost:1337/admin/plugins/content-manager/restaurant).

You are now in the Content Manager plugin: an auto-generated user interface which let you see and edit entries.

Let's create a restaurant:

  • Click on Add New Restaurant.
  • Give it a name, a description and drop an image.
  • Save it.

Create as many restaurants as you would like to see in your apps.

Content Manager

Allow access

Having the items in database is great. Being able to request them from an API is even better. As you already know, Strapi's mission is to create API (I have got a super secret anecdote for you: its name is coming from Bootstrap your API 😮).

When you were creating your restaurant Content Type, Strapi created, behind the scene, a few set of files located in api/restaurant. These files include the logic to expose a fully customisable CRUD API. The find route is available at http://localhost:1337/restaurants. Try to visit this URL and will be surprised to be blocked by a 403 forbidden error. This is actually totally normal: Strapi APIs are secured by design.

Don't worry, making this route accessible is actually super intuitive:

  • Navigate to the Settings then Roles & Permissions.
  • Click on the Public role.
  • Tick the find and findone checkboxes of the Restaurant section.
  • Save.

Important: do the same thing for the authenticated role.

Now go back to http://localhost:1337/restaurants: at this point, you should be able to see your list of restaurants.

Users Permissions

Enabling GraphQL

By default, API generated with Strapi are best on REST conventions. What if I would tell you that you could transform them into GraphQL within 10 seconds?

Well, let me prove you that.

A GraphQL plugin, which will make all the work for you, is available for Strapi. Install it with the Strapi CLI:

navigate to your backend folder where strapi is installed and install graphql using the strapi install command:

cd backend
strapi install graphql

Or click "Marketplace" on your admin dashboard and select GraphQL

And that's it, you are done installing GraphQL.

Make sure to restart your strapi server if it does not auto restart

#Important: ####Downgrage graphql-js dependency

With the current implementation you will receive a GraphQL error when trying to query a MongoDB _id field:

This is currently being fixed by the graphql-js maintainers but as of time of writing has not been fixed (github issue linked below).

message: "ID cannot represent value: { _bsontype: "ObjectID", id: <Buffer 5b c1 5b 8b a1 a2 92 54 65 51 88 23> }"

picture

This is caused by a breaking change in the graphql-js update on how the ID's from a MongoDB are serialized: https://github.com/graphql/graphql-js/issues/1518.

Based on the comments it looks like the maintainers are working on a fix but as of the time of writing (10/12/2018) the fix is to downgrade your graphql package to: GraphQL 0.13.2.

The dependency package is in the /backend folder under the GraphQL plugin folder listed at: /backend/plugins/graphql/.

cd plugins/graphql
npm install graphql@0.13.2 --save

Inside of your packages.json file your graphql dependency should be listed as "graphql": "0.13.2"

Until the issue is fixed if you upgrade your packages inside the server you will break the implementation.


GraphQL installation

Restart your server, go to http://localhost:1337/graphql and try this query:

{
  restaurants {
    _id
    name
  }
}

Strapi GraphQL

Display restaurants

It looks you are going to the right direction. What if we would display these restaurants in our Next app?

Restaurants list

Install Apollo in the frontend of our application, navigate to the /frontend folder:

cd ..
cd ..
cd ..
cd frontend
yarn add react-apollo next-apollo graphql gql recompose

To connect our application with GraphQL we will use Apollo and the next-apollo implementation to wrap our components in a withData HOC to give them access to make apollo data queries.

There are a couple differnt approaches to implementing GraphQL into a Nextjs app, the approach we will take is extracting the Apollo logic into lib file and wrapping our components with a Higher Order Component called withData to handle the GQL queries inside each respective components.

Example repo detailing the Apollo Next.js implementation: https://github.com/adamsoffer/next-apollo-example.

Create a lib directory in the root of the project:

mkdir lib
cd lib
touch apollo.js

Path: /frontend/lib/apollo.js

We will generate the list of Restaurants inside a RestaurantList file as:

cd ..
cd components

mkdir RestaurantList
cd RestaurantList
touch index.js

Path: /frontend/components/RestaurantList/index.js

Now update your /pages/index.js home route to display the Restaurant list:

Path: /frontend/pages/index.js

We will need to update our _app.js file to wrap our application with the Apollo Provider that will enable GraphQL to make queries:

Path: /frontend/pages/_app.js

Now you should see the list of restaurants on the page.

Well done!

🍔 In the next section, you will learn how to display the list of dishes: https://blog.strapi.io/strapi-next-dishes.

  • Share on facebook
  • Share on linkedin
  • Share on twitter
  • Share by email

Unleash content.

Starters
Get Started

Strapi is the leading open-source Headless CMS. Strapi gives developers the freedom to use their favorite tools and frameworks while allowing editors to easily manage their content and distribute it anywhere.

Product

  • Why Strapi?
  • Content Architecture
  • Features
  • Enterprise Edition
  • Partner Program
  • Roadmap
  • Support
  • Integrations
  • Try live demo
  • Changelog

Resources

  • How to get started
  • Meet the community
  • Tutorials
  • API documentation
  • GitHub repository
  • Starters
  • Strapi vs Wordpress
  • The Guide to headless CMS

Integrations

  • Gatsby CMS
  • React CMS
  • Vue.js CMS
  • Nuxt.js CMS
  • Next.js CMS
  • Angular CMS
  • Gridsome CMS
  • Jekyll CMS
  • 11ty CMS
  • Svelte CMS
  • Sapper CMS
  • Ruby CMS
  • Python CMS

Company

  • About us
  • Blog
  • Careers
  • Contact
  • Newsroom
  • © 2021, Strapi
  • LicenseTermsPrivacy