React Native with Apollo — Part 2, Apollo Client
Here, we will be using the Apollo GraphQL server we created in part 1 to retrieve data from our database and render it in our app.
If you have not already build the client from part one but want to start from here, download this repo and follow the instructions on how to start the server.
If you would like to view part one, check it out here.
We will be having a <TextInput />
that will be updating the GraphQL Query fetching a president and then rendering the results to the view.
To get this up and running, we will be using the apollo-client
and react-apollo
packages from Apollo, but before we get started there we first need to create a new react-native app:
react-native init apolloclient
Once this is installed, cd into the directory and install the following dependencies:
yarn add graphql-tag apollo-client react-apollo
graphql-tag
— A JavaScript template literal tag that parses GraphQL queries. This lets us build GraphQL queries usingtemplate literals
apollo-client
— This is a package that can be used in any JavaScript frontend where you want to use data from a GraphQL server.react-apollo
— Allows you to use your GraphQL server data in your React components. Provides a<Provider />
similar to that of redux as a way to connect to the server.
We will only be working in two files: index.ios.js (or index.android.js) and a new file called app.js In index.io.js, add the following code:
I will walk through the main points here:
- import both
ApolloClient
and a method calledcreateNetworkInterface
from theapollo-client
package - import
ApolloProvider
fromreact-apollo
- create
createNetworkInterface
is called, passing in the apollo server url for theuri
argument. This method also takes optionaltransportBatching
(boolean) andoptions
(object) arguments. Read more about this method here. - create a variable named
client
and create a new instance ofApolloClient
, passing in thenetworkInterface
we just created as the only argument. The api forApolloClient
can be found here. - Finally, we return the App component we will create next and wrap it in
<ApolloProvider />
passing in the client as a prop.
Next, in App.js:
Here are the main points referencing what is going on here:
- import both
graphql
fromreact-apollo
andgql
fromgraphql-tag
. Thegraphql
method will allow us to create a higher order component for providing Apollo data to a component.gql
will be used to create our query. - set an initial state of
name: "George Washington"
- create an
updateName
method to update the state - create a new query using
gql
that we will use when we create the higher order component - create a component named
President
that we will pass to the higher order component along with the query - create a component called
ViewWithData
using thegraphql
method.graphql
takes two arguments: a query and an optional configuration object. To learn more about this api check the documentation. - render the view with a title, a
<TextInput/>
that will callupdateName
when changed, and theViewWithData
component.
Now, when we update the TextInput
, the new name will be passed to our query and fetch the name from our graphql server! (here is a link to the dataset that was populated into the database).
This has been a very basic implementation meant to introduce you to the basics of the Apollo Stack.
To download the completed project, check out this repo.
ヾ(⌐■_■)ノ♪
My Name is Nader Dabit . I am a software consultant and trainer and the founder of React Native Training where we teach developers at top companies around the world how to quickly get up and running with React Native.
If you like React and React Native, checkout out our podcast — React Native Radio on Devchat.tv
Also, check out my book, React Native in Action now available from Manning Publications
If you enjoyed this article, please recommend and share it! Thanks for your time