In GraphQL, the schema defines what data can be requested, while resolvers define how that data is fetched or processed. A resolver connects a field in the GraphQL schema to the underlying data source, such as a database, API, or custom logic.
Resolver
A resolver is a function that provides the value for a field defined in a GraphQL schema. When a client sends a query, GraphQL uses the corresponding resolver to fetch or calculate the requested data.
For Example, if a schema contains a "user" field, its resolver determines how the user data should be retrieved:
const resolvers = {
Query: {
user: () => {
return { id: 1, name: "John" };
}
}
};
How Resolvers Work
When a client sends a GraphQL query, the GraphQL server identifies the requested fields from the schema and calls the corresponding resolver functions. The resolver then fetches or calculates the required data and returns it to GraphQL.
The process can be understood as:
Client → Query → Schema → Resolver → Data Source → Response
- Client sends a query: The client requests specific fields.
- GraphQL checks the schema: It verifies that the requested fields are valid.
- Resolver is called: GraphQL executes the resolver associated with each requested field.
- Data is fetched: The resolver can retrieve data from a database, API, or other source.
- Response is returned: GraphQL uses the resolver results to construct the response according to the query.
For example, for this query:
query {
user {
id
name
}
}
The corresponding resolver might be:
const resolvers = {
Query: {
user: () => {
return { id: 1, name: "John" };
}
}
};
Here, GraphQL calls the "user" resolver, which returns the user data. GraphQL then sends only the requested "id" and "name" fields back to the client.
Resolver Function Structure
A resolver function generally receives four arguments that provide information about the current request and field:
fieldName: (parent, args, context, info) => {
// Resolver logic
}
- parent: Contains the result returned by the resolver of the parent field.
- args: Contains the arguments passed to the field in the query.
- context: Contains shared information available to resolvers, such as authentication details or database connections.
- info: Contains information about the current GraphQL operation and field being resolved.
Not every resolver needs to use all four arguments
Types of Resolvers
Resolvers can be used at different levels of a GraphQL schema depending on which field needs to provide data.
1. Root Resolvers
Root resolvers handle the top-level fields defined under Query, Mutation and Subscription.
const resolvers = {
Query: {
users: () => getUsers()
},
Mutation: {
createUser: (_, args) => createUser(args)
}
};
2. Field Resolvers
Field resolvers define how a specific field of an object type should be resolved.
const resolvers = {
User: {
posts: (parent) => getPostsByUser(parent.id)
}
};
3. Custom Resolvers
Custom resolvers contain application-specific logic for fields that cannot be resolved directly from the returned object or require additional processing.
const resolvers = {
User: {
fullName: (parent) => `${parent.firstName} ${parent.lastName}`
}
};
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 -yNow, install the dependencies for creating an Apollo Server using the below command -
npm i apollo-serverNow, 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.
// 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.
// 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.
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
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.jsYou 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:
