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
