Docs
Launch GraphOS Studio
You're viewing documentation for a previous version of this software. Switch to the latest stable version.

API Reference: @apollo/subgraph

Apollo Federation API reference


This API reference documents the exports from the @apollo/subgraph package.

buildSubgraphSchema

This method previously existed in the @apollo/federation package and was renamed from buildFederatedSchema after @apollo/federation v0.28.0 (the previous name still works but might be removed in a future release).

A function that takes a schema module object (or an array of them) and returns a federation-ready :

const server = new ApolloServer({
schema: buildSubgraphSchema({ typeDefs, resolvers })
});

Used when defining a subgraph in a federated .

Each schema module is an object with the following format:

{
typeDefs: DocumentNode,
resolvers: ResolverMap
}

Parameters

Name /
Type
Description
modules

Object or Array

Required. A schema module object (or an array of them) with the structure shown above.

Example

const typeDefs = gql`
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 = new ApolloServer({
schema: buildSubgraphSchema({ typeDefs, resolvers })
});

__resolveReference

The name of a special reference resolver function you can define for every entity in a map, if that resolver map is part of a subgraph schema.

The __resolveReference function enables your gateway's to resolve a particular by whatever unique identifier your other use to reference it. For details, see Resolving entities.

The function takes the parameters listed below.

Parameters

Name /
Type
Description
reference

Object

The representation of the entity that's passed from another .

This object includes a __typename , along with whichever fields the subgraph uses for the entity's @key.

context

Object

An object that's passed to every resolver that executes for a particular . This enables resolvers to share helpful context, including any relevant DataSources.

For details, see The context argument.

info

Object

Contains information about the operation's execution state, including the field name, the path to the field from the root, and more.

This object's core are listed in the GraphQL.js source code, and it is extended with additional functionality by other modules, like apollo-cache-control.

Example

const typeDefs = gql`
type User @key(fields: "id") {
id: ID!
username: String
}
`;
const resolvers = {
User: {
__resolveReference(user, { datasources }){
// user will always have at least the `id` and the `__typename` here
return datasources.users.fetchUserById(user.id)
}
}
};
Previous
Exporting designs
Next
@apollo/gateway
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company