You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 18, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+44-60Lines changed: 44 additions & 60 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,76 +2,65 @@
2
2
3
3
# Contentstack GraphQL with the Apollo Client React-Native
4
4
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.
5
6
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.
7
8
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).
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
16
14
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)
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).
26
23
27
-
To import the content types, perform the following steps:
24
+
## Step 2: Add a publishing environment
28
25
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).
38
27
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
40
31
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)
42
33
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.
48
35
49
-
## Step 5: Installing the Apollo framework
36
+
## Step 4: Add content
50
37
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.
52
39
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.
54
41
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:
Using Apollo Boost you can easily configure Apollo Client with the recommended settings in your app.
59
50
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.
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)
70
59
```
71
60
import { ApolloClient } from 'apollo-client';
72
61
import { HttpLink } from 'apollo-link-http';
73
62
import { InMemoryCache } from 'apollo-cache-inmemory';
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.
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.
99
89
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:
**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
102
98
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`.
104
100
105
-
Let’s create an FETCH_ALL_PRODUCT component in index.jsto 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!
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:
- 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
161
145
162
146
Now that we have a working project, you can build and run it.
0 commit comments