How to Create Resolvers in Apollo Server: Simple Guide
In Apollo Server, create resolvers by defining a
resolvers object that matches your GraphQL schema's types and fields. Each resolver is a function that fetches or computes the data for a specific field when a query runs.Syntax
A resolver in Apollo Server is an object where keys are type names and values are objects mapping field names to resolver functions. Each resolver function receives parameters like parent, args, context, and info to help fetch or compute data.
- Type name: Matches a type in your GraphQL schema.
- Field name: Matches a field in that type.
- Resolver function: Returns the data for that field.
javascript
const resolvers = { Query: { fieldName: (parent, args, context, info) => { // return data for this field } }, TypeName: { fieldName: (parent, args, context, info) => { // return data for this field } } };
Example
This example shows a simple Apollo Server setup with a Query type and a resolver that returns a greeting message. It demonstrates how to connect schema fields to resolver functions.
javascript
const { ApolloServer, gql } = require('apollo-server'); // Define schema const typeDefs = gql` type Query { hello: String } `; // Define resolvers const resolvers = { Query: { hello: () => 'Hello, world!' } }; // Create Apollo Server const server = new ApolloServer({ typeDefs, resolvers }); // Start server server.listen().then(({ url }) => { console.log(`Server ready at ${url}`); });
Output
Server ready at http://localhost:4000/
Common Pitfalls
Common mistakes when creating resolvers include:
- Not matching resolver keys exactly to schema type and field names.
- Forgetting to return data from resolver functions.
- Misusing resolver parameters like
argsorcontext. - Not handling asynchronous data fetching properly (use
asyncfunctions or return Promises).
Always ensure your resolver structure matches your schema and returns the expected data type.
javascript
const resolversWrong = { Query: { hello: () => { console.log('Hello called'); // Missing return statement - this will cause undefined result } } }; const resolversRight = { Query: { hello: () => 'Hello, world!' } };
Quick Reference
| Concept | Description |
|---|---|
| Resolver Object | Maps schema types to field resolver functions. |
| Resolver Function | Function that returns data for a field. |
| Parameters | parent, args, context, info provide data and context. |
| Async Support | Resolvers can be async and return Promises. |
| Matching Schema | Resolver keys must match schema type and field names exactly. |
Key Takeaways
Resolvers are functions that provide data for each field in your GraphQL schema.
Define a resolver object matching your schema's types and fields exactly.
Always return data from resolver functions; use async if fetching data asynchronously.
Use resolver parameters to access query arguments and context like authentication.
Test resolvers to avoid common mistakes like missing returns or mismatched keys.