Federation

Apollo Federation lets you declaratively combine multiple GraphQL APIs into a single, federated graph. This federated graph enables clients to interact with multiple APIs through a single request.

GQLoom Federation provides GQLoom support for Apollo Federation.

Installation

npm
yarn
pnpm
bun
npm install graphql @gqloom/core @apollo/subgraph @gqloom/federation

GraphQL Directives

Apollo Federation Directives is used to describe how to combine multiple GraphQL APIs into a federated graph.

In GQLoom, we can declare GraphQL directives in the directives field of the extensions property of objects and fields:

valibot
zod
graphql.js
import * as v from "valibot" import { asObjectType } from "../src" export const User = v.pipe( v.object({ id: v.string(), name: v.string(), }), asObjectType({ name: "User", extensions: { directives: { key: { fields: "id", resolvable: true } }, }, }) ) export interface IUser extends v.InferOutput<typeof User> {}

In the above example, we have declared a @key directive which marks the id field of the User object as a resolvable field and we will get the following Schema:

type User @key(fields: "id", resolvable: true) { id: String! name: String! }

We have two formats for declaring instructions:

  • Using arrays:
{ directives: [ { name: "validation", args: { regex: "/abc+/" } }, { name: "required", args: {}, } ] }
  • Using key-value pairs:
{ directives: { validation: { regex: "/abc+/" }, required: {} } }

Resolve Reference

@gqloom/apollo provides the resolveReference function to help you resolve references.

import { loom } from "@gqloom/core" import { resolveReference } from "@gqloom/federation" const { resolver, query } = loom export const UserResolver = resolver.of( User, { user: query(User, () => ({ id: "1", name: "John" })), }, { extensions: { ...resolveReference<IUser, "id">((user) => getUserByID(user.id)), }, } )

Weaving

The FederatedSchemaWeaver.weave function, introduced from @gqloom/federation, is used to weave Federation Schema. compared to @gqloom/core, the FederatedSchemaWeaver.weave function in @gqloom/apollo will output Schema with Directives.

It is also worth noting that we need to use the printSubgraphSchema function imported from @apollo/subgraph to convert the Schema to a textual format in order to preserve the Directives.

import { FederatedSchemaWeaver } from "@gqloom/federation" import { printSubgraphSchema } from "@apollo/subgraph" const schema = FederatedSchemaWeaver.weave(UserResolver) const schemaText = printSubgraphSchema(schema)