Docs
Launch GraphOS Studio

Moving a GraphQL monolith to Apollo Federation

Steps for migrating from a GraphQL monolith to a federated supergraph

federationserver

💡 TIP

For a complete, step-by-step tutorial, check out

.

As with any monolithic service, teams can struggle to change and maintain their API as it grows larger and receives contributions from more developers.

Breaking up the monolith into smaller APIs might be the right strategy for your team. With , you can break up a monolith without sacrificing the unified API that your client applications depend on. Each subgraph can be independently updated, deployed, and scaled while contributing to a single unified schema.

Here are the steps we recommend to convert a monolithic API into a federated GraphQL API.

Planning and preparation

1. Put a router in front of your existing API

Start the process by making a "federated of one." Your existing monolith can act as a without any schema changes.

  1. If you're not already publishing your schema to , create a new graph in your Studio organization. Choose "" for your graph architecture.

  2. Publish your monolith schema to as a with the following command (modify your --routing-url and --schema path as needed):

    rover subgraph publish --name monolith \
    --schema ./schema.graphql \
    --routing-url http://monolith.prod.svc.cluster.local/graphql \
    --convert # necessary if you're publishing to an existing variant
  3. Deploy an instance of the

    to your environment.

    Self-hosting the is limited to

    . Other plan types use
    managed cloud routing with GraphOS
    . Check out the
    pricing page
    to learn more.

  4. Set up

    so that the monolith receives any necessary headers from the .

  5. Set up internal routing rules to redirect client requests to the instead of your monolith.

  6. Enable usage metrics reporting to .

  7. Add

    to your monolith's CI pipeline.

At this point, client requests go to your new instead of the monolith, but it's serving the same schema so clients won't know the difference.

Not only are you prepared to federate your schema, you now have -level visibility into usage and breaking change detection.

2. Identify entities

Next, look through all the types in your schema and identify possible

. Entities are types that form the foundation of your data model, and they must include that can uniquely identify each instance of them.

Consider this schema for a travel booking site:

Types such as User, Reservation, Flight, and Hotel are uniquely identifiable, whereas Profile, CancellationPolicy, and Amenity are basically groups of attributes attached to those entities.

3. Group entities

After you've identified your entities, group them by their logical domain or concern. These groups usually correspond to product boundaries, but they might also be team boundaries. This is how you'll determine how many you'll end up with.

Accounts domainFlights domainHotels domain
AccountAirplaneBed
UserAirlineHotel
FlightReservation
ReservationRoom
Seat

4. Rank entities by ease of migration

When you're deciding how to start migrating types to other , it's helpful to consider a few things first:

Count the number of value types associated with an . You'll need to copy all those types over to the new when you migrate the entity. Entities with fewer related , enums, and non-entity will be a bit easier to migrate.

You won't need to move related entities at the same time as long as you can return an

. For example, you can move the Room type if you have access to the Hotel foreign key:

Hotels subgraph
type Room @key(fields: "number") {
number: ID!
floor: Int
hotel: Hotel
}
type Hotel @key(fields: "id", resolvable: false) {
id: ID! # comes from rooms.hotel_id in the database
}

It might be safer and easier to move the entire Room type but only a "stub" of the Hotel type. The can fetch the rest of the Hotel from the monolith until you move that type as well.

How complex will your query plans become during the migration?

If you start by moving a type that's deeply interconnected with other types, you might introduce unnecessary complexity to your 's . For example, consider this query:

query MyFlights {
me {
reservations {
flights {
...FlightDetails
}
}
}
}

This returns a list of Reservation objects belonging to a particular User, and each Reservation contains a list of Flights. If you start by moving the Reservation type to another , this results in an "A→B→A" (fetching the User, then the Reservation, then the Flight in three serial fetches):

monolith
subgraph
monolith

A better choice at this stage might be to move the Flight type so that the is much more efficient, fetching both the User and Reservation together before fetching the Flight:

monolith
subgraph

When you move a type to another , you should also move all root-level that return that type (such as Query.flight(id:). This way, objects of that type can be returned with only a single in the best case. And in the general case, the can fetch any additional data with fewer total subgraph operations:

subgraph
monolith

Inevitably, some become more complex while you're migrating types between . By ranking your entities and moving the lowest-impact ones first, you can minimize this increase.

Implementation

1. Make your monolith a real subgraph

Now that you have a migration plan, you can start making schema and code changes. The first change is to add the

to the monolith. The steps involved depend on which
Apollo-Federation-compatible library
you use with your monolith's language and framework.

The most important functionality to add is defining your entities (by adding @key ) and adding their

.

2. Deploy your new subgraph

Start with an empty to quickly set up your deployment and continuous integration pipelines. You can use this stub , which won't affect the client-facing schema:

extend schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@shareable", "@inaccessible"])
type Query {
_todo: String @shareable @inaccessible
}

After your new is deployed, set up

so that you can catch errors quickly and start contributing to the .

  1. Start by marking all the value types (enums and non- ) you're going to move to the as @shareable in the monolith.
  2. Copy the types and over to the and port their from the monolith.
  3. Deploy the and test it by making calls to it directly. Use the
    _entities root field
    to test joins between entities.

When you're satisfied with the behavior and performance of your new , you can start moving all traffic to it and cleaning up the monolith.

  1. Use the
    @override
    to mark in the with @override(from: "monolith"), telling the to prefer the new over the monolith.
  2. Remove types and from the monolith schema.
  3. Delete unneeded from the monolith.
  4. Remove @override from the .
  5. Remove @shareable from types and in the when appropriate.

4. Migrate additional functionality out of the monolith as desired

Towards the end of the migration, you can decide whether to leave the monolith in place to handle the few entities that sit in the middle of your domain (such as User), or completely deconstruct the monolith into new services and decommission it. Either way, your newly federated API is well-positioned to scale even larger in the future.

Next
Home
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company