npm.io
0.0.19 • Published 3 years ago

@graphql-sse/apollo-client

Licence
MIT
Version
0.0.19
Deps
2
Size
9 kB
Vulns
0
Weekly
0

@graphql-sse/apollo-client

A client for GraphQL subscription using Server Sent Events.

Installation

npm install @graphql-sse/apollo-client

Usage

Basic Example

The snippet below is from the Example React App where you have a full working example App.

import { split, HttpLink, ApolloClient, InMemoryCache } from '@apollo/client';
import { getMainDefinition } from '@apollo/client/utilities';
import { ServerSentEventsLink } from '@graphql-sse/apollo-client';

const httpLink = new HttpLink({
  uri: 'http://localhost:4000/graphql',
});

const sseLink = new ServerSentEventsLink({
  graphQlSubscriptionUrl: 'http://localhost:4000/graphql',
});

const splitLink = split(
  ({ query }) => {
    const definition = getMainDefinition(query);
    return (
      definition.kind === 'OperationDefinition' &&
      definition.operation === 'subscription'
    );
  },
  sseLink,
  httpLink
);

export const client = new ApolloClient({
  link: splitLink,
  cache: new InMemoryCache(),
});

Keywords