DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Cover image for GraphQL Schema Stitching
Makenna Smutz
Makenna Smutz

Posted on

GraphQL Schema Stitching

GraphQL Schema Stitching and Enhancing Content APIs

UPDATE: While not entirely the same thing, thereโ€™s a new kid on the block for composing schemas called federation. Weโ€™ll be dropping our take on that in a few weeks. For now, enjoy the content below for a deep-dive into the land of GraphQL schemas.

GraphQL schema stitching is an excellent way to enhance APIs with a wealth of extra data. The concept is simple and the execution is straight forward. How it happens, however, is anything but. What lies beneath is a complex game of delayed requests, resolver assignments and more โ€“ but thanks to helpful folks at Apollo, we can let the tooling do the heavy lifting and give us the benefits of schema stitching with minimum effort.

The High-Level Take on GraphQL Stitching

Schema Stitching is the process of combining multiple schemas from various APIs into a single Schema / API. GraphQL is a fantastic technology that allows the server to resolve our data across tables via a clean, query syntax, but what happens when those tables are actually different databases all together living on different servers?

Wouldnโ€™t it be great if we could resolve a list of hotels in one database with a list of regional activities in another through a single query? Thatโ€™s what schema stitching allows us to do.

Schema stitching follows a four-step process:

  1. Introspect the remote APIs. (Finding out what schema structure you have to work with.)
  2. Handle type name collisions.
  3. Associate which fields get added to which types.
  4. Resolve the data.

When to Ditch the Stitch

It's not always a good idea to stitch your schemas together. Here are some reasons why you might not want to stitch your schemas.

  1. The endpoints are not versioned or reliable and might change on you without proper notice.
  2. One endpoint for all your data also means one endpoint to take down the project.
  3. Difference in TTL for your data.
  4. Performance is a critical factor, you can optimize REST for better performance in server-to-server communication.

With the gotchas in mind, stitching is a great way to combine multiple data sets into a single distributable, explorable and maintainable API. Particularly in the API architecture for MVP projects or one-off sites, it's a great way to get the developers up and running, fast while staying in the GraphQL eco-system and not having the overhead of technical context switching.

Let us Begin

Find the rest of the tutorial on the GraphCMS Blog here.

Top comments (0)

๐ŸŒš Friends don't let friends browse without dark mode.

Sorry, it's true.