Integrating with React Native
You can use Apollo Client with React Native exactly as you do with React.js. Install it with npm
like so:
npm install @apollo/client graphql
Then wrap your application in the ApolloProvider
component, like so:
import React from 'react';import { AppRegistry } from 'react-native';import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client';// Initialize Apollo Clientconst client = new ApolloClient({uri: 'http://localhost:4000/graphql',cache: new InMemoryCache()});const App = () => (<ApolloProvider client={client}><MyRootComponent /></ApolloProvider>);AppRegistry.registerComponent('MyApplication', () => App);
For more information on setting up Apollo Client, see
Example application
Apollo Client Devtools
1. Using React Native Debugger
The React Native Debugger supports the
- Install React Native Debugger and open it.
- Enable "Debug JS Remotely" in your app.
- If you don't see the Developer Tools panel or the Apollo tab is missing from it, toggle the Developer Tools by right-clicking anywhere and selecting Toggle Developer Tools.
2. Using Flipper
A community plugin called
-
Install Flipper and open it.
-
Go to add plugin and search for
react-native-apollo-devtools
and install it -
Add
react-native-flipper
andreact-native-apollo-devtools-client
as dev dependecy to react native app. -
Initialize the plugin with flipper on client side
import { apolloDevToolsInit } from 'react-native-apollo-devtools-client';const client = new ApolloClient({// ...});if (__DEV__) {apolloDevToolsInit(client);}
Consuming multipart HTTP via text streaming
By default, React Native ships with a fetch
implementation built on top of XHR that does not support text streaming.
For this reason, if you are using either @defer
- Install
react-native-fetch-api
andreact-native-polyfill-globals
and save them both as dependencies. - In your application's entrypoint (i.e.
index.js
,App.js
or similar), import the following three polyfills and call each of thepolyfill*
functions before any application code:
import { polyfill as polyfillEncoding } from "react-native-polyfill-globals/src/encoding";import { polyfill as polyfillReadableStream } from "react-native-polyfill-globals/src/readable-stream";import { polyfill as polyfillFetch } from "react-native-polyfill-globals/src/fetch";polyfillReadableStream();polyfillEncoding();polyfillFetch();
- Finally, there’s a special option we’ll need to pass to our polyfilled
fetch
. Create anHttpLink
so we can set the following on our defaultfetchOptions
:
const link = new HttpLink({uri: "http://localhost:4000/graphql",fetchOptions: {reactNative: { textStreaming: true },},});
Note: if you're still experiencing issues on Android after adding the polyfills above, there may be a library like Flipper that is intercepting requests during local development. Try commenting out NetworkFlipperPlugin
in e.g. android/app/src/debug/java/com/<projectname>/ReactNativeFlipper.java
, or running your app in release mode.
Now you're ready to use @defer
and/or multipart subscriptions over HTTP in your React Native app!
Troubleshooting
Uncaught Error: Cannot read property 'prototype' of undefined
, or similar Metro build error when importing from@apollo/client
This is due to the way .cjs
and .mjs
filesimport { ApolloClient, InMemoryCache } from '@apollo/client'
will result in an error. You can amend your import statement to e.g. import { ApolloClient, InMemoryCache } from '@apollo/client/main.cjs'
, or you can install @expo/metro-config
and configure their implicit resolution via metro.config.js
in the root of your project:
const { getDefaultConfig } = require('@expo/metro-config');const config = getDefaultConfig(__dirname);config.resolver.sourceExts.push('cjs');module.exports = config;