Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

GraphQL · Const

resolver

A resolver is the server function behind a GraphQL field. GraphQL invokes it during execution, passes field context and inputs, and uses its result to resolve nested selections or produce a leaf value.

How it works

A resolver is the server-side function associated with a GraphQL field. When execution reaches that field, GraphQL calls the function, passes the parent value, field arguments, shared context, and resolver metadata, then uses the returned value; nested object results continue execution until scalar or enum leaves.

Syntax

resolver(obj, args, context, info)

Parameters

NameTypeDescription
objanyThe value resolved by the parent field; for a root Query field it is often unused.
argsobjectArguments supplied to the field in the GraphQL operation.
contextanyA value shared with every resolver, potentially containing the authenticated user or database access.
infoobjectInformation about the field and execution state.

Returns

The value for the field, which may be a scalar, enum, object, list, or asynchronous result that GraphQL completes according to the schema.

Key points

  • Every field in the schema has a corresponding server-side function that supplies its value.
  • The executor starts with the root operation selected by the client.
  • When a field is reached, GraphQL invokes its resolver and uses the returned value for continued execution.
  • GraphQL uses the declared field type to coerce resolver output into the API's expected representation.
  • The response preserves the query's requested field names, including aliases, when storing resolved values.
  • Context is shared with every resolver and can hold request-specific information such as authentication or database access.

Common mistakes

  • A resolver for an object field does not finish execution by itself; the client must select fields until scalar or enum leaf values are reached.
  • Resolvers can return asynchronously, so the execution layer must support asynchronous data retrieval.

Example: Resolvers for query and object fields

const resolvers = {
  Query: {
    me: (_parent, _args, context) => context.request.auth.user,
  },
  User: {
    name: (user, _args, context) => context.db.getUserFullName(user.id),
  },
};

Each resolver returns the value for its corresponding field; nested fields can use the parent object to obtain related data.

When to use it

Resolve top-level query fields

A root query needs request-specific data.

Resolve related fields

A nested object field depends on the parent object's identifier.

Adapt external data sources

A field reads from a database or remote service.

Resolve live updates

A subscription field must obtain event-backed data.

Interview questions

QHow does GraphQL execute resolvers?

GraphQL calls the resolver associated with each requested field. Execution starts at the root operation fields, and object results trigger execution of the selected child fields until leaf values are produced.

Follow-up: What happens when a resolver returns an object? GraphQL uses the selection set for that object and resolves each requested child field recursively.

QWhat arguments does a resolver receive?

In the reference implementation, the resolver receives the previous object, field arguments, shared context, and an information object. The parent value is commonly unused for root Query fields but is useful for nested fields.

Follow-up: Why is context useful? It can carry request-scoped data such as the authenticated user or a database connection.

QHow do resolvers relate to data sources and schema types?

Resolvers are independent of a particular storage technology. They can retrieve data from databases, services, or other sources, and GraphQL assembles their returned values into the response shape requested by the client.

Follow-up: What if a resolver returns a scalar of the wrong shape? GraphQL applies scalar coercion according to the schema's type contract, or reports an execution error when the value cannot satisfy it.

  • Query
  • Mutation
  • Subscription
  • scalar coercion
  • field execution
  • context
  • selection set

Source: Execution - GraphQL. Checked 2026-10-15.