Docs
Launch GraphOS Studio

API Reference: @apollo/subgraph


This API reference documents the exports from the @apollo/subgraph package. This package enables you to use as a in a federated . For more information, see

.

Note, we recommend using @apollo/subgraph with , but it is compatible with any built on graphql-js.

buildSubgraphSchema

This method was renamed from buildFederatedSchema after @apollo/federation v0.28.0 (the previous name still works, but it 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 }),
});
const server = new ApolloServer({
schema: buildSubgraphSchema({ typeDefs, resolvers }),
});

Used when

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

import gql from 'graphql-tag';
import { ApolloServer } from '@apollo/server';
import { buildSubgraphSchema } from '@apollo/subgraph';
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 }),
});
import gql from 'graphql-tag';
import { ApolloServer } from '@apollo/server';
import { buildSubgraphSchema } from '@apollo/subgraph';
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

in a 's map.

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

.

If the can be resolved, __resolveReference returns the . Otherwise, it returns null.

The function takes the parameters listed below.

Parameters

Name /
Type
Description
reference

Object

The representation of the that's passed from another .

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

context

Object

An object that's passed to every that executes for a particular , enabling resolvers to share helpful context.

Within and plugins, this object is named contextValue. For details, see

.

info

Object

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

This object's core are listed in the

.

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);
},
},
};
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
Setup
Next
Setup
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company