In this lesson, we're going to tackle the first step for migrating our monolith app into a supergraph. We'll start by converting the monolith server into a single subgraph, which will run on a different port. Along the way, we'll also:
Review how to create a subgraph using Apollo Server
Publish our first subgraph to GraphOS
✅ Service check
Let's make sure all of our processes are still running from the previous lesson.
One terminal should be running npm start, to start the monolith server on port 4000.
Another terminal should be running npm run launch, to start our services in the monolith directory on ports 4010 and 4011.
✏️ Importing packages
Let's stop the main monolith server process with CTRL+C.
Then, install the @apollo/subgraph package.
monolith
npminstall @apollo/subgraph
This directory contains an index.js file, which is running our monolith server. Let's adjust it slightly to turn it into a monolith subgraph server instead.
To make this server a subgraph, we'll need to import the buildSubgraphSchema method from the @apollo/subgraph package. Add the following line to the imports in your index.js file:
Next, let's modify our ApolloServer instance. Right now, we pass our typeDefs and resolvers directly to the ApolloServer constructor.
monolith/index.js
const server =newApolloServer({
typeDefs,
resolvers,
});
To convert this server into a subgraph, let's define a schema property in the constructor instead. We'll use the buildSubgraphSchema method, as we did in the first course, passing it an array containing a single object with the typeDefs and resolvers.
While we're here, let's also set our server to listen on port 4001 (a different port from where it was originally running!). We want the monolithsubgraph to use a different port because, as we outlined in our migration plan, we'll set up the GraphOS Router to take over port 4000. This way, the client doesn't need to make any changes to communicate with the GraphQL server.
Set up the server's listen function to run on port 4001.
monolith/index.js
- const port = 4000;
+ const port = 4001;
✏️ Federation 2 subgraph schema definition
We'll want to make sure to opt in to the latest Federation 2 features for this subgraph. Open up the schema.graphql file in the same monolith directory and add the following snippet at the top:
With this line, we can specify the various directives we'd like to use within our schema file (such as @key, as shown in the import).
Note:GraphOS composes our supergraph using the Federation version configured on the graph (its build pipeline), and that version must be at least as new as the one in our subgraph's @link. Graphs created with the Supergraph architecture use a recent version by default, so we're all set. If we ever see an UNKNOWN_FEDERATION_LINK_VERSION error, see the troubleshooting section below.
With that, we're ready to restart our monolith server process!
monolith
npm start
We'll see some output in our console and then the message "🚀 Server ready at http://localhost:4001/".
Testing the monolith subgraph
Let's visit that URL http://localhost:4001 in the browser to open up Apollo Sandbox again. Let's try out the same query from the previous lesson to make sure everything is still working as before!
queryGetAllAmenities{
listingAmenities{
category
name
}
}
After running the query, we can see data showing up in the Response panel on the right. Great, we know it's working! By transforming the monolith schema into a subgraph, we know that all of our schema's types, fields, and features are still preserved and intact.
We've got our first subgraph ready, so the next step is to publish it to the registry!
Publishing our subgraph
Recall that the rover subgraph publish command looks like this:
rover subgraph publish <APOLLO_GRAPH_REF>\
--schema <SCHEMA_FILE_PATH>\
--name <SUBGRAPH_NAME>\
--routing-url <SERVER_URL>
Let's publish this subgraph!
Tip: We're going to be using a few different Rover commands as we go through the course. You can create a new file to store these commands and copy-paste them to the terminal when you need them, or you can incorporate them into scripts to run in the package.json file.
Open up a new terminal window in the root directory and run the command below, substituting the values of the APOLLO_GRAPH_REF flag to match your own. We're going to name this subgraphmonolith.
rover subgraph publish <APOLLO_GRAPH_REF>\
--schema ./monolith/schema.graphql \
--name monolith \
--routing-url http://localhost:4001
We'll get a message asking if we want to publish with a localhost URL.
The host`localhost` is not routable via the public internet.
Continuing the publish will make this subgraph reachable inlocal environments only.
Would you still like to publish? [y/N]
We're in tutorial land, so we're working with local endpoints for now. Type y and hit enter.
When the command finishes, we'll see a message that our subgraph was published!
Because we created our graph with a supergraph architecture, it's ready to accept subgraphs right away.
Task!
If there's an error in the terminal, have no fear! These troubleshooting tips cover a few of the most common scenarios we might encounter when publishing our subgraph.
Failed authentication
Rover might indicate that publishing failed due to malformed or invalid credentials.
The error message:
error[E014]: The API key you provided is malformed.
An API key must have three parts separated by a colon.
How to fix it:
When an APOLLO_KEY environment variable is set, Rover uses that key instead of your login. Double-check that it matches the APOLLO_KEY provided for your graph. If necessary, generate a new key for your graph by visiting the Settings tab in GraphOS Studio. If you're not using APOLLO_KEY, log in again with rover auth login.
You can also check which credentials Rover is using by running the command rover auth whoami. An invalid API key produces the error below:
error[E013]: The registry did not recognize the provided API key.
Check your API key to make sure it's valid (are you using the right profile?).
For more information about configuring Rover to work with graph keys, see the docs on configuring Rover.
Invalid path
Rover might indicate that it can't find a schema file at the specified location.
The error message:
error: Invalid path. No file found at ./monolith/schema.graphql
How to fix it: Make sure to run the rover subgraph publish command from the root directory of your project. The path to the subgraph schema file should be relative from that directory.
Syntax error
Rover might indicate an error parsing the schema if one of the subgraph files contains invalid syntax.
The error message:
error: Could not parse partial schema as AST.
How to fix it: Check the schema and server files for errors, or revisit Lessons 2 and 3 to validate your code.
Unknown federation version
The error message:
UNKNOWN_FEDERATION_LINK_VERSION: [monolith] Invalid version v2.15 for the federation feature in @link directive on schema
How to fix it:
Your graph's build pipeline is using an older version of Apollo Federation than the @link in monolith/schema.graphql. Graphs created with the Supergraph architecture use a recent version by default, so this usually means the graph was created with a different architecture or was set up earlier. To upgrade it, open your variant's Settings page in Studio, find General > Build Pipeline, click Upgrade, and select Federation 2.15. Then run the publish command again.
Non-federated graph error
The error message:
error[E007]: The graph `[APOLLO_GRAPH_REF]` is a non-federated graph.
This operation is only possible for federated graphs.
How to fix it:
This means your graph was created with the Monograph architecture. Create a new graph with the Supergraph architecture (see the previous lesson) and update APOLLO_GRAPH_REF in your .env file. If you'd rather keep this graph, re-run the same publish command with a --convert flag, then upgrade the build pipeline to Federation 2.15 as described above.
Seeing another error?
For any other scenarios not covered here, please see the official docs on Rover errors.
Still having trouble? Visit the Odyssey forums to get help.
A subgraph is born!
In the browser, let's get back to the graph in Studio. We'll see a label at the top indicating that it's federated, along with the specific federation version.
studio.apollographql.com
Let's navigate to the Subgraphs page. We can see we have one subgraph, monolith and all its details. (If you don't see the Subgraphs menu option, try refreshing the page!)
"Similar to HTTP status code, represents the status of the mutation"
code:Int!
"Indicates whether the mutation was successful"
success:Boolean!
"Human-readable message for the UI"
message:String!
"Updated wallet amount"
amount:Float
}
{
"name":"airlock-course2-starter",
"version":"1.0.0",
"description":"",
"main":"index.js",
"engines":{
"npm":">=6.0.0",
"node":">=22"
},
"scripts":{
"start":"nodemon index.js",
"launch":"node utils/launch.js",
"postinstall":"npm run install-services",
"install-services":"node utils/install.js",
"db:reset":"node utils/reset.js"
},
"nodemonConfig":{
"ext":"*.js, *.graphql"
},
"keywords":[],
"author":"",
"license":"ISC",
"dependencies":{
"@apollo/datasource-rest":"^6.4.1",
"@apollo/server":"^5.5.1",
"@apollo/subgraph":"^2.17.0",
"axios":"^1.21.0",
"concurrently":"^10.0.6",
"date-fns":"^4.4.0",
"graphql":"^16.11.0",
"graphql-tag":"^2.12.7",
"sequelize":"^6.37.8",
"sqlite3":"^6.0.1",
"uuid":"^14.0.3"
},
"devDependencies":{
"nodemon":"^3.1.14"
}
}
Practice
Which of these does the migration plan enable us to do when converting the monolith into a supergraph?
Key takeaways
We set up the monolith subgraph to use port 4001 because, as we outlined in our migration plan, we want the router to take over port 4000. (Don't worry—we haven't gotten there yet!) This way, the client doesn't need to make any changes to communicate with the router.
Because our graph uses a supergraph architecture, we can publish the monolith as our first subgraph with rover subgraph publish. The graph's build pipeline needs to support the Federation version in the subgraph's @link (we're using 2.15).
Up next
Let's review our migration plan. We can successfully check off the first step, so let's see what's up next:
✅ Convert the monolith GraphQL server into a subgraph server, which we'll run on a different port. This subgraph will be published to the schema registry.
➡️ Create a router running on the monolith's original port. The router will be connected to the schema registry and will handle all of the same queries that were previously being sent to the monolith server.
Start to split off small chunks of our single monolith subgraph into new domain-specific subgraphs. This will take several steps, which we'll explain in more detail later on.
The next step is to configure the GraphOS Router. This router needs to handle authentication and authorization in Airlock, so let's learn about how that works in the next lesson.
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.