Odyssey

Federation with TypeScript & Apollo Server

Course overview and setupFederation with Apollo GraphOSCreating a supergraphRunning the routerAdding a subgraphLocal development with rover devIntroducing entitiesContributing fields to an entitySchema checksPublishing to GraphOS
9. Schema checks
4m

Overview

We've seen our supergraph compose locally, and our changes are ready for the next stage. We could just publish our updated schemas to GraphOS (we made changes in both listings and reviews) , but let's take a pause and first validate that these changes won't break anything. We're responsible developers, after all!

In this lesson, we will:

  • Learn about the types of schema checks
  • Understand how schema checks fit into the overall process of updating a supergraph
  • Run a schema check locally using the Rover CLI
  • Inspect the results of a schema check in Studio

What are schema checks?

Schema checks are a set of predefined tests that help identify potential failures caused by schema updates. They check for issues like incompatibilities between subgraph schemas or breaking existing client operations. With schema checks, we can ensure that our schema changes won't cause issues when we deploy to production.

We'll talk about three types of schema checks: build checks, operation checks, and linter checks.

Build checks

Build checks validate that a subgraph's schema changes can still compose successfully with other subgraph schemas in the supergraph.

For example, if a new type is added to one subgraph, a build check determines whether that addition is compatible with the rest of the subgraphs in the supergraph. If it isn't, we need to investigate the error and fix it.

Illustration of subgraph characters being checked for composition

Operation checks

Operation checks validate that a schema's changes won't break any operations that existing clients send to the graph.

For example, let's say a web client regularly sends a GraphQL query to retrieve data for its homepage. If a schema change involves adding a required argument to a field in that query, it might break that client's existing operation if it doesn't include that argument! An operation check helps us guard against this potential failure, listing out the affected operations and allowing the team to address them.

Illustration of Studio checking a schema against existing operations

Linter checks

Linter checks analyze your proposed schema changes for violations of formatting rules and other GraphQL best practices. GraphOS provides a set of default rules you can configure to suit your team's conventions. You can see a full list of rules in the Apollo documentation.

Some common schema conventions include: writing field names in camelCase, type names in PascalCase, and enums in SCREAMING_SNAKE_CASE.

Running checks with Rover

We can run these checks right from the command line using Rover. Rover assesses our local changes for how they'll impact what we've already got in production. Its output will tell us everything we need to know—and what we need to fix!—before publishing our changes.

There are two main ways to run schema checks using the Rover CLI. We can run schema checks locally in our terminal (which we're about to do!). We can also integrate schema checks into our CI pipelines to run automatically against new pull requests.

In both scenarios, Rover needs to be able to connect with our production graph in GraphOS so it has a reference to compare our changes against. By embedding Rover in our deployment pipelines, we can ensure that we catch build and operation errors before they go to production.

Running schema checks: rover subgraph check

To perform a schema check for a subgraph, we use the rover subgraph check command with the following parameters:

rover subgraph check <APOLLO_GRAPH_REF> \
--schema <SCHEMA_FILE_PATH> \
--name <SUBGRAPH_NAME>

This command runs the schema checks then reports the results to Studio, so we can view them from our graph's Checks page.

Running checks on reviews

Let's start by checking our changes in the reviews subgraph.

Open up a new terminal in the reviews directory and paste in the rover subgraph check command. Make sure you replace the parameters with your own values.

reviews
rover subgraph check <APOLLO_GRAPH_REF> \
--schema ./src/schema.graphql \
--name reviews

After the process completes, we can see a report of the schema changes. The terminal output shows the following:

Checking the proposed schema for subgraph reviews against Airlock@current
error[E029]: Encountered 1 build error while trying to build subgraph "reviews"
into supergraph "Airlock@current".
Caused by:
INVALID_FIELD_SHARING: Non-shareable field "Listing.id" is resolved
from multiple subgraphs: it is resolved from subgraphs "listings" and "reviews"
and defined as non-shareable in subgraph "listings"
The changes in the schema you proposed for subgraph reviews are incompatible
with supergraph Airlock@current. See https://www.apollographql.com/docs/federation/errors/
for more information on resolving build errors.

So, what's this INVALID_FIELD_SHARING error? Well, the reviews subgraph declares Listing as an entity, but as far as our schema registry knows, the listings subgraph does not! And it was the subgraph that originally defined the Listing type, so this looks a bit problematic.

Let's take a closer look at this. We can check out the results of this schema check (and any past checks) in Studio as well. (Rover adds a link to this specific check at the end of its message.)

Head over to your supergraph in Studio and navigate to the Checks page. You'll see the same results reflected there.

studio.apollographql.com

The Checks page in Studio, showing a failed new entry

When we click into the check, we can see more detail about any problems that it may have found.

studio.apollographql.com

Further detail about the recently run check, showing that build was a failure

It looks like we're dealing with an order-of-operations problem. The schema registry doesn't know about the changes that we've made to the listings subgraph, so let's run the same check on our listings subgraph.

Running checks on listings

In your terminal, navigate to the listings directory, and run the following command. Be sure that you specify your own graph ref.

listings
rover subgraph check <APOLLO_GRAPH_REF> \
--schema ./src/schema.graphql \
--name listings

When we run this command, we should see the following output.

There were no changes detected in the composed API schema, but the core schema was modified.

Recall that there was only one thing that we changed in our listings schema: we turned the Listing type into an entity. Let's jump back into Studio and review the new entry on the Checks page.

studio.apollographql.com

The check entry that was run for the listings subgraphl

Click into this check and select Build from the TASK panel. Then, from under the WORKFLOW panel, click the button View schema.

studio.apollographql.com

Clicking into the check, and selecting the Build resultsl

This gives us two options: we can view the listings subgraph schema that we ran a check against, or we can view the entire supergraph schema that would be composed with the latest changes. Let's click on just the listings subgraph schema option for now.

studio.apollographql.com

Clicking into the check, and selecting the Build resultsl

This option shows us the full subgraph schema that we ran the check against. And we can see the @key directive and primary key field that we specified for the Listing entity here.

studio.apollographql.com

Clicking into the check, and selecting the Build resultsl

To take care of the error that appeared when we ran checks on the reviews subgraph, we just need to follow a specific order when publishing our subgraphs: first, we'll publish the changes to our listings subgraph that register Listing as an entity type. Then we can follow up by publishing our reviews subgraph changes, and our error will have vanished! Let's tackle this in our next and final lesson.

Practice

Types of schema checks
Schema checks identify potential failures from proposed schema changes. 
 
 checks validate that a schema follows GraphQL conventions. 
 
 checks validate that existing client operations won't break. 
 
 checks validate that the supergraph schema will still compose successfully.

Drag items from this box to the blanks above

  • Deprecated

  • Linter

  • Build

  • Subgraph

  • Field

  • Operation

To pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
Which of the following information does the rover subgraph check command need?

Key takeaways

  • Schema checks help identify potential failures caused by schema updates before they can cause issues in production.
  • Build checks validate that a subgraph's schema changes can still compose successfully with other subgraph schemas.
  • Operation checks validate that a schema's changes won't break any operations that existing clients are sending to the graph.
  • Linter checks validate that a schema follows formatting rules and conventions.
  • To run a schema check, we use the rover subgraph check command.
  • We can inspect the results of a schema check through the terminal or in the Studio Checks page.

Up next

Using GraphOS tools like Rover and Studio, we've validated that our schema changes are safe and don't break anything! 🎉 Next up, we'll publish our subgraph schemas.

Previous
Next

Share your questions and comments about this lesson

Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.

              supergraph

              A unified, federated graph composed of separate GraphQL APIs using Apollo Federation. Enables a microservices architecture that exposes a unified GraphQL API to clients.

              GraphOS

              A platform for building and managing a supergraph. It provides a management plane to test and ship changes and runtime capabilities to secure and monitor the graph.

              schema checks

              A GraphOS feature that ensures proposed subgraph schema changes successfully compose into a supergraph schema, don't break active clients, and follow best practices.

              schema checks

              A GraphOS feature that ensures proposed subgraph schema changes successfully compose into a supergraph schema, don't break active clients, and follow best practices.

              supergraph

              A unified, federated graph composed of separate GraphQL APIs using Apollo Federation. Enables a microservices architecture that exposes a unified GraphQL API to clients.

              Rover CLI

              Apollo's command-line interface for managing and maintaining graphs with GraphOS.

              subgraph schemas

              A schema for a subgraph server. A subgraph schema must be compliant with the GraphQL and Apollo Federation specs to be composed into a supergraph.

              operations

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              schema checks

              A GraphOS feature that ensures proposed subgraph schema changes successfully compose into a supergraph schema, don't break active clients, and follow best practices.

              schema checks

              A GraphOS feature that ensures proposed subgraph schema changes successfully compose into a supergraph schema, don't break active clients, and follow best practices.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              subgraph

              A service in a federated GraphQL architecture. Acts as a module for a supergraph. Includes both GraphQL services and REST services integrated via Apollo Connectors.

              subgraph schemas

              A schema for a subgraph server. A subgraph schema must be compliant with the GraphQL and Apollo Federation specs to be composed into a supergraph.

              supergraph

              A unified, federated graph composed of separate GraphQL APIs using Apollo Federation. Enables a microservices architecture that exposes a unified GraphQL API to clients.

              subgraph

              A service in a federated GraphQL architecture. Acts as a module for a supergraph. Includes both GraphQL services and REST services integrated via Apollo Connectors.

              supergraph

              A unified, federated graph composed of separate GraphQL APIs using Apollo Federation. Enables a microservices architecture that exposes a unified GraphQL API to clients.

              operations

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              graph

              A schema-based data model representing how different data elements interconnect and can be accessed.

              GraphQL

              An open-source query language and specification for APIs that enables clients to request specific data, promoting efficiency and flexibility in data retrieval.

              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              argument

              A key-value pair associated with a particular schema field that lets operations pass data to that field's resolver.

              Argument values can be hardcoded as literal values (shown below for clarity) or provided via GraphQL variables (recommended).

              query GetHuman {
              human(id: "200") {
              name
              height(unit: "meters")
              }
              }
              field

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              GraphQL

              An open-source query language and specification for APIs that enables clients to request specific data, promoting efficiency and flexibility in data retrieval.

              GraphOS

              A platform for building and managing a supergraph. It provides a management plane to test and ship changes and runtime capabilities to secure and monitor the graph.

              field

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              Rover

              Apollo's command-line interface for managing and maintaining graphs with GraphOS.

              schema checks

              A GraphOS feature that ensures proposed subgraph schema changes successfully compose into a supergraph schema, don't break active clients, and follow best practices.

              Rover CLI

              Apollo's command-line interface for managing and maintaining graphs with GraphOS.

              Rover

              Apollo's command-line interface for managing and maintaining graphs with GraphOS.

              graph

              A schema-based data model representing how different data elements interconnect and can be accessed.

              GraphOS

              A platform for building and managing a supergraph. It provides a management plane to test and ship changes and runtime capabilities to secure and monitor the graph.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              subgraph

              A service in a federated GraphQL architecture. Acts as a module for a supergraph. Includes both GraphQL services and REST services integrated via Apollo Connectors.

              schema checks

              A GraphOS feature that ensures proposed subgraph schema changes successfully compose into a supergraph schema, don't break active clients, and follow best practices.

              graph

              A schema-based data model representing how different data elements interconnect and can be accessed.

              subgraph

              A service in a federated GraphQL architecture. Acts as a module for a supergraph. Includes both GraphQL services and REST services integrated via Apollo Connectors.

              subgraph

              A service in a federated GraphQL architecture. Acts as a module for a supergraph. Includes both GraphQL services and REST services integrated via Apollo Connectors.

              subgraph

              A service in a federated GraphQL architecture. Acts as a module for a supergraph. Includes both GraphQL services and REST services integrated via Apollo Connectors.

              Rover

              Apollo's command-line interface for managing and maintaining graphs with GraphOS.

              supergraph

              A unified, federated graph composed of separate GraphQL APIs using Apollo Federation. Enables a microservices architecture that exposes a unified GraphQL API to clients.

              operations

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              subgraph

              A service in a federated GraphQL architecture. Acts as a module for a supergraph. Includes both GraphQL services and REST services integrated via Apollo Connectors.

              subgraph

              A service in a federated GraphQL architecture. Acts as a module for a supergraph. Includes both GraphQL services and REST services integrated via Apollo Connectors.

              graph ref

              A unique identifier for a particular variant of a particular graph in GraphOS. Made up of a graph's unique ID and variant name with the following format: graph-id@variant-name.

              entity

              An object type of Apollo Federation that can be fetched with one or more unique keys. Can resolve its fields from multiple data sources in a federated graph.

              subgraph schema

              A schema for a subgraph server. A subgraph schema must be compliant with the GraphQL and Apollo Federation specs to be composed into a supergraph.

              supergraph schema

              A special type of GraphQL schema that is created by declaratively combining one or more subgraph schemas using the Apollo Federation specification.

              subgraph schema

              A schema for a subgraph server. A subgraph schema must be compliant with the GraphQL and Apollo Federation specs to be composed into a supergraph.

              subgraph schema

              A schema for a subgraph server. A subgraph schema must be compliant with the GraphQL and Apollo Federation specs to be composed into a supergraph.

              directive

              A GraphQL annotation for a schema or operation that customizes request execution. Prefixed with @ and may include arguments. For example, the @lowerCase directive below can define logic to return the username field in lowercase:

              type User {
              username: String! @lowerCase
              }
              field

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              entity

              An object type of Apollo Federation that can be fetched with one or more unique keys. Can resolve its fields from multiple data sources in a federated graph.

              subgraph

              A service in a federated GraphQL architecture. Acts as a module for a supergraph. Includes both GraphQL services and REST services integrated via Apollo Connectors.

              subgraph

              A service in a federated GraphQL architecture. Acts as a module for a supergraph. Includes both GraphQL services and REST services integrated via Apollo Connectors.

              entity type

              An object type in a federated graph that's defined as an entity.

              subgraph

              A service in a federated GraphQL architecture. Acts as a module for a supergraph. Includes both GraphQL services and REST services integrated via Apollo Connectors.

              Schema checks

              A GraphOS feature that ensures proposed subgraph schema changes successfully compose into a supergraph schema, don't break active clients, and follow best practices.

              subgraph

              A service in a federated GraphQL architecture. Acts as a module for a supergraph. Includes both GraphQL services and REST services integrated via Apollo Connectors.

              subgraph schemas

              A schema for a subgraph server. A subgraph schema must be compliant with the GraphQL and Apollo Federation specs to be composed into a supergraph.

              Operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              graph

              A schema-based data model representing how different data elements interconnect and can be accessed.

              NEW COURSE ALERT

              Introducing Apollo Connectors

              Connectors are the new and easy way to get started with GraphQL, using existing REST APIs.

              Say goodbye to GraphQL servers and resolvers—now, everything happens in the schema!

              Take the course