Building Chatty — Part 1: Setup

A WhatsApp clone with React Native and Apollo

Simon Tucker
React Native Training
9 min readApr 9, 2017

--

This is the first blog in a multipart series where we will be building Chatty, a WhatsApp clone, using React Native and Apollo. You can view the code for this part of the series here.

Overview

Each part of this series will be focused on teaching a core concept of Apollo or React Native. We’ll start from scratch, and by the end of the series, we’ll have a kick-ass group messaging application with real-time updates.

7 Steps later…

Steps

  1. Setup
  2. GraphQL Queries with Express
  3. GraphQL Queries with React Apollo
  4. GraphQL Mutations & Optimistic UI
  5. GraphQL Pagination
  6. GraphQL Subscriptions
  7. GraphQL Authentication

Future posts beyond the core series will cover more complex features like push notifications, file uploads, and query optimizations.

Since we are using shiny new tech, this series will be a living document. I will update each post as the tools we use continue to advance. My goal is to use this series as a best practices model for building a complex application using some of the best software available.

With that in mind, if you have any suggestions for making this series better, please leave your feedback!

The Stack

Chatty will use the following stack:

This is a pretty awesome stack for building complex real-time native applications.

For those of you who are new to Apollo, I just want to point out some of the coolest built-in features for Apollo with React:

  • Smart query caching (client side state gets updated and cached with each query/mutation)
  • Subscriptions (realtime updates pushed by server)
  • Optimistic UI (UI that predicts how the server will respond to requests)
  • SSR support
  • Prefetching

That’s a ton of buzzwords! In the end, what that all really adds up to is our app will be data driven, really fast for users, and get real-time updates as they happen.

Part 1 Goals

Here’s what we are going to accomplish in this first tutorial:

  1. Set up our dev environment
  2. Start an Apollo Server
  3. Create our first GraphQL Schema
  4. Start a basic React Native client
  5. Connect our Apollo Server and RN client!

Getting started

For this tutorial series, we’re going to start from absolute scratch. My style is to keep everything really simple and refactor as we add complexity. Let’s start with this basic directory structure:

We will keep our React Native code separate from our server code. This will also keep server dependencies separate from React Native dependencies, which means we will have 2 package.json files. That may sound weird/bad, but trying to get everything set up with one packager is a huge hassle. It will also save us from a few other issues down the line.

Step 1.1: Create package.json and index.js

Here’s the terminal code to get us started:

Setting up the dev environment

We’ll start setting up our dev env with the following features:

  1. Server stays running and reloads when we modify code
  2. ES6 syntax including import syntax in our server code
  3. ESLint with AirBNB presets

My eslintrc.js file looks like this:

Step 1.2: Add eslint, babel, and nodemon

Added .eslintrc.js

Create our start script inside package.json:

Step 1.3: Create start script

Changed package.json

Starting the Express server

Let’s import apollo-server in index.js using ES6 syntax.

  1. npm i apollo-server graphql (apollo-server requires graphql)
  2. Add the following to index.js:

Step 1.4: Add ApolloServer

Changed package.json

Changed server/index.js

Quickly verify our setup works by running npm start: 🚀 Server ready at http://localhost:8080/

We have a great starting point. Our start script will transpile ES6 code, spin up our Apollo Server, and refresh as we make changes to server code. Nice!

But wait, there’s more! Apollo Server comes with some amazing features out of the gate, including GraphQL Playground. Head on over to http://localhost:8080/ and you should see a slick playground for us to test GraphQL queries against our server!

Type in {testString} and you should get back a response:

Great! So now we have a server that runs the most basic GraphQL. We could build up our GraphQL backend a bit more, but I’d prefer to connect our server and React Native client before we make our Schema any more complex.

Starting the React Native client

First we’ll download the dependencies and initialize our React Native app. For the sake of brevity, I’m going to focus on iOS, but all our code should also work with Android.

Running the initialization will create an index.js file. In this file is boilerplate code that creates a React component and registers it with AppRegistry, which renders the component.

Let’s pull out the Chatty component from index.js and stick it in its own file. I prefer to organize my files by type rather than feature, but you’re welcome to organize differently if you feel strongly about it.

So I’m going to place the Chatty component code into client/src/app.js and rename the component App.

Step 1.6: Move app code to /src

Changed client/index.js

Changed client/App.js

Adding Apollo to React Native

We’re going to modify app.js to use React-Apollo and Redux. While Apollo can be used sans Redux, the developer experience for React Native is much sweeter with Redux for monitoring our app's state, as you'll soon see.

We need to add a bunch of Apollo packages and a couple Redux ones:

We need to do the following:

  1. Create a Redux store
  2. Create an Apollo client
  3. Connect our Apollo client to our GraphQL endpoint via apollo-link-http
  4. Catch and log any GraphQL errors via apollo-link-error
  5. Connect Redux to our Apollo workflow via apollo-link-redux. This will let us track Apollo events as Redux actions!
  6. Set our Apollo client’s data store (cache) to Redux via apollo-cache-redux

We can also swap out compose for composeWithDevTools, which will let us observe our Redux state remotely via React Native Debugger.

Step 1.7: Add ApolloClient

Changed client/package.json

Changed client/src/app.js

Finally, we need to connect our app to Apollo and Redux. We’ll wrap our App component in the ApolloProvider component from react-apollo and the Provider component from redux.

Step 1.8: Add ApolloProvider to App

Changed client/src/app.js

If we reload the app (CMD + R), there hopefully should be no errors in the simulator. We can check if everything is hooked up properly by opening Redux Native Debugger and confirming the Redux store includes apollo:

That’s one small step for man…

Hello world!

You have successfully created a solid setup for a full-stack Apollo app with Express and React Native. In the next step, we are going to expand our GraphQL Schema, add Queries, and connect real data from a database.

As always, please share your thoughts, questions, struggles, and breakthroughs below!

You can view the code for this tutorial here

Continue to Building Chatty — Part 2 (GraphQL Queries with Express)

--

--