liveBook · Manning

GraphQL server

As explained in Contract Testing in Action

A GraphQL server is a server application that exposes a GraphQL API and executes client queries against its schema and data sources.

A GraphQL server exposes a GraphQL API, defines its schema, resolves client queries, and connects those resolvers to one or more data sources. A practical service can separate a REST-backed MovieAPI from the main server entry point. section 7.2.1

A small movie service divides the server into these responsibilities:

  • The data source extends RESTDataSource and provides getMovies and getMovieById methods for the movies API. section 7.2.1
  • The SDL declares Movie fields and Query operations, including movies and movie(movieId: Int!). section 7.2.1
  • Resolvers map those operations to data-source methods, while server context makes MovieAPI available to each request. section 7.2.1

Starting a GraphQL movie service

Install dependencies and run the start:graphql:server script. The script starts both the movies API provider on port 3001 and the GraphQL server, which reports readiness at http://localhost:4000/. GraphQL Playground then provides an interactive browser interface for trying queries. section 7.2.2

Figure 7.3 GraphQL API at a glance with all the relevant files
figure

Use the architecture figure to trace the consumer request through the GraphQL server, resolvers, and underlying data source. section 7.2.1

The server can also act as a contract-testing boundary. A consumer contract may verify the GraphQL endpoint directly while the movies API runs underneath; Pact verification publishes the provider result. section 7.4

How Contract Testing in Action covers it

How deep it goes
Explains schema, resolver, context, startup, and contract verification, while deferring Apollo Server 3-to-4 migration details.
Examples
Uses a Movie type, movies and movie queries, a REST-backed MovieAPI, npm startup scripts, and Pact provider verification.
What it assumes
Assumes a movies API provider and npm project structure, while demonstrating schema, resolver, data-source, and startup wiring.
What you can do after
Build it from scratch as an Apollo Server 4 application backed by a REST data source and verify its consumer contract.
How it connects
Connects GraphQL schema, resolvers, data sources, REST APIs, Apollo Server, GraphQL Playground, 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.