NodeJS GraphQL: Writing the GraphQL Query and Resolvers #3

In this post, we will learn how to write GraphQL Query resolvers for the queries



In this section, you are going to implement the first API operation that provides the functionality for a Star Wars API, We will add wars character Human.

We will learn
  • How to define a type in GraphQL
  • How to write and resolve the GraphQL queries
  • We will filter human character by ID
  • How to pass the arguments to the GraphQL query
  • We will write a query to get all human characters

So let's get started with writing the GraphQL queries.
. . .

Extending the schema definition

In general, when adding a new feature to the API, the process will look pretty similar every time:
  1. Extend the GraphQL schema definition with a new root field (and new data types, if needed)
  2. Implement corresponding resolver functions for the added fields
This process is also referred to as schema-driven or schema-first development. So, let’s go ahead and tackle the first step, extending the GraphQL schema definition.
. . .

A ) Get List of All Humans

Let’s start by implementing a humans query that allows you to retrieve a list of star wars Human elements. 1. Define Schema So, let’s go ahead and tackle the first step, extending the GraphQL schema definition by adding the Human type.

const typeDefs = gql`
// #1
type Person {
id: Int!
name: String!
gender: String!
height: Int!
mass: Int!
homeworld: String!
}

type Query {
// #2
allPeople: [Person]
}
`;

Let's walk through the comments:
  1. Pretty straightforward. You’re defining a new Person Type that represents a Human character of star wars. Each Person Type has an id, a name homePlanet planet, etc fields. All the fields are required.
  2. You’re then adding another root field allPeople to the Query type that allows you to retrieve a list of Human elements.
2. Implement resolver functions The next step is to implement the resolver function for the allPeople query. In fact, one thing we haven’t mentioned yet is that not only root fields, but virtually all fields on the types in a GraphQL schema have resolver functions. You’re adding a new resolver for the feed root field. Notice that a resolver always has to be named after the corresponding field from the schema definition.

// #1
const Persons = [
{
"name": "Luke Skywalker",
"gender": "male",
"height": "172",
"mass": "77",
"homeworld": "Tatooine",
"id": 1
},
... more persons
];

// #2
const resolvers = {
Query: {
// #3
async allPeople(parent, args) {
return Persons;
},
},
};
Let’s walk through the numbered comments again:
  1. const Persons contains the list of human characters
  2. We have defined the root resolver function which contains the resolver function for each query defined into the schema
  3. We have added allPeople resolver function which returns the list of Human characters
Go ahead and test the implementation by restarting the server (first use CTRL+C to stop the server if it is still running, then execute node index.js again) and navigate to http://localhost:8080 in your browser.

If you expand the documentation of the Playground, you’ll notice that another query called humans is now available:

Try it out by sending the following query to retrieve the list of humans:
{
allPeople {
id
name
mass
height
gender
homeworld
}
}
Awesome, the server responds with the data you defined in Human:
{
"data": {
"allPeople": [
{
"id": 1,
"name": "Luke Skywalker",
"mass": 77,
"height": 172,
"gender": "male",
"homeworld": "Tatooine"
},
{
"id": 2,
"name": "C-3PO",
"mass": 75,
"height": 167,
"gender": "n/a",
"homeworld": "Tatooine"
},
{
"id": 3,
"name": "R2-D2",
"mass": 32,
"height": 96,
"gender": "n/a",
"homeworld": "Naboo"
},
{
"id": 4,
"name": "Darth Vader",
"mass": 136,
"height": 202,
"gender": "male",
"homeworld": "Tatooine"
},
{
"id": 5,
"name": "Leia Organa",
"mass": 49,
"height": 150,
"gender": "female",
"homeworld": "Alderaan"
}
]
}
}
Feel free to play around with the query by removing any fields from the selection set and observe the responses sent by the server.
. . .

B ) Query: Get Human by ID

We will pass the human ID into the resolver function and it should return the appropriate Human, let's write some code
1. Define Schema
We will use the same schema previously defined for the Person

const typeDefs = gql`
// #1
type Person {
id: Int!
....
}

type Query {
// #2
person(id: Int!): Person
}
`;

Let's walk through the comments:
  1. Pretty straightforward. You’re defining a new Person Type that represents a Human character of star wars.
  2. You’re then adding another root field person to the Query type which accepts the ID as an argument and allows you to retrieve a Human character
2. Implement resolver functions The next step is to implement the resolver function for the human query. It will accept the human id as parameter

// #1
const Persons = [
{
"name": "Luke Skywalker",
"gender": "male",
"height": "172",
"mass": "77",
"homeworld": "Tatooine",
"id": 1
},
.... more persons
];

// #2
const resolvers = {
Query: {
#3
async person(parent, args ) {
#4
const id = args.id
return Persons.find(person => person.id === id)
},
},
};
Let's walk through the comments to understand
  1. Persons array contains the list of persons.
  2. We have defined the root query, which contains the resolver for each query.
  3. We have added the person query which returns the human.
  4. We can receive the arguments passed to the query, we are accepting the human id as arguments and it can not be empty. Go ahead and test the implementation, If you expand the documentation of the Playground, you’ll notice that another query called human is now available:

Try it out by sending the following query to retrieve the human character by id:
{
person(id: 1) {
id
name
mass
height
homeworld
gender
}
}
Awesome, the server responds with the data you defined in Human:
{
"data": {
"person": {
"id": 1,
"name": "Luke Skywalker",
"mass": 77,
"height": 172,
"homeworld": "Tatooine",
"gender": "male"
}
}
}

Feel free to play around with the query by removing any fields from the selection set and observe the responses sent by the server.
. . .
In the next tutorial, we will add the mutations that allow to create and update the human character


Never miss a post from Gufran Mirza, when you sign up for Ednsquare.