This code demonstrates how to connect to your MongoDB Realm app's GraphQL API from a React application using Apollo Client 3. For a detailed walkthrough, check out Use GraphQL with Apollo Client (React) in the MongoDB Realm documentation.
-
Clone the Repository
git clone https://github.com/mongodb-university/realm-graphql-apollo-react cd realm-graphql-apollo-react
-
Install the dependencies
Install the project dependencies with
npm
:npm install
-
Load the Sample Data
In MongoDB Atlas, load the sample datasets into your cluster.
-
Create a MongoDB Realm App
In the same project, create a new MongoDB Realm app. Make sure that the app is linked to the cluster that has the sample data.
-
Generate a Schema
Find the
sample_mflix.movies
collection on the Schema screen of the Realm UI and generate a schema based on the sample data. When you save the schema, Realm automatically generates corresponding GraphQL types. -
Define Rules
Find the
sample_mflix.movies
collection on the Rules screen of the Realm UI and set the default role to have full read and write permission. -
Enable Anonymous Authentication
This sample app automatically logs in as an anonymous user, so make sure that you enable the Anonymous provider.
-
Add Your App ID
Find your App ID and paste it into
src/index.js
. -
Run the App
npm run start