Back to Apollo Client

SchemaLink

docs/source/api/link/apollo-link-schema.mdx

3.14.12.0 KB
Original Source

<DocBlock canonicalReference="@apollo/client/link/schema!SchemaLink:class" customOrder={["summary", "remarks", "example"]} />

Constructor signature

ts
constructor(
  options: SchemaLink.Options
): SchemaLink

Usage examples

Server Side Rendering

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

js
import { ApolloClient, InMemoryCache } from "@apollo/client";
import { SchemaLink } from "@apollo/client/link/schema";

import schema from "./path/to/your/schema";

const graphqlClient = new ApolloClient({
  cache: new InMemoryCache(),
  ssrMode: true,
  link: new SchemaLink({ schema }),
});

Mocking

For more detailed information about mocking, refer to the graphql-tools documentation.

js
import { ApolloClient, InMemoryCache } from '@apollo/client';
import { SchemaLink } from '@apollo/client/link/schema';
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';

const typeDefs = `
  Query {
  ...
  }
`;

const mocks = {
  Query: () => ...,
  Mutation: () => ...
};

const schema = makeExecutableSchema({ typeDefs });
const schemaWithMocks = addMockFunctionsToSchema({
  schema,
  mocks
});

const apolloCache = new InMemoryCache(window.__APOLLO_STATE__);

const graphqlClient = new ApolloClient({
  cache: apolloCache,
  link: new SchemaLink({ schema: schemaWithMocks })
});

Types

<InterfaceDetails canonicalReference="@apollo/client/link/schema!SchemaLink.Options:interface" headingLevel={3} displayName="SchemaLink.Options" />

<DocBlock canonicalReference="@apollo/client/link/schema!SchemaLink.ResolverContext:type" />

Signature

ts
type ResolverContext = Record<string, any>;

<FunctionDetails canonicalReference="@apollo/client/link/schema!SchemaLink.SchemaLinkDocumentationTypes.ResolverContextFunction:function(1)" headingLevel={3} result={false} displayName="SchemaLink.ResolverContextFunction" />