这是indexloc提供的服务,不要输入任何密码

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.

note
While 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.
TypeScript
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

TypeScript
1constructor(
2  options: SchemaLink.Options
3): SchemaLink

Usage examples

Server Side Rendering

When performing SSR on the same server, you can use this library to avoid making network calls.

JavaScript
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.

JavaScript
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.

Properties
Name / Type
Description
SchemaLink.ResolverContext | SchemaLink.ResolverContextFunction

Context 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.

GraphQLSchema

An 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.

TypeScript
1 import { makeExecutableSchema } from "@graphql-tools/schema";
2
3 const schema = makeExecutableSchema({
4   typeDefs,
5   resolvers,
6 });
7
8 const link = new SchemaLink({ schema });

Whether 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

TypeScript
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

TypeScript
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

TypeScript
1ResolverContextFunction(
2  operation: ApolloLink.Operation
3): SchemaLink.ResolverContext | PromiseLike<SchemaLink.ResolverContext>

Parameters

Name / Type
Description
operation
ApolloLink.Operation

The Apollo Link operation

Show/hide child attributes
ApolloClient

The 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 | undefined

The string name of the GraphQL operation. If it is anonymous, operationName will be undefined.

OperationTypeNode

The type of the GraphQL operation, such as query or mutation.

DocumentNode

A 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.

OperationVariables

A map of GraphQL variables being sent with the operation.

Feedback

Edit on GitHub

Ask Community