# apollo-link-schema

> Use a GraphQL Schema to request data

Latest version **1.2.5** (published 2020-04-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install apollo-link-schema
pnpm add apollo-link-schema
yarn add apollo-link-schema
bun add apollo-link-schema
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.5 |
| Published | 2020-04-09 |
| First published | 2017-12-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 24 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1423 |
| Author | Tomas Trescak |
| Maintainers | apollo-bot, evanshauser, jbaxleyiii, peggyrayzis |

## Links

- npm: https://www.npmjs.com/package/apollo-link-schema
- Repository: https://github.com/apollographql/apollo-link
- Homepage: https://github.com/apollographql/apollo-link#readme
- Issues: https://github.com/apollographql/apollo-link/issues
- npm.io page: https://npm.io/package/apollo-link-schema

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^1.9.3
- [apollo-link](https://npm.io/package/apollo-link.md) ^1.2.14

## Recent versions

- 1.2.5 (latest) — 2020-04-09
- 2.0.0-beta.0 (beta) — 2019-11-15
- 1.2.4 — 2019-09-06
- 1.2.3 — 2019-06-14
- 1.2.2 — 2019-03-14
- 1.2.1 — 2019-03-14
- 1.2.0 — 2019-03-05
- 1.1.6 — 2019-02-01
- 1.1.5 — 2019-02-01
- 1.1.4 — 2018-12-15
- 1.1.3 — 2018-12-13
- 1.1.2 — 2018-11-21
- 1.1.1 — 2018-09-15
- 1.1.0 — 2018-04-12
- 1.0.6 — 2018-02-23
- … 6 more at https://npm.io/package/apollo-link-schema/versions

## README

---
title: apollo-link-schema
description: Assists with mocking and server-side rendering
---

The schema link provides a [graphql execution environment](http://graphql.org/graphql-js/graphql/#graphql), which allows you to perform GraphQL operations on a provided schema. This type of behavior is commonly used for server-side rendering (SSR) to avoid network calls and mocking data. While the schema link could provide graphql results on the client, currently the graphql execution layer is [too heavy weight](https://bundlephobia.com/result?p=graphql) for practical application. To unify your state management with client-side GraphQL operations, you should use [apollo-link-state](state), because it integrates with the Apollo Client cache and is much more lightweight.

## Installation

`npm install apollo-link-schema --save`

## Usage

### Server Side Rendering

When performing SSR _on the same server_ you can use this library to avoid making network calls.

```js
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { SchemaLink } from 'apollo-link-schema';

import schema from './path/to/your/schema';

const graphqlClient = new ApolloClient({
  ssrMode: true,
  cache: new InMemoryCache(),
  link: new SchemaLink({ schema })
});
```

### Mocking

For more detailed information about mocking, please look the [graphql-tools documentation](https://www.apollographql.com/docs/graphql-tools/mocking.html).

```js
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { SchemaLink } from 'apollo-link-schema';
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';

const typeDefs = `
  Query {
  ...
  }
`;

const mocks = {
  Query: () => ...,
  Mutation: () => ...
};

const schema = makeExecutableSchema({ typeDefs });
addMockFunctionsToSchema({
  schema,
  mocks
});

const apolloCache = new InMemoryCache(window.__APOLLO_STATE__);

const graphqlClient = new ApolloClient({
  cache: apolloCache,
  link: new SchemaLink({ schema })
});
```

### Options

The `SchemaLink` constructor can be called with an object with the following properties:

* `schema`: an executable graphql schema
* `rootValue`: the root value that is passed to the resolvers (i.e. the first parameter for the [rootQuery](http://graphql.org/learn/execution/#root-fields-resolvers))
* `context`: an object passed to the resolvers, following the [graphql specification](http://graphql.org/learn/execution/#root-fields-resolvers) or a function that accepts the operation and returns the resolver context. The resolver context may contain all the data-fetching connectors for an operation.

---
_Source: https://npm.io/package/apollo-link-schema · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
