Apollo Federation with GraphQL Yoga#
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#
yarn add @graphql-yoga/node
yarn add @apollo/gateway
yarn add @envelop/apollo-federation
yarn add graphql@15
pnpm add @graphql-yoga/node
pnpm add @apollo/gateway
pnpm add @envelop/apollo-federation
pnpm add graphql@15
npm install @graphql-yoga/node
npm install @apollo/gateway
npm install @envelop/apollo-federation
npm install graphql@15
Example Gateway#
import { createServer } from '@graphql-yoga/node'
import { ApolloGateway } from '@apollo/gateway'
import { useApolloFederation } from '@envelop/apollo-federation'
// Initialize the gateway
const gateway = new ApolloGateway({
serviceList: [
{ name: 'accounts', url: 'http://localhost:4001' },
{ name: 'products', url: 'http://localhost:4002' },
// ...additional subgraphs...
],
})
// 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#
yarn add @graphql-yoga/node
yarn add @apollo/subgraph
yarn add graphql
pnpm add @graphql-yoga/node
pnpm add @apollo/subgraph
pnpm add graphql
npm install @graphql-yoga/node
npm install @apollo/subgraph
npm install 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.