SchemaLink
Assists with mocking and server-side rendering
SchemaLink is a terminating link that executes GraphQL operations against
a local GraphQL schema instead of making network requests. This is commonly
used for server-side rendering (SSR) and mocking data.
SchemaLink can provide GraphQL results on the client, the GraphQL
execution layer is quite large for practical client-side use.
For client-side state management, consider Apollo Client's local state management
functionality instead, which integrates with the Apollo Client cache.1 import { SchemaLink } from "@apollo/client/link/schema";
2 import schema from "./path/to/your/schema";
3
4 const link = new SchemaLink({ schema });Constructor signature
1constructor(
2 options: SchemaLink.Options
3): SchemaLinkUsage examples
Server Side Rendering
When performing SSR on the same server, you can use this library to avoid making network calls.
1import { ApolloClient, InMemoryCache } from "@apollo/client";
2import { SchemaLink } from "@apollo/client/link/schema";
3
4import schema from "./path/to/your/schema";
5
6const graphqlClient = new ApolloClient({
7 cache: new InMemoryCache(),
8 ssrMode: true,
9 link: new SchemaLink({ schema }),
10});Mocking
For more detailed information about mocking, refer to the graphql-tools documentation.
1import { ApolloClient, InMemoryCache } from '@apollo/client';
2import { SchemaLink } from '@apollo/client/link/schema';
3import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
4
5const typeDefs = `
6 Query {
7 ...
8 }
9`;
10
11const mocks = {
12 Query: () => ...,
13 Mutation: () => ...
14};
15
16const schema = makeExecutableSchema({ typeDefs });
17const schemaWithMocks = addMockFunctionsToSchema({
18 schema,
19 mocks
20});
21
22const apolloCache = new InMemoryCache(window.__APOLLO_STATE__);
23
24const graphqlClient = new ApolloClient({
25 cache: apolloCache,
26 link: new SchemaLink({ schema: schemaWithMocks })
27});Types
Options for configuring the SchemaLink.
SchemaLink.ResolverContext | SchemaLink.ResolverContextFunctionContext object or function that returns the context object to provide to resolvers. The context is passed as the third parameter to all GraphQL resolvers.
If a static object is provided, the same context will be used for all operations
If a function is provided, the function is called for each operation to generate operation-specific context
The root value passed to root-level resolvers. It's typically not used in most schemas but can be useful for certain advanced patterns.
GraphQLSchemaAn executable GraphQL schema to use for operation execution.
Read more...
This should be a complete, executable GraphQL schema created using
tools like makeExecutableSchema from @graphql-tools/schema or
buildSchema from graphql.
1 import { makeExecutableSchema } from "@graphql-tools/schema";
2
3 const schema = makeExecutableSchema({
4 typeDefs,
5 resolvers,
6 });
7
8 const link = new SchemaLink({ schema });booleanWhether to validate incoming queries against the schema before execution.
When enabled, queries will be validated against the schema before execution,
and validation errors will be returned in the result's errors array,
just like a remote GraphQL server would.
This is useful for testing and development to catch query errors early, but may add overhead in production environments.
SchemaLink.ResolverContext
The resolver context object passed to GraphQL resolvers.
This context object is passed as the third parameter to GraphQL resolvers and typically contains data-fetching connectors, authentication information, and other request-specific data.
Signature
1type ResolverContext = Record<string, any>;A function that returns the resolver context for a given operation.
This function is called for each operation and allows you to create operation-specific context. This is useful when you need to include information from the operation (like headers, variables, etc.) in the resolver context.
Example
1 const link = new SchemaLink({
2 schema,
3 context: (operation) => {
4 return {
5 userId: operation.getContext().userId,
6 dataSources: {
7 userAPI: new UserAPI(),
8 },
9 };
10 },
11 });Signature
1ResolverContextFunction(
2 operation: ApolloLink.Operation
3): SchemaLink.ResolverContext | PromiseLike<SchemaLink.ResolverContext>Parameters
The Apollo Link operation
Show/hide child attributes
ApolloClientThe Apollo Client instance executing the request.
Record<string, any>A map that stores extensions data to be sent to the server.
() => Readonly<ApolloLink.OperationContext>A function that gets the current context of the request. This can be used by links to determine which actions to perform. See managing context
string | undefinedThe string name of the GraphQL operation. If it is anonymous,
operationName will be undefined.
OperationTypeNodeThe type of the GraphQL operation, such as query or mutation.
DocumentNodeA DocumentNode that describes the operation taking place.
{
(context: Partial<ApolloLink.OperationContext>): void;
(updateContext: (previousContext: Readonly<ApolloLink.OperationContext>) => Partial<ApolloLink.OperationContext>): void;
}A function that takes either a new context object, or a function which takes in the previous context and returns a new one. See managing context.
OperationVariablesA map of GraphQL variables being sent with the operation.