Resolvers are a crucial part of GraphQL that determines how data is fetched and returned in response to a query. They act as the bridge between the client's request and the data source, whether it's a database, API, or any other data store. Resolvers are responsible for fetching the data for each field in a query and transforming it into the format expected by the client. In this article, we will learn about Resolvers along with an understanding of How Resolvers Work with examples and so on.
What are GraphQL Resolvers?
GraphQL resolvers are defined inside a GraphQL Schema and are responsible for resolving the fields that are represented in a given query. Resolvers have to fetch the data and transform it into the required format before sending it to the client. Resolvers can return scalar values like strings or numbers, as well as complex types like objects or arrays, depending on the schema definition. They can be synchronous or asynchronous, allowing for complex data-fetching operations. They can also interact with 3rd party APIs or external databases, depending on the use case and requirements of the product.
How to Resolvers Work?
- A client makes a GraphQL query request and sends it to the GraphQL server. Each field in the query corresponds to a separate resolver function defined in the GraphQL server.
- The resolvers are responsible for fetching the data, from the database or some other services, for all the fields that are requested in the query.
- Once the data is retrieved, the resolver sends back the response in the same format as requested by the client
- A resolver accepts 4 arguments:
- Parent: It represents the data that is returned by the parent's resolver field, if we have nested resolvers present inside the query.
- Arguments: These represent the additional arguments that is passed to the query by the user.
- Context: It represents a shared object that is present across the resolvers that get called during a single query operation.
- Info: It represents the data that is present during the query operation, and represents the state of the query, like the field or the path to the field for which the resolver is getting resolved.
Resolver Anatomy
Let's find out how a resolver looks when implemented.
Example
In the below example, we will create a resolver inside a Query object and we will name it `getUser`. The resolver takes in 4 arguments, as mentioned above. Inside the resolver, we write the logic to fetch the data from the database, and return that data in the required format. For the sake of this example, we will return dummy data to the client.
JavaScript
const resolvers = {
Query: {
getUser(parent, args, context, info) {
return {
"id": "1",
"name: "GFG:
}
}
}
};
Using Resolvers in Queries
In this step, we will create our own resolver for queries in a GraphQL API.
Step 1: Creating a Node.js Server
We will create a basic node.js server, and then later, we will create a Schema resolver.
First, run the below command to initialise an npm project
npm init -y
Now, install the dependencies for creating an Apollo Server using the below command -
npm i apollo-server
Now, create a new file called server.js, where we will write the logic of creating a GraphQL server, and we will also add GraphQL resolver in the same file. The final project structure should look like below

Step 2: Defining Schema Type
In this step, we will create a GraphQL Schema that will have a type Animal, with the properties of id, and name. We will also define a resolver inside it to get an animal by an ID.
JavaScript
// Define your GraphQL schema
const typeDefs = gql`
type Animal {
id: ID!
name: String!
}
type Query {
getAnimal(id: ID!): Animal
}
`;
Step 3: Creating a Resolver to Serve the Requests
In this step, we will create a GraphQL resolver to serve the requests that are hit to the GraphQL endpoint. We will create a resolver for the `getAnimal`, and return a dummy data for any ID passed in the resolver argument.
JavaScript
// Define your resolver functions
const resolvers = {
Query: {
getAnimal: (_, { id }) => {
return {
id: '1',
name: 'Lion'
}
},
},
};
Step 4: Creating and Starting the Apollo Server
In this step, we will create and start our Apollo Server on port 4000.
JavaScript
const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
console.log(`Server running at ${url}`);
});
Step 5: Integrating the Above Code Together in a Single File
We will put the whole code above into a single file, and name the file "server.js". The file would look like below -
Filename: server.js
JavaScript
const { ApolloServer, gql } = require('apollo-server');
// Define your GraphQL schema
const typeDefs = gql`
type Animal {
id: ID!
name: String!
}
type Query {
getAnimal(id: ID!): Animal
}
`;
// Define your resolver functions
const resolvers = {
Query: {
getAnimal: (_, { id }) => {
return {
id: '1',
name: 'Lion'
}
},
},
};
// Create an instance of ApolloServer with your type definitions and resolvers
const server = new ApolloServer({ typeDefs, resolvers });
// Start the server and listen for incoming requests
server.listen().then(({ url }) => {
console.log(`Server running at ${url}`);
});
Let's start our server by running the below command -
node server.js
You will see the output of the server running -
Server running at http://localhost:4000/
Now, open the graphQL playground here, and execute the below query to fetch the data on the basis of provided schema
query {
getAnimal(id: "1") {
id
name
}
}
Output:

Conclusion
Overall, GraphQL resolvers are essential components that bridge the gap between client queries and data sources. They fetch and transform data as per the query, enabling flexible and efficient data retrieval. Resolvers can interact with various data sources, making them versatile for different use cases. Understanding resolver anatomy and how they work is crucial for building robust GraphQL APIs.
Similar Reads
Variables in GraphQL
GraphQL is a query language for API (Application Programming Interface). It was developed by Facebook in 2012 after they released it publicly in 2015. Graphql Provides a more efficient, powerful, and flexible alternative to traditional REST APIs. In this article, we will learn about variables in Gra
5 min read
Types in GraphQL
GraphQL is a strongly typed query language used as a manipulative language for various APIs. It is also called a query language for APIs. GraphQL also helps to describe our data. GraphQL services can run in any language In this, Types are the fundamental concepts that define various data types prese
4 min read
Schema in GraphQL
GraphQL is a powerful open-source Query Language for APIs. It is most commonly known for its single endpoint query which allows the user to define a single endpoint to fetch all the information needed. Schemas in GraphQL are the blueprints for its APIs and define what the request-response structure
5 min read
GraphQL | Scalars
Scalars are content of the GraphQL Type System. GraphQL scalars are the value types that every field in a GraphQL document ultimately resolves to. It is primitive data types that store single value. There are five built-in scalars in GraphQL but you can define custom made scalars too. Built-in scala
4 min read
Operation Name in GraphQL
In GraphQL, the operation name is a label that we attach to requests sent to servers which acts as an indication of code complexity. This allows us to define queries, mutations, and subscriptions to improve code readability and organize in particular when it comes to large projects making it easier
6 min read
Mutations in GraphQL
GraphQL is a query language for REST APIs created by Facebook which is designed to provide more efficiency, flexibility, and superiority in data handling. While GraphQL queries are used to retrieve information, mutations are used to perform data operations on the server, similar to HTTP Commands lik
6 min read
Introspection in GraphQL
Introspection in GraphQL is a powerful feature that allows users to understand and query the structure of a GraphQL schema. It enables developers to dynamically explore the available object types, mutations, queries, and fields supported by the server. Introspection plays a crucial role in GraphQL d
5 min read
Serving GraphQL over HTTP
HTTP or Hypertext Transfer Protocol, is the foundation of Web communication. Being a stateless protocol, each request from a client to the server is independent and doesn't retain information about past requests. GraphQL is a query language for APIs and a runtime for executing those queries. Unlike
6 min read
GraphQL | Query
We will use GraphQL API for demonstration. GraphQL is an open-source data query and manipulation language for APIs and a runtime for fulfilling queries with existing data. It's neither an architectural pattern nor a web service. GraphQL was developed internally by Facebook in 2012 before being publi
4 min read
JQuery Integration in GraphQL
jQuery integration in GraphQL refers to the practice of using jQuery, a popular JavaScript library that interacts with GraphQL servers. This integration simplifies the process of sending and receiving data asynchronously, which is crucial for building dynamic web applications. In this article, We wi
5 min read