Daniel Doubrovkine bio photo

Daniel Doubrovkine

aka dB., @awscloud, former CTO @artsy, +@vestris, NYC

Email Twitter LinkedIn Github Strava
Creative Commons License

In the previous post I added a React Native ticking timer. I now have enough client-side parts and it’s time to wire up the client app to a server. This cookbook should be helpful to anyone doing it for the first time.

Rails Server


Make a Rails API server with MongoDB.

  1. Create a server with rails new server --skip-active-record -T --api, 33-minutes-server@74fd5a.
  2. Generate a MongoDB config with rails g mongoid:config, 33-minutes-server@3e1c6f.
  3. Add RuboCop, my workflow typically consists of rubocop -a ; rubocop --auto-gen-config, 33-minutes-server@fc034c.
  4. Add RSpec, 33-minutes-server@3af9b9.
  5. Enable Travis-CI, 33-minutes-server@8050ab.

GraphQL Controller

Follow my tutorial on exposing a GraphQL API and use warden to keep authenticated user context. Since Warden requires cookies, re-add the ActionDispatch::Cookies middleware to config/application.rb.

config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore, key: '_namespace_key'

A GraphQL mutation creates a user.

Mutations::CreateUserMutation = GraphQL::Relay::Mutation.define do
  name 'createUser'
  input_field :email, !types.String
  input_field :password, !types.String
  input_field :name, !types.String
  return_field :user, Types::UserType
  resolve ->(_object, inputs, ctx) {
    user = User.create!(
      email: inputs[:email],
      name: inputs[:name],
      password: inputs[:password]
  { user: user }

Similar GraphQL mutations login and logout users.

When serializing users with Warden, don’t serialize the whole object or you’ll get a ActionDispatch::Cookies::CookieOverflow exception.

Rails.application.config.middleware.insert_after Rack::ETag, Warden::Manager do |manager|
  manager.failure_app = GraphqlController
  Warden::Manager.serialize_into_session do |user|
  Warden::Manager.serialize_from_session do |id|

You can see the entire code in 33-minutes-server@52a249 and 33-minutes-server@0c3c4d.

Schema IDL

Run rake graphql:schema:idl to generate a schema.graphql file and copy it to the client app’s schema folder. This will have to be done every time the Rails API GraphQL schema changes.

React Native Client

Signed In vs. Signed Out

I used a switch navigator to toggle between the signed-in and signed-out states in 33-minutes-app@9f252c. Each state is its own stack navigator.

const SignedOut = createStackNavigator({
  SignIn: {
    screen: SignIn
  SignUp: {
    screen: SignUp

const SignedIn = createStackNavigator({
  Main: {
    screen: Tabs
  Settings: {
    screen: Settings

export const createRootNavigator = (signedIn = false) => {
  return createSwitchNavigator(
      SignedIn: {
        screen: SignedIn
      SignedOut: {
        screen: SignedOut
      initialRouteName: signedIn ? 'SignedIn' : 'SignedOut'

The entire app is that navigator.

export default class App extends Component {
  render() {
    const Layout = createRootNavigator();
    return (
      <Layout />

To toggle between the states, navigate to either SignedIn or SignedOut (see the mutations below).

Relay Boilerplate

The Relay Quick Start Guide is pretty good. We throw in react-relay with yarn add react-relay and relay-compiler with yarn add -dev relay-compiler. Add a relay script into package.json.

"scripts": {
  "relay": "relay-compiler --src ./ --schema ./schema/schema.graphql --extensions=js --extensions=jsx"

Run yarn relay. Generally you will be running yarn relay --watch, which generates JavaScript code under a __generated__ folders every time something changes. You will also have to restart this process when schema changes. This is not integrated with the rest of the compiler toolchain because that would be too easy.


Copy-paste a relay environment into app/Environment.js. Note my hard-coded https://localhost:3000/graphql for the Rails server. I will make this dynamic in the future.

import { Environment, Network, RecordSource, Store } from 'relay-runtime';

function fetchQuery(operation, variables) {
  return fetch('https://localhost:3000/graphql', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    body: JSON.stringify({
      query: operation.text,
  }).then(response => {
    return response.json();

const environment = new Environment({
  network: Network.create(fetchQuery),
  store: new Store(new RecordSource()),

export default environment;

Create User Mutation

Copy-paste a relay-style mutation into app/mutations/CreateUserMutation.js.

import { graphql } from 'react-relay'
import commitMutation from 'relay-commit-mutation-promise'

const mutation = graphql`
  mutation CreateUserMutation($input: createUserInput!) {
    createUser(input: $input) {
      user {

function commit({ environment, input }) {
  const variables = { input }
  return commitMutation(environment, {

export default {

Create a User

Invoke the mutation, switch to a signed-in navigator upon success, or set an error message otherwise.

  input: {
    name: this.state.name,
    email: this.state.email,
    password: this.state.password
}).then(response => {
}).catch(error => {
  this.setState({ message: error.message });

Login is very similar. For now we’re using cookies to store a session and don’t remember anything on the client, which means that reloading the app logs the user out.

Show Some Data

To show data wrap up a GraphQL query into a QueryRenderer. You’ll always need one.

render() {
  return (
        query UserQuery {
         user {

      render={({error, props, retry}) => {
        if (error) {
          // todo

        if (! props) {
          return <Text>Loading ...</Text>

        // props.user ...


See 33-minutes-app@e3e6b9 for complete code. In the next post I’ll enable Relay-style pagination.