You can use GraphQL Yoga to implement a Gateway exposing an
Apollo Federation supergraph. For this, the gateway
needs to have access to the supergraph SDL.
You have the choice between loading it from a schema registry (like Hive or Apollo GraphOS) or
locally from the file system.
Instead of using GraphQL Yoga to implement the gateway, we recommend to use
Hive Gateway, our open-source MIT-licensed
GraphQL gateway for Apollo Federation.
Using Hive
Hive is a schema registry and usage reporting plateform for GraphQL. You can use it to manage an
Apollo Federation supergraph.
Apollo GraphOS can be used as a schema registry when using Managed Federation.
You can use GraphQL Yoga to implement a Gateway exposing the Managed Federation supergraph stored in
GraphOS by using @graphql-yoga/apollo-managed-federation. This plugin will automatically fetch the
supergraph SDL from GraphOS, sticth it into an executable schema and serve it.
It also starts a polling mechanism to keep the schema up to date in the background and handle retry
on failure.
Installation
npm i graphql-yoga graphql @graphql-yoga/apollo-managed-federation
You can also provide this configuration programaitically by providing a configurtion object to the
plugin, along with other options to customize the polling/retry behavior.
const yoga = createYoga({ plugins: [ useManagedFederation({ apiKey: '<YOU_APOLLO_API_KEY>', graphRef: '<YOUR_GRAPH_ID>[@<VARIANT>]', maxRetries: 5, // max retries in case of loading failure retryDelaySeconds: 0, // delay between retries minDelaySeconds: 1 // minimum delay between polling requests }) ]})
Other more advanced options are available, please refer to the type definitions for more
information.
Using the file system
To create a gateway server without using Apollo GraphOS, you can use the @graphql-tools/federation
which can generate an executable schema from a supergraph SDL.
Installation
npm i graphql-yoga graphql @graphql-tools/federation
bun add graphql-yoga graphql @graphql-tools/federation
Example
To generate the schema, we first need the supergraph SDL. This can be generated by the Apollo Rover
CLI or fetched from a schema registry (like Hive or Apollo GraphOS).
To generate the supergraph SDL locally, you first have to define a supergraph configuration by
creating a supergraph.yaml file.
import { readFileSync } from 'fs'import { createServer } from 'node:http'import { createYoga } from 'graphql-yoga'import { getStitchedSchemaFromSupergraphSdl } from '@graphql-tools/federation'const yoga = createYoga({ schema: getStitchedSchemaFromSupergraphSdl({ // This doesn't have to be from a file system, it can be fetched via HTTP from a schema registry supergraphSdl: readFileSync('./supergraph.graphql', 'utf-8') })})const server = createServer(yoga)server.listen(4000, () => { console.log(`🚀 Server ready at http://localhost:4000/graphql`)})
Handling Subgraph Errors
By default, GraphQL Yoga masks any unexpected GraphQL Errors. This is done to prevent leaking
internal errors to the client. If you know that your subgraph is safe and you want to expose the
errors to the client, you can customize the error masking bahviour.
bun add graphql-yoga @graphql-yoga/plugin-apollo-inline-trace graphql
Example Federated tracing
import { createServer } from 'http'import { createYoga } from 'graphql-yoga'import { useApolloInlineTrace } from '@graphql-yoga/plugin-apollo-inline-trace'const yoga = createYoga({ plugins: [ useApolloInlineTrace() // ...rest of your Apollo federation plugins ]})// Start the server and explore http://localhost:4000/graphqlconst server = createServer(yoga)server.listen(4000, () => { console.info('Server is running on http://localhost:4000/graphql')})