apollo-link-optimistic
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

apollo-link-optimistic

npm version Build Status codecov

An Apollo Link that immediately returns an optimistic response provided in the context of the request, before returning the server response(s).

Install

npm install apollo-link-optimistic

or

yarn add apollo-link-optimsitic

Usage

import { ApolloLink } from 'apollo-link';
import { HttpLink } from 'apollo-link-http';
import { RetryLink } from 'apollo-link-retry';
import gql from 'graphql-tag';
 
import OptimisticLink from 'apollo-link-optimistic';
 
this.link = ApolloLink.from([
    new OptimisticLink(),
    new HttpLink({ uri: URI_TO_YOUR_GRAPHQL_SERVER }),
]);
 
const optimisticResponse = {
    data: {
        hello: 'Optimistic World',
    },
};
 
const op = {
    query: gql`{ hello }`,
    context: {
        // OptimisticLink gets the optimistic response from the context.
        optimisticResponse,
    },
};
 
link.execute(op).subscribe({
    next(response) { console.log(response.data.hello); },
    complete() { console.log('complete!'); },
});
 
// Assuming the server responds with { data: { hello: "Server World" } }
// This code will output:
// "Optimistic World"
// "Server World"
// "complete!"

Package Sidebar

Install

npm i apollo-link-optimistic

Weekly Downloads

1

Version

3.0.0

License

MIT

Unpacked Size

223 kB

Total Files

20

Last publish

Collaborators

  • helfer