UNPKG

@shopify/react-graphql

Version:

Tools for creating type-safe and asynchronous GraphQL components for React

45 lines (40 loc) 1.11 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var client = require('@apollo/client'); function createSsrExtractableLink() { return new SsrExtractableLink(); } class SsrExtractableLink extends client.ApolloLink { constructor(...args) { super(...args); this.operations = new Set(); } resolveAll(then) { if (this.operations.size > 0) { return Promise.all([...this.operations]).then(then); } return then(); } request(operation, nextLink) { if (nextLink == null) { throw new Error('SsrExtractableLink must not be the last link in the chain.'); } let operationDone; const promise = new Promise(resolve => { operationDone = () => { this.operations.delete(promise); resolve(); }; }); this.operations.add(promise); const observable = nextLink(operation); observable.subscribe({ complete() { operationDone(); } }); return observable; } } exports.SsrExtractableLink = SsrExtractableLink; exports.createSsrExtractableLink = createSsrExtractableLink;