Back to all terms
ClientAPIrequestresponse
APIintermediate

GraphQL Resolvers

Functions that populate each field in a GraphQL schema by fetching data from databases, services, or other sources.

Also known as: Resolver Functions, GraphQL Field Resolvers

Description

Resolvers are the runtime implementation behind a GraphQL schema. Each field in the schema can have a resolver function that receives four arguments: the parent object, the field arguments, the context (shared per-request state like auth info and data sources), and the info object (AST details about the query). When a client sends a query, the GraphQL engine walks the schema tree and calls resolvers to populate each requested field.

Resolver design has significant performance implications. Naive implementations that make a database call per field lead to the N+1 query problem (addressed by DataLoader). Best practices include keeping resolvers thin -- they should delegate to a service or data-access layer rather than containing business logic directly. The context object is typically used to pass authenticated user information, database connections, and DataLoader instances so they're available throughout the resolver chain.

For mutations, resolvers handle input validation, execute the business operation, and return the modified object so the client can query updated fields in the same round trip. Error handling in resolvers should use structured GraphQL errors with extensions (error codes, field paths) rather than throwing generic exceptions, enabling clients to programmatically handle specific failure cases.

Prompt Snippet

Keep resolvers as thin orchestration layers that delegate to injected service classes via context (e.g., context.services.invoiceService.getById(id)). Pass DataLoader instances through context to batch and cache field-level data fetching within a single request. Return structured GraphQL errors using GraphQLError with extensions: { code: 'INVOICE_NOT_FOUND', field: 'invoiceId' } for client-consumable error handling. Use resolver middleware (e.g., graphql-shield) for authorization checks rather than inlining permission logic in each resolver.

Tags

graphqlresolversdata-fetchingarchitecture