How to add Apollo Federation in Node.js
Apollo Federation allows you to compose multiple GraphQL services into a single unified API gateway. As the creator of CoreUI with 12 years of Node.js backend experience, I’ve architected federated GraphQL systems serving millions of requests daily.
The most scalable approach is to create separate subgraph services and compose them with Apollo Gateway.
Install Dependencies
Install Apollo Federation packages:
npm install @apollo/server @apollo/subgraph graphql
npm install @apollo/gateway
Create Subgraph Service
Create services/users/index.js:
const { ApolloServer } = require('@apollo/server')
const { startStandaloneServer } = require('@apollo/server/standalone')
const { buildSubgraphSchema } = require('@apollo/subgraph')
const gql = require('graphql-tag')
const typeDefs = gql`
type User @key(fields: "id") {
id: ID!
name: String!
email: String!
}
type Query {
users: [User]
user(id: ID!): User
}
`
const users = [
{ id: '1', name: 'John Doe', email: '[email protected]' },
{ id: '2', name: 'Jane Smith', email: '[email protected]' }
]
const resolvers = {
Query: {
users: () => users,
user: (_, { id }) => users.find(u => u.id === id)
},
User: {
__resolveReference(reference) {
return users.find(u => u.id === reference.id)
}
}
}
const server = new ApolloServer({
schema: buildSubgraphSchema({ typeDefs, resolvers })
})
startStandaloneServer(server, { port: 4001 })
.then(({ url }) => console.log(`Users subgraph ready at ${url}`))
Create Another Subgraph
Create services/posts/index.js:
const { ApolloServer } = require('@apollo/server')
const { startStandaloneServer } = require('@apollo/server/standalone')
const { buildSubgraphSchema } = require('@apollo/subgraph')
const gql = require('graphql-tag')
const typeDefs = gql`
type Post @key(fields: "id") {
id: ID!
title: String!
content: String!
authorId: ID!
author: User
}
extend type User @key(fields: "id") {
id: ID! @external
posts: [Post]
}
type Query {
posts: [Post]
post(id: ID!): Post
}
`
const posts = [
{ id: '1', title: 'First Post', content: 'Content 1', authorId: '1' },
{ id: '2', title: 'Second Post', content: 'Content 2', authorId: '2' }
]
const resolvers = {
Query: {
posts: () => posts,
post: (_, { id }) => posts.find(p => p.id === id)
},
Post: {
__resolveReference(reference) {
return posts.find(p => p.id === reference.id)
},
author(post) {
return { __typename: 'User', id: post.authorId }
}
},
User: {
posts(user) {
return posts.filter(p => p.authorId === user.id)
}
}
}
const server = new ApolloServer({
schema: buildSubgraphSchema({ typeDefs, resolvers })
})
startStandaloneServer(server, { port: 4002 })
.then(({ url }) => console.log(`Posts subgraph ready at ${url}`))
Create Gateway
Create gateway/index.js:
const { ApolloServer } = require('@apollo/server')
const { startStandaloneServer } = require('@apollo/server/standalone')
const { ApolloGateway, IntrospectAndCompose } = require('@apollo/gateway')
const gateway = new ApolloGateway({
supergraphSdl: new IntrospectAndCompose({
subgraphs: [
{ name: 'users', url: 'http://localhost:4001' },
{ name: 'posts', url: 'http://localhost:4002' }
]
})
})
const server = new ApolloServer({ gateway })
startStandaloneServer(server, { port: 4000 })
.then(({ url }) => console.log(`Gateway ready at ${url}`))
Query Example
Query the federated graph:
query {
users {
id
name
posts {
id
title
}
}
}
Best Practice Note
This is the same Apollo Federation architecture we recommend for CoreUI enterprise applications. Federation allows teams to work independently on different services while maintaining a unified GraphQL API. For production, use managed federation with Apollo Studio for schema composition and monitoring.
Related Articles
If you’re building GraphQL APIs, you might also want to learn how to implement caching in Node.js to optimize your federated services.



