bitoverflow logobitoverflow logo
Navigate back to the homepage

How to Get Started With a Graph QL, React, Apollo Client, and Apollo Server App

Sachin Thakur
April 8th, 2020 · 6 min read

This is a two-part series. In part one we will learn what graphql is and what are some of its advantages and build a backend using graphql. In part two we will learn to integrate our graphql backed to our react frontend service using Apollo Client.

Graphql has been around for quite some time now and we often think that graphql is some complex thing but in reality, all graphql is a specification of how the data will be exchanged between the server and the client over the HTTP. It’s essentially a query language for your API’s and defined what data can be fetched from the server. Now, this is unlike anything you might have used in terms of a standard API, where you have a specific endpoint for fetching specific data. Like in the case of a medium API, we might have an API called /api/allarticles/:userId which returns us all articles for a specific user. Now this was of building API is known as REST API’s and we have been building APIs using this technique for quite some time now and before that, we had SOAP in which we use to have XML data structure. Now, what makes graphql different if how it improves upon the ideas of REST. In case of rest, where we hit a URL and get some data back in case of graphql we can specifically ask for what we are looking for and fetch only a specific subset whatever we want to build a specific page.

Getting Started

Now, after this small introduction let dive right into some demo. In this demo, we will be focusing on building a small react application using Apollo Client a graphql client library that is available for all major front end javascript framework and Apollo server for building our backend. Now all code for this tutorial will be available here. So, let’s get right into building a simple application.

Now, this demo will be focused on building a simple application to get started with Apollo client on the front end with ReactJs and Apollo server for building a lightweight graphQl backend. Let’s start by setting up a simple folder structure. Now for sake of simplicity in this starting guide, we will have both backend and frontend inside the same folder. So, let’s get started.

Basic Project Setup Commands

Basic Folder Structure

Now, after setting up the folder structure we will start by building our backend first and then move on to build a react frontend to showcase our data.

Building Backend Service with Apollo graphQl

Basic backend Setup commands

Folder Structure for our backend Service

Now, Since we are done with the initial Folder let’s start by writing some code and get started learning a few things about the apollo server. So let’s get right into our index.js file and initialize our server with basic minimal configuration.

1const { ApolloServer, gql } = require("apollo-server");
3const server = new ApolloServer({
4 typeDefs,
5 resolvers,
9 .listen()
10 .then(({ url }) => {
11 console.log(`Server ready at ${url}`);
12 })
13 .catch((err) => {
14 console.log(err);
15 });

Now, before we move any further let’s just analyze the 12 lines of code we have written so far and see what we are working with. Now most of the code is pretty straight forward except we see something called typeDefs and resolvers . So let’s first explore what exactly typeDefs and resolvers are.

Every graphQl server needs to define the data that can be accessed by the client and that can be done through a schema and these schemas are stored inside our typeDefs file. Now, this schema can have three root operation. These three operations are Query , Mutation and subscription . And all these have their specific purpose. Query are generally used for fetching the data which already exists in our database, Mutation are used to create or update any data and Subscription are used to listen to the events generated by our graphql server. Subscriptions depend on the use of a publish and subscribe primitive to generate the events that notify a subscription.

Now, since we are done with some basic introduction to Query , Mutation and Subscription . Similarly a resolver is essentially a function or a method that resolves some value for a field in the schema. They are the once which perform all the task to fetch data, create data, run some business logic to resolve the fields asked by the client. let’s get into some examples of how we can use them together to create our graphql server.

Now, let’s move forward with our example application. I personally prefer separating my resolvers and typeDefs so l et’s create our files for resolvers and typeDefs .

Creating typeDefs and resolvers file

After creating our files let’s look at our new folder structure and then we can start working with typeDefs because typeDefs are essentially like interfaces for our client based on which our client can ask for data from the server. So let’s start by creating our first typeDefs .

New Folder structure after adding typeDefs and resolvers file

Now, as I said earlier that typeDefs is the way for the client to connect to our backend service and ask for data. So let’s see how we can define.

1const { gql } = require("apollo-server");
3const typeDefs = gql`
4 type Query {
5 sayHello: String
6 }
9module.exports = typeDefs;

Now, in the above example, we have defined a simple Query which helps us fetch some data from backend and in our case it is sayHello and it returns a type of String as defined by the sayHello Query itself. Just make sure that you name your query so that they are self-declarative. Here our Query name clearly signifies what it’s going to do. Now as we have defined our typeDefs we also have to define our resolver function against this query which will actually resolve or compute some value and the way graphQl does that is by mapping each typeDefs name to each resolver function name. So here in our case, we have to define resolver with the same name. So let’s do that too.

1const resolvers = {
2 Query: {
3 sayHello: () => "hello random person",
4 },
7module.exports = resolvers;

Here we have defined our sayHello function inside our Query and it resolves to certain value here in our case hello random person . Just make sure the return type of your resolver function and typeDefs make otherwise your queries will result in returning null . Now since we have created both our typeDefs and resolvers files we just have to make a little change to our index.js file and we are good to go. We just have to import our resolvers and typeDefs file into our index.js file and make use of them.

1const { ApolloServer } = require("apollo-server");
2const typeDefs = require("./typeDefs");
3const resolvers = require("./resolvers");
4const server = new ApolloServer({
5 typeDefs,
6 resolvers,
10 .listen()
11 .then(({ url }) => {
12 console.log(`Server ready at ${url}`);
13 ``;
14 })
15 .catch((err) => {
16 console.log(err);
17 });

Now, since we are done with our introduction, let’s build a simple TODO list to get into doing CRUD operations using graphQl. Now, here we are not going to use some database, we will have a fake database inside our backend service in the form of a json object and we can manipulate that to perform our CRUD operations. So let’s create our fake JSON file.

Creating fake_data.js file

1const DAILY_TASKS = [
2 {
3 task: "Make Coffee",
4 completed: false,
5 id: 1,
6 },
7 {
8 task: "Learn GraphQl",
9 completed: false,
10 id: 2,
11 },
12 {
13 task: "Learn GoLang",
14 completed: false,
15 id: 3,
16 },
17 {
18 task: "Learn NodeJs",
19 completed: false,
20 id: 4,
21 },
22 {
23 task: "Learn GraphQl",
24 completed: false,
25 id: 5,
26 },
29module.exports = DAILY_TASKS;

Now, We are going to have 3 Mutation to update, create and delete data inside our fake JSON file and 1 query for interacting and fetching our data.

Now, let’s create our first Query to fetch the data from our backend service. Let’s call it fetchTasks .

1const { gql } = require("apollo-server");
3const typeDefs = gql`
4 type Tasks {
5 task: String
6 id: ID
7 completed: Boolean
8 }
9 type Query {
10 fetchTasks: Tasks
11 }
14module.exports = typeDefs;

Here we define our fetch task Query and it has a return type of Tasks . Now let’s write a resolver function for our newly added query.

1const DAILY_TASKS = require("./fake_data");
2const resolvers = {
3 Query: {
4 fetchTasks: () => DAILY_TASKS[0],
5 },
8module.exports = resolvers;

Here our query is going to return the first task always. Before updating this behavior let’s run our server first.

Running our server

Now let’s navigate to `http://localhost:4000/` .


Now, when we navigate to http://localhost:4000/ we are greeted with this GUI. This is known as graphql playground and we can run our queries here. Let’s run our first Query here.

Running our first Query

Now, after running our first query we see our results it fetches data from our backend which we have in our fake JSON file. Now, let’s add some logic to our functions and accept some data as a filter from our clients.

1const { gql } = require("apollo-server");
3const typeDefs = gql`
4 type Tasks {
5 task: String
6 id: ID
7 completed: Boolean
8 }
10 input fetchTaskFilter {
11 id: ID!
12 }
14 input addTaskInput {
15 name: String!
16 completed: Boolean!
17 }
19 input updateTaskInput {
20 id: ID!
21 name: String
22 completed: Boolean
23 }
25 type Query {
26 fetchTask(filter: fetchTaskFilter): Tasks
27 fetchTasks: [Tasks]
28 }
30 type Mutation {
31 addTask(input: addTaskInput): Tasks
32 updateTask(input: updateTaskInput): Tasks
33 }
36module.exports = typeDefs;

Now, in the above example, we have defined our mutation and queries to interact with our data. Now, one new thing we see is the !mark in front of our data types, now what this means is that this field is compulsory and we cannot execute our queries or mutation on the backend. Now let’s add some logic to our resolvers so we can interact with our data. Every resolver function inside our resolvers file receives 4 function arguments and in some form or another, almost all graphql server receives these 4 function arguments inside resolvers.

  • root — Result from the previous/parent type.
  • args — Arguments provided to the field by the client. For example, in our typeDefs we have addTask(input:addTaskInput) so the args, in this case, would be {input:{name:"some name",completed:false}} .
  • context — a Mutable object that is provided to all resolvers. This basically contains the authentication, authorization state, and anything else that should be taken into account when resolving the query. You get access to your request object so you can apply any middlewares and provide that info to your resolvers through context.
  • info — Field-specific information relevant to the query. This argument is only used in advanced cases, but it contains information about the execution state of the query, including the field name, the path to the field from the root, and more.

Here we will primarily focus on args to get access to our fields sent by our client or playground.

1const DAILY_TASKS = require("./fake_data");
2const resolvers = {
3 Query: {
4 fetchTask: (parent, args, context, info) => {
5 return DAILY_TASKS[];
6 },
7 fetchTasks: (parent, args, context, info) => {
8 return DAILY_TASKS;
9 },
10 },
11 Mutation: {
12 addTask: (parent, args, context, info) => {
13 const {
14 input: { name, completed },
15 } = args;
16 const nextId = DAILY_TASKS[DAILY_TASKS.length - 1].id + 1;
17 const newTask = {
18 task: name,
19 completed: completed,
20 id: nextId,
21 };
22 DAILY_TASKS.push(newTask);
23 return newTask;
24 },
25 updateTask: (parent, args, context, info) => {
26 const {
27 input: { id, name, completed },
28 } = args;
29 const updateTask = DAILY_TASKS.filter((task) => {
30 return == id;
31 });
32 if (name) {
33 updateTask[0].task = task;
34 }
35 if (completed) {
36 updateTask[0].completed = completed;
37 }
38 DAILY_TASKS.push(updateTask);
39 return updateTask[0];
40 },
41 },
44module.exports = resolvers;

Now, we have just added some simple logic to interact with our fake database. Now let’s see how we can interact through our playground.

GraphQl playground

Now, we see all our mutations and queries here. Now let’s run a few mutations and queries and see if it works.

Running addTask Query

We are done with the building of our server with minimal configurations. In part two of this blog post, we are going to use React and Apollo client to build our Frontend client and make use of the API’s we just build.

Join my email list and get notified about new content

Be the first to receive my latest content with the ability to opt-out at anytime. I promise to not spam your inbox or share your email with any third parties.

More articles from Sachin Thakur

Understanding JavaScript: Generators Functions

Functions that wait until they're ready to continue

March 29th, 2020 · 3 min read

Understanding JavaScript the weird parts: this context

The this keyword in JavaScript has confused a lot of developers. But if you understand Basic, not anymore.

March 22nd, 2020 · 4 min read
© 2018–2020 Sachin Thakur
Link to $ to $ to $