Skip to content
This repository was archived by the owner on Dec 18, 2025. It is now read-only.

Commit 5578f4a

Browse files
committed
2 parents 0887f96 + 343b10f commit 5578f4a

File tree

4 files changed

+45
-61
lines changed

4 files changed

+45
-61
lines changed

Android.png

1.7 MB
Loading

README.md

Lines changed: 44 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,65 @@
22

33
# Contentstack GraphQL with the Apollo Client React-Native
44

5+
Apollo Client provides the best way to use GraphQL to build client applications as you can use it to quickly build a UI that fetches data with GraphQL, and you can use any JavaScript front-end.
56

6-
## Step 1: Create a stack
7+
Using Apollo Boost you can easily configure Apollo Client with the recommended settings in your app. Apollo Boost includes packages that are essential for building an Apollo app.
78

8-
Log in to your Contentstack account and [create a new stack](https://www.contentstack.com/docs/guide/stack#create-a-new-stack). This stack will hold all the data that is specific to your website. Read more about [stacks](https://www.contentstack.com/docs/guide/stack).
9+
| ![](https://github.com/contentstack/contentstack-reactnative-graphql-example/raw/master/iOS.png) | ![](https://github.com/contentstack/contentstack-reactnative-graphql-example/raw/master/Android.png)|
10+
|--|--|
911

10-
## Step 2: Add a publishing environment
1112

12-
1. To [add an environment](https://www.contentstack.com/docs/guide/environments#add-an-environment) in Contentstack, navigate to Settings | Environment and click on the + New Environment tab.
13-
2. Provide a suitable name for your environment, say, ‘staging’.
14-
3. Specify the base URL (e.g., ‘http://YourDomainName.com’) and select the language (e.g., English - United States).
15-
4. Then, click on Save.
13+
## Prerequisite
1614

17-
Read more about [environments](https://www.contentstack.com/docs/guide/environments).
18-
15+
- Use [Xcode 10.1 and later](https://developer.apple.com/xcode/)
16+
- Use Latest version of Android Studio (for Android)
17+
- React Native [setup](https://facebook.github.io/react-native/docs/getting-started.html)
18+
- [Contentstack](https://www.contentstack.com/) account
1919

20-
## Step 3: Import content types
20+
## Step 1: Create a stack
2121

22-
A content type is like the structure or blueprint of a page or a section of your web or mobile property. Read more about [Content Types](https://www.contentstack.com/docs/guide/content-types).
23-
24-
For this application, basic content types is required: Product. For quick integration, we have already created these content types. You simply need to import them to your stack. (You can also [create your own content types](https://www.contentstack.com/docs/guide/content-types#creating-a-content-type). Learn how to do this).
25-
22+
Log in to your Contentstack account and [create a new stack](https://www.contentstack.com/docs/guide/stack#create-a-new-stack). Read more about [stacks](https://www.contentstack.com/docs/guide/stack).
2623

27-
To import the content types, perform the following steps:
24+
## Step 2: Add a publishing environment
2825

29-
1. Save the zipped folder of the JSON files given below on your local machine.
30-
2. Extract the files from the folder.
31-
3. Then, go to your stack in Contentstack. The next screen prompts you to either create a new content type or import one into your stack.
32-
4. Click the Import link and select the JSON file saved on your machine.
33-
34-
Here’s a brief overview of the content types required for this project.
35-
- **Product**: This content type lets you add the product content into your app.
36-
37-
Download All Content Types
26+
[Add a publishing environment](https://www.contentstack.com/docs/guide/environments#add-an-environment) to publish your content in Contentstack. Provide the necessary details as per your requirement. Read more about [environments](https://www.contentstack.com/docs/guidtens).
3827

39-
Now that all the content types are ready, let’s add some content for your news app.
28+
## Step 3: Import content types
29+
For this app, we need one content type: Product Here’s what it is needed for:
30+
- **Product**: Lets you add the product content into your app
4031

41-
## Step 4: Adding content
32+
For quick integration, we have already created the content type. [Download the content types](https://github.com/contentstack/contentstack-reactnative-graphql-example/raw/master/ContentTypes.zip) and [import](https://www.contentstack.com/docs/guide/content-types#importing-a-content-type) it to your stack. (If needed, you can [create your own content types](https:/cs/guide/content-types#rting-a-content-type). Read more about [Content Types](https://www.contentstack.com/docs/guide/content-types)
4233

43-
[Create](https://www.contentstack.com/docs/guide/content-management#add-a-new-entry) and [publish](https://www.contentstack.com/docs/guide/content-management#publish-an-entry) entries for the ‘Product’ content type.
44-
45-
Add a few dummy entries for products for the Product content type. Save and publish these entries. Learn how to create and publish entries.
46-
47-
With this step, you have created sample data for your application. Now, it’s time to use and configure the presentation layer.
34+
Now that all the content types arecredy, let’s add some content for your Product app.
4835

49-
## Step 5: Installing the Apollo framework
36+
## Step 4: Add content
5037

51-
In order to pull data from the new Contentstack GraphQL endpoint, you need to integrate a GraphQL client library that handles sending GraphQL queries and parsing the corresponding responses. The Apollo SDK for React-Native is one of the most popular choices for a GraphQL client.
38+
[Create](https://www.contentstack.com/docs/guide/content-management#add-a-new-entry) and [publish](https://www.contentstack.com/docs/guide/content-management#publish-an-entry) entries for thereti-content type.
5239

53-
let us create React-Native application and install the dependencies related to Apollo client
40+
Now that we have created the sample data, it’s time to use and configure the presentation layer.
5441

42+
## Step 5: Clone and configure application
43+
44+
To get your app up and running quickly, we have created a sample React Native app for this project. You need to download it and change the configuration. Download the app using the command given below:
5545
```
56-
$ react-native init ProductList
57-
$ npm install apollo-boost react-apollo graphql-tag graphql --save
46+
$ git clone https://github.com/contentstack/contentstack-reactnative-graphql-example.git
5847
```
48+
## Step 6: Install the Apollo Framework
49+
Using Apollo Boost you can easily configure Apollo Client with the recommended settings in your app.
5950

60-
## Step 6: Clone and configure application
61-
62-
To get your app up and running quickly, we have created a sample React Native app for this project. You need to download it and change the configuration. Download the app using the command given below:
51+
Let's create a React-Native application, and use Apollo Boost to include packages that are essential for building the Apollo app.
6352
```
64-
$ git clone https://github.com/raweng/NewsApp-iOS.git
53+
$ react-native init ProductList
54+
$ npm install apollo-boost react-apollo graphql-tag graphql --save
6555
```
6656
## Step 7: Create Apollo Client
6757

68-
Create a file called apollo.js and export a funtion that accepts a token and returns an instance of Apollo Client. You have to configure the Apollo client with the GraphQL endpoint and the token. (Replace with your own GraphQL endpoint)
69-
58+
Create a file named `apollo.js` and export a funtion that accepts a token and returns an instance of Apollo Client. You have to configure the Apollo client with the GraphQL endpoint and the token. (Replace with your own GraphQL endpoint)
7059
```
7160
import { ApolloClient } from 'apollo-client';
7261
import { HttpLink } from 'apollo-link-http';
7362
import { InMemoryCache } from 'apollo-cache-inmemory';
74-
const GRAPHQL_ENDPOINT = `https://graphql.contentstack.io/stacks/api_key/explore?access_token=environment_specific_delivery_token&environment=environment_name`;
63+
const GRAPHQL_ENDPOINT = `https://graphql.contentstack.io/stacks/api_key?access_token=environment-specific_delivery_token&environment=environment_name`;
7564
7665
const apolloClient = () => {
7766
const link = new HttpLink({
@@ -82,7 +71,7 @@ export default apolloClient;
8271
```
8372
## Step 8: Connect your client to React
8473

85-
To connect Apollo Client to React, you will need to use the ApolloProvider component exported from react-apollo. The ApolloProvider is similar to React’s context provider. It wraps your React app and places the client on the context, which allows you to access it from anywhere in your component tree.
74+
To connect Apollo Client to React, you need to use the `ApolloProvider` component exported from `react-apollo`. The `ApolloProvider` component wraps your React app and places the client on the context, which allows you to access it from anywhere in your component tree.
8675
```
8776
import {ApolloProvider} from 'react-apollo'
8877
export default class App extends Component<{}> {
@@ -96,13 +85,20 @@ export default class App extends Component<{}> {
9685
}
9786
```
9887
## Step 9: Write GraphQL queries
88+
Contentstack provides a GraphQL playground, which is a GraphiQL interface, to test your GraphQL queries in your browser. Use this interface to write and test your queries.
9989

100-
Once your ApolloProvider is hooked up, you’re ready to start requesting data with Query components! Query is a React component exported from react-apollo that uses the render prop pattern to share GraphQL data with your UI.
90+
Open a browser of your choice and hit the URL given below:
91+
```
92+
https://graphql.contentstack.io/stacks/api_key/explore?access_token=environment-specific_delivery_token&environment=environment_name
93+
```
10194

95+
**Note**: If you have pieces of data you may want to reuse in multiple places, make use of fragments. Refer the [Using fragments](https://www.apollographql.com/docs/ios/fragments.html) doc for more details.
96+
97+
## Step 10: Fetch data using Apollo Client
10298

103-
First, pass your GraphQL query wrapped in the gql function to the query prop on the Query component. Then, you’ll provide a function to the Query component’s children prop to determine what to render, which Query will call with an object containing loading, error, and data properties. Apollo Client tracks error and loading state for you, which will be reflected in the loading and error properties. Once the result of your query comes back, it will be attached to the data property.
99+
Once you have set up `ApolloProvider` while connecting your client to React, you can start requesting data using `Query`, which is a React component exported from `react-apollo`.
104100

105-
Let’s create an FETCH_ALL_PRODUCT component in index.js to see the Query component in action!
101+
Create the `FETCH_ALL_PRODUCT` component in `index.js`, add the following code snippet, and run it to see the `Query` component in action!
106102
```
107103
import {Query} from 'react-apollo';
108104
import gql from 'graphql-tag';
@@ -145,18 +141,6 @@ export default class Products extends React.Component {
145141
}
146142
}
147143
```
148-
149-
Contentstack provides a GraphQL playground, which is a GraphiQL interface, to test your GraphQL queries in your browser. Use this interface to write and test your queries.
150-
151-
Open a browser of your choice and hit the URL given below:
152-
```
153-
https://graphql.contentstack.io/stacks/api_key/explore?access_token=environment-specific_delivery_token&environment=environment_name
154-
```
155-
156-
Note:
157-
158-
- If you have pieces of data you may want to reuse in multiple places, make use of fragments. Refer the [Using fragments](https://www.apollographql.com/docs/ios/fragments.html) doc for more details.
159-
160-
## Step 10: Build and run your application
144+
## Step 11: Build and run your application
161145

162146
Now that we have a working project, you can build and run it.

apollo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ const apolloClient = () => {
1313
cache: new InMemoryCache()
1414
})
1515
}
16-
export default apolloClient;
16+
export default apolloClient;

iOS.png

1.75 MB
Loading

0 commit comments

Comments
 (0)