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.
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:
- Introspect the remote APIs. (Finding out what schema structure you have to work with.)
- Handle type name collisions.
- Associate which fields get added to which types.
- Resolve the data.
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.
- The endpoints are not versioned or reliable and might change on you without proper notice.
- One endpoint for all your data also means one endpoint to take down the project.
- Difference in TTL for your data.
- 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.
Find the rest of the tutorial on the GraphCMS Blog here.