2. Creating our graph
10m

Overview

Everything starts with creating a in . This gives us a single source of truth for all of our , plus it serves as a dashboard for how our is running as a whole.

In this lesson, we will:

  • Create a new in Studio
  • Store our environment variables
  • Publish the we'll be working with

Create a graph in GraphOS

Check your plan: Part of this course covers the self-hosted GraphOS Router, which requires an Enterprise or GraphOS Trial plan. You can still follow along if your organization is on a different plan, but you won't be able to complete certain hands-on tasks. You can also test out this functionality by signing up for a free trial.

  1. Open a new browser window and go to GraphOS Studio.

  2. If you haven't created a in Studio before now, you'll be prompted to do so. Otherwise, we can create a new graph by clicking the + Create New Graph button in the upper right corner of the dashboard.

    studio.apollographql.com

    The New Graph button in Studio

  3. We'll give our a title—we're calling it Airlock, to match our app—keep the default settings for Graph Architecture as "", then click Next.

    studio.apollographql.com

    The create graph modal in Studio, filled out with the name Airlock

    If you don't see the modal above, you may be on the wrong plan.

    Check your plan: Part of this course covers the self-hosted GraphOS Router, which requires an Enterprise or GraphOS Trial plan. You can still follow along if your organization is on a different plan, but you won't be able to complete certain hands-on tasks. You can also test out this functionality by signing up for a free trial.

  4. We should now see a modal with options for publishing a schema.

    studio.apollographql.com

    A modal window showing options for publishing the schema for our graph

Task!

Connecting the router to the graph

To connect our to our new , we'll need two values:

  • APOLLO_KEY: Your 's API key, used to interact with a single in . It starts with something like service:your-graph-name.
  • APOLLO_GRAPH_REF: The erence (or graph ref) for our . A graph ref starts with the graph's ID, followed by an @ symbol, followed by the .

We'll use these values every time we run the , so we'll store them in our project's .env file.

Let's locate both values. Go back to the configuration options in Studio that appeared after you created your . Make sure you're on the Schema Document tab.

  1. First, make sure that the Supergraph Pipeline Track dropdown is set to Federation 2.7 Supergraph. This specifies that our should be built using the latest features of .

    studio.apollographql.com

    The schema publish modal, highlighting the Supergraph Pipeline Track dropdown

  2. Below, take a little peek at the command for publishing a . This shows how we can use to publish our subgraphs from the command line. We'll run this command momentarily, but for now we can focus on the APOLLO_KEY value included here.

    APOLLO_KEY=your-graphs-apollo-key \
    rover subgraph publish your-graph-name@current \
    --name products --schema ./products-schema.graphql \
    --routing-url http://products.prod.svc.cluster.local:4001/graphql
  3. Click on the eye icon on the code block to reveal the full value of APOLLO_KEY. Copy APOLLO_KEY and its value into your project's .env file. We'll need it for the next step, and won't have access to the same key again through Studio.

    studio.apollographql.com

    The Apollo Key value outlined, along with the eye icon that toggles the value from hidden to visible

    .env
    APOLLO_KEY=your-unique-apollo-api-key
  4. Now let's go back to Studio to get our . The value we're looking for appears in the same code block, directly after the rover subgraph publish part of the command. We'll save this value as an environment variable as well, but we can access it anytime from our 's home page.

    studio.apollographql.com

    The graph ref value outlined

    APOLLO_KEY=your-graphs-apollo-key \
    rover subgraph publish your-graph-name@current \
    --name products --schema ./products-schema.graphql \
    --routing-url http://products.prod.svc.cluster.local:4001/graphql

Great! Your .env file should now have both values for APOLLO_KEY and APOLLO_GRAPH_REF.

.env
APOLLO_KEY=your-unique-apollo-api-key
APOLLO_GRAPH_REF=your-graph-name@current

Publishing the subgraph schemas

has a command ready to help us with this important task: rover subgraph publish. This command pushes the latest version of a single to .

Usually, we would provide with the path to our file. We can also choose to publish the results of a schema that we introspect from a running . This means that we can pull the schemas from our two running services—listings and reviews—and publish each as components of our .

Here's what the command looks like. First, we call rover subgraph introspect to introspect the schema from the remote service's URL; then we pipe the results into rover subgraph publish.

The boilerplate for introspecting and publishing a remote schema
rover subgraph introspect \
<REMOTE ROUTING URL> | \
rover subgraph publish <APOLLO_GRAPH_REF> \
--name <SUBGRAPH NAME> \
--schema <SCHEMA FILE PATH> \
--routing-url <REMOTE ROUTING URL>

To use this command for both of our , we need to provide the for the we want to publish to and the following command line options:

OptionWhat is it?
--nameWhat we want to call our subgraph in GraphOS
--schemaWe can use the results of the previous command's introspection by giving this option a single dash (-)
--routing-urlThe URL where our subgraph runs (a remote address)

We'll also need the URL for each of our remote running on Heroku. We'll use the routing URL provided for each subgraph in the table below as part of our publish command.

Let's fill out this command for our listings . (Make sure to replace the <APOLLO_GRAPH_REF> value with your own!)

The command for introspecting and publishing the listings schema
rover subgraph introspect \
https://rt-airlock-subgraphs-listings.herokuapp.com/ | \
rover subgraph publish <APOLLO_GRAPH_REF> \
--schema - \
--name listings \
--routing-url https://rt-airlock-subgraphs-listings.herokuapp.com/

Note: We've used the \ character in this command to improve legibility by putting each command-line option on its own line. If you choose to type the entire rover subgraph publish command on a single line, you don't need to include the \.

If all is well in the world, running this command should output a message confirming that the has been published and the has been updated!

Let's do the same thing for the reviews , swapping in the corresponding values for the subgraph name, schema file, and routing URL.

The command for introspecting and publishing the reviews schema
rover subgraph introspect \
https://rt-airlock-subgraphs-reviews.herokuapp.com/ | \
rover subgraph publish <APOLLO_GRAPH_REF> \
--schema - \
--name reviews \
--routing-url https://rt-airlock-subgraphs-reviews.herokuapp.com/

Let's go back to Studio. We can click "See schema changes" in the modal to see what's changed in our .

studio.apollographql.com

The schema publish modal, now with the 'See schema changes' button highlighted

Task!

We've done everything we need to with our . Let's take care of configuring our next.

Router configuration

We'll start with the 's configuration. Open up router-config.yaml in your IDE or code editor, and let's paste the following contents into it.

router-config.yaml
supergraph:
listen: 127.0.0.1:5000
include_subgraph_errors:
all: true
telemetry:
instrumentation:
spans:
mode: spec_compliant

We've set a few options, including the port that the should run on (under the supergraph.listen property). We also configured a setting to include all the errors that bubble up. By default, the redacts the details of subgraph errors in its responses, and we might see an error message like "Subgraph errors redacted". In production environments, this property is usually set to false, but it'll be helpful if you run into any issues following along in this tutorial.

Lastly, we have some telemetry configuration which reduces the kinds of errors we'll see in our terminal output.

Note: To learn more about other options available for configuring the , check out the documentation.

Running the router

Next up: we'll tell the which to connect to, and how to authenticate to it.

For that, we'll use those two specific pieces of data we copied from the creation process: our APOLLO_KEY and APOLLO_GRAPH_REF values.

From a terminal in your project directory, run the command below, replacing the placeholder <APOLLO_KEY> and <APOLLO_GRAPH_REF> with your values from .env.

Terminal opened to the project directory
APOLLO_KEY=<APOLLO_KEY> APOLLO_GRAPH_REF=<APOLLO_GRAPH_REF> ./router --config router-config.yaml

Notice the --config flag we're including in our command? This tells the to use the settings we've included in router-config.yaml.

We'll see a few lines of output, and finally a message that our router is running on port 5000, ready to receive queries!

Let's copy this address.

http://127.0.0.1:5000/

This is the entrance to our : if this were a production URL, it's where anyone any part of our schema would send their requests to. For this course, we'll keep our running locally on port 5000—so let's add this detail to !

Connecting the router to GraphOS

Flipping back over to Studio, click on the README tab in the sidebar. Next, tap the Connection Settings link at the top of the page.

studio.apollographql.com

The graph README page, highlighting that the graph does not yet have an endpoint

We'll paste the address we copied (http://127.0.0.1:5000) as the endpoint, then save.

studio.apollographql.com

The connection settings modal, highlighting the input where we can paste our endpoint

Testing our schema

Let's put together a that retrieves the titles and descriptions of our featured listings. Select the Explorer tab from the sidebar and paste in the following .

query GetFeaturedListings {
featuredListings {
id
title
description
numOfBeds
}
}

Now let's run this , and... fantastic! We've got data!

Task!

Key takeaways

  • Creating a in Studio gives us a single source of truth for our and performance.
  • We can connect our to our in Studio by providing two , our APOLLO_GRAPH_REF and our APOLLO_KEY.
  • The can be configured by running the start command with the --config, which accepts a YAML file as its .
  • When connected to our in Studio, the acts as the single point of content to our clients. It receives requests, delegates each part to the responsible, and returns the data in a single response.

Up next

Our 's equipped to execute some queries! So let's take a closer look at what exactly it caches in the process.

Previous

Share your questions and comments about this lesson

This course is currently in

beta
. 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.