Docs / Features / Apollo Federation Apollo Federation GraphQL Yoga v2 documentation (superseded by v5): Learn how to use GraphQL Yoga with Apollo Federation. GraphQL Yoga is a fully-featured GraphQL Server with focus on easy setup, performance & great developer experience.
v5latest v4 v3 v2
This is the documentation for the old GraphQL Yoga v2 We recommend upgrading to the latest GraphQL Yoga v5.
Migrate to GraphQL Yoga v5 .
Apollo Federation is a specification that applies
microservice architecture through GraphQL APIs.
Thanks to Envelopβs Apollo Federation plugin,
we can use GraphQL Yoga to build our gateway server.
As documented in the Apollo Federation
docs , @apollo/gateway package
doesnβt support GraphQL v16 so you have to install graphql@15.
Please note that Apollo Federation implementation doesnβt support GraphQL Subscriptions. If you
need to use Subscriptions with a Federated Gateway you can use Schema
Stitching .
Gateway
Installation for Gateway
npm yarn pnpm bun
npm i @graphql-yoga/node @apollo/gateway @envelop/apollo-federation graphql@15 yarn add @graphql-yoga/node @apollo/gateway @envelop/apollo-federation graphql@15 pnpm add @graphql-yoga/node @apollo/gateway @envelop/apollo-federation graphql@15 bun add @graphql-yoga/node @apollo/gateway @envelop/apollo-federation graphql@15
Example Gateway
import { ApolloGateway, RemoteGraphQLDataSource } from '@apollo/gateway'
import { useApolloFederation } from '@envelop/apollo-federation'
import { createServer } from '@graphql-yoga/node'
/**
* Needed since federation remote data source fetcher
* doesn't support `application/graphql-response+json` content type
* https://github.com/apollographql/federation/issues/2161
* And yoga defaults to `application/graphql-response+json` as per the spec.
*/
class DataSource extends RemoteGraphQLDataSource {
willSendRequest ({ request }) {
request.http.headers. set ( 'accept' , 'application/json' )
}
}
// Initialize the gateway
const gateway = new ApolloGateway ({
serviceList: [
{ name: 'accounts' , url: 'http://localhost:4001' },
{ name: 'products' , url: 'http://localhost:4002' }
// ...additional subgraphs...
],
introspectionHeaders: {
accept: 'application/json'
},
buildService ({ url }) {
return new DataSource ({ url })
}
})
// Make sure all services are loaded
await gateway. load ()
const server = createServer ({
plugins: [ useApolloFederation ({ gateway })]
})
// Start the server and explore http://localhost:4000/graphql
server. start ()
Federation Service
You donβt need any extra plugins for Yoga for Federation Service.
Installation
npm yarn pnpm bun
npm i @graphql-yoga/node @apollo/subgraph graphql yarn add @graphql-yoga/node @apollo/subgraph graphql pnpm add @graphql-yoga/node @apollo/subgraph graphql bun add @graphql-yoga/node @apollo/subgraph graphql
Example User Service
const { parse } = require ( 'graphql' )
const { buildSubgraphSchema } = require ( '@apollo/subgraph' )
const { createServer } = require ( '@graphql-yoga/node' )
const typeDefs = parse ( /* GraphQL */ `
type Query {
me: User
}
type User @key(fields: "id") {
id: ID!
username: String
}
` )
const resolvers = {
Query: {
me () {
return { id: '1' , username: '@ava' }
}
},
User: {
__resolveReference ( user , { fetchUserById }) {
return fetchUserById (user.id)
}
}
}
const server = createServer ({
schema: buildSubgraphSchema ([{ typeDefs, resolvers }]),
port: 4001
})
server. start (). then (() => {
console. log ( `π Server ready at http://localhost:4001` )
})
Working Example
Check our
working example to
try it out.
β Testing Configuring CORS with Yoga β