GraphQL – Resolver


GraphQL – Resolver


”;


Resolver is a collection of functions that generate response for a GraphQL query. In simple terms, a resolver acts as a GraphQL query handler. Every resolver function in a GraphQL schema accepts four positional arguments as given below −

fieldName:(root, args, context, info) => { result }

An example of resolver functions is shown below −

//resolver function  with no parameters and returning string
greeting:() => {
   return "hello from  TutorialsPoint !!!"
}

//resolver function with no parameters and returning list
students:() => db.students.list()

//resolver function with arguments and returning object
studentById:(root,args,context,info) => {
   return db.students.get(args.id);
}

Given below are the positional arguments and their description −

Sr.No. Arguments & Description
1

root

The object that contains the result returned from the resolver on the parent field.

2

args

An object with the arguments passed into the field in the query.

3

context

This is an object shared by all resolvers in a particular query.

4

info

It contains information about the execution state of the query, including the field name, path to the field from the root.

Resolver Result Format

Resolvers in GraphQL can return different types of values as given below −

Sr.No. Arguments and Description
1

null or undefined

this indicates the object could not be found

2

array

this is only valid if the schema indicates that the result of a field should be a list

3

promise

resolvers often do asynchronous actions like fetching from a database or backend API, so they can return promises

4

scalar or object

a resolver can also return other values

Illustration

Let us create a simple application to understand resolver. This will create schema for querying a student by id from the server. The student data will be stored in a flat file and we will use a node module called notarealdb to fake a database and read from flat file.

The following is a step-wise process to create a simple application −

Step 1 − Download and Install Required Dependencies for the Project

Create a folder named resolver-app. Change your directory to resolver-app from the terminal. Later, follow steps 3 to 5 in the Environment Setup chapter.

Step 2 − Create a Schema

Add schema.graphql file in the project folder resolver-app and add the following code −

type Query { 
   greeting:String
   students:[Student]
   studentById(id:ID!):Student 
}

type Student {
   id:ID!
   firstName:String
   lastName:String
   password:String
   collegeId:String
}

The schema file shows that user can query for greeting, students and studentById. To retrieve students with specific id, we use data type ID! which shows a non nullable unique identifier field. The students field returns an array of students, and greeting returns a simple string value.

Step 3 − Create Resolver

Create a file resolvers.js in the project folder and add the following code −

const db = require(''./db'')
const Query = {
   //resolver function for greeting
   greeting:() => {
      return "hello from  TutorialsPoint !!!"
   },
   
   //resolver function for students returns list
   students:() => db.students.list(),

   //resolver function for studentbyId
   studentById:(root,args,context,info) => {
      //args will contain parameter passed in query
      return db.students.get(args.id);
   }
}
module.exports = {Query}

Here, studentById takes in three parameters. As discussed in this chapter, the studentId can be retrieved from args; root will contain the Query object itself. To return a specific student, we need to call get method with id parameter in the students collection.

Here greeting, students, studentById are the resolvers that handle the query. students resolver function returns a list of students from the data access layer. To access resolver functions outside the module, Query object has to be exported using module.exports.

Step 4 − Run the Application

Create a server.js file. Refer step 8 in the Environment Setup Chapter. Execute the command npm start in the terminal. The server will be up and running on 9000 port. Here, we use GraphiQL as a client to test the application.

Open the browser and enter the url, http://localhost:9000/graphiql. Type the following query in the editor −

{  
   studentById(id:"S1001") {
      id
      firstName
      lastName
   }
}

The output for the above query is as shown below −

{
   "data": {
      "studentById": {
         "id": "S1001",
         "firstName": "Mohtashim",
         "lastName": "Mohammad"
      }
   }
}

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *