NewPlatano

Tutorial Fullstack React Native | GraphQL | Apollo Server & Client

Beto, April 15, 2022 · 2,718 views

Learn how to create a fullstack React Native application connected to a GraphQL backend using Apollo Server and Apollo Client. You will learn to build the server with Node.js, define your GraphQL schema, and connect the React Native client to query and mutate data.

It is ideal for developers with some JavaScript and backend experience who want to understand how to integrate GraphQL APIs with React Native apps, including real-time updates like likes and new posts.

What's inside

  • Project setup: folder structure and tools
  • Creating the Node.js Apollo Server
  • Defining GraphQL schema with types, queries, and mutations
  • Managing data with a simple in-memory dataset
  • Connecting React Native client with Apollo Client
  • Real-time UI updates on likes and new posts
  • Useful commands and resources for setup

Project setup: folder structure and tools

The tutorial starts by creating a project folder with two main subfolders: for the React Native app and for the backend. The server folder is initialized with Node.js using to create a package.json.

Visual Studio Code is used as the editor. I emphasize organizing the backend and frontend separately to keep concerns clear. This structure helps when scaling or deploying each part independently.

Creating the Node.js Apollo Server

Inside the folder, I install Apollo Server and GraphQL packages using:

The server entry point is an file where Apollo Server is imported and configured. The server will handle GraphQL requests and serve data to clients.

I show how to import Apollo Server and GraphQL tools, then instantiate a new ApolloServer instance with schema definitions and resolvers.

Defining GraphQL schema with types, queries, and mutations

The schema is defined using GraphQL SDL inside the server code. A type is created with fields like (Int!), (String!), (String), (String), (String), (Int), and (String).

The exclamation mark (!) marks required fields. Queries like return an array of posts. Mutations allow creating posts and updating likes.

I explain how to write these type definitions with tagged template literals and how to expose queries and mutations to the client.

Managing data with a simple in-memory dataset

For simplicity, the backend uses an in-memory array to store posts. This simulates a database but is volatile and resets on server restart.

I demonstrate how to create, update, and retrieve posts from this array inside resolver functions. This approach is good for learning but should be replaced with a real database in production.

Connecting React Native client with Apollo Client

The React Native app is created with Expo and uses Apollo Client to connect to the GraphQL server.

I show how to install Apollo Client packages and set up the ApolloProvider at the root of the app. Queries and mutations are executed using Apollo hooks like and .

This setup allows the app to fetch posts, create new ones, and update likes with real-time UI feedback.

Real-time UI updates on likes and new posts

The demo app shows two iPhones side by side. When a user likes a post or creates a new one on one device, the other device updates automatically.

This real-time behavior is achieved by refetching queries or using Apollo Client's cache updates after mutations. I highlight how to keep UI state in sync with backend data.

Useful commands and resources for setup

Commands used include:

  • to initialize Node.js project
  • for server dependencies
  • Expo CLI commands to create and run the React Native app

I also mention a GitHub repo and a community Discord for help. The full project code is available on Beto’s platform for free.

Resources

CourseReact Native course

YouTubeFullstack React Native with GraphQL and Apollo

Let's connect!

Had a win? Get featured on Code with Beto.Share your story