liveBook · Manning

Resolver

As explained in Contract Testing in Action

A function that supplies the value for a GraphQL field or operation.

A resolver supplies the value for a GraphQL field or operation. It connects a query such as retrieving one movie with the data source that fetches the requested record. section 7.1

The GraphQL SDL defines the data structure, while resolvers define API behavior. A query requesting a field absent from the SDL fails before the resolver can provide it. section 7.1

A movie GraphQL server separates resolver responsibilities from data-source access:

  • Define Query fields such as movies and movie in the schema. section 7.2.1
  • Map each field to a data-source method, passing arguments such as movieId and returning the fetched result. section 7.2.1
  • Provide the data source through the GraphQL server context so resolvers can access the MovieAPI instance. section 7.2.1

Resolving one movie

const resolvers = { Query: { movie: (root, { movieId }, { dataSources }) => { return dataSources.movieApi.getMovieById(movieId); } } }; The resolver extracts movieId from the query arguments and delegates retrieval to movieApi rather than implementing the REST request itself. section 7.1 section 7.2.1

const resolvers = {
  Query: {
    movie: (root, { movieId }, { dataSources }) => {
      return dataSources.movieApi.getMovieById(movieId);
    }
  }
}

Shows the resolver signature and how a GraphQL query argument reaches the movie data source. section 7.1

How Contract Testing in Action covers it

How deep it goes
Explains schema separation, argument passing, data-source delegation, and server context, without detailing resolver execution internals.
Examples
Uses movie and movies queries backed by MovieAPI methods for retrieving one movie or a collection.
What it assumes
Uses GraphQL SDL fields, query arguments, REST data sources, and JavaScript function syntax.
What you can do after
Build resolvers that map GraphQL query arguments to Apollo data-source methods.
How it connects
Connects GraphQL schema definition language, GraphQL queries, data sources, REST APIs, Apollo Server, and contract testing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Contract Testing in Action. The section links point to the parts of the book it draws on.