1.0.3 • Published 4 years ago

@nextcart/apollo-link-server v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

apollo-server-link

Blog post coming soon at http://nextcart.com/blog

The server link provides a graphql execution environment, which allows you to perform GraphQL operations on a provided server instance. This type of behavior is commonly used for server-side rendering (SSR) to avoid network calls and to unify caching.

Installation

npm install @nextcart/apollo-link-server --save

Usage

Server Side Rendering

When performing SSR on the same server you can use this library to avoid making network calls while still benefiting from the cache. apollo-link-schema does not allow for use of the server's cache.

import { ApolloServer } from 'apollo-server-micro';
import { ApolloClient } from 'apollo-client';
import { ServerLink } from '@nextcart/apollo-link-server';

const apolloServer = new ApolloServer({...})

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

Options

The ServerLink constructor can be called with an object with the following properties:

  • server: an apollo server instance
  • context: (optional) an object passed to the resolvers, following the graphql specification 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.
  • requestHandler: (optional) A function which receives an Operation and a NextLink and returns an Observable of an ExecutionResult