Federation
Apollo Federation allows you to declaratively combine multiple GraphQL APIs into a single federated graph. A federated graph enables clients to interact with multiple APIs through a single request.
GQLoom Federation provides GQLoom's support for Apollo Federation.
Installation
GraphQL Directives
Apollo Federation directives (Directives) are used to describe how to combine multiple GraphQL APIs into a federated graph.
Declare Directives on Objects
In GQLoom, we can declare GraphQL directives in the directives
field of the extensions
property of objects and fields:
In the above example, we declared a @key
directive, which marks the id
field of the User
object as a resolvable field. We will get the following Schema:
Declare Directives on Resolvers
We can also use resolvers to declare directives for objects:
Add Directives to the Schema
Directive Formats
We have two formats for declaring directives:
- Using an array:
- Using key-value pairs:
Resolve Reference
@gqloom/apollo
provides the resolveReference
function to help you resolve references.
Weaving
The FederatedSchemaWeaver.weave
function imported from @gqloom/federation
is used to weave the Federation Schema. Compared with @gqloom/core
, the FederatedSchemaWeaver.weave
function in @gqloom/apollo
will output a Schema with directives.
It's also worth noting that we need to use the printSubgraphSchema
function imported from @apollo/subgraph
to convert the Schema to text format to preserve the directives.