@graphql-tools/links
Advanced tools
Comparing version 6.0.2-alpha-bc3fdcf.0 to 6.0.2-alpha-bf587ef.0
@@ -11,2 +11,3 @@ 'use strict'; | ||
const crossFetch = require('cross-fetch'); | ||
const utils = require('@graphql-tools/utils'); | ||
const graphql = require('graphql'); | ||
@@ -116,71 +117,3 @@ | ||
function observableToAsyncIterable(observable) { | ||
const pullQueue = []; | ||
const pushQueue = []; | ||
let listening = true; | ||
const pushValue = (value) => { | ||
if (pullQueue.length !== 0) { | ||
pullQueue.shift()({ value, done: false }); | ||
} | ||
else { | ||
pushQueue.push({ value }); | ||
} | ||
}; | ||
const pushError = (error) => { | ||
if (pullQueue.length !== 0) { | ||
pullQueue.shift()({ value: { errors: [error] }, done: false }); | ||
} | ||
else { | ||
pushQueue.push({ value: { errors: [error] } }); | ||
} | ||
}; | ||
const pullValue = () => new Promise(resolve => { | ||
if (pushQueue.length !== 0) { | ||
const element = pushQueue.shift(); | ||
// either {value: {errors: [...]}} or {value: ...} | ||
resolve({ | ||
...element, | ||
done: false, | ||
}); | ||
} | ||
else { | ||
pullQueue.push(resolve); | ||
} | ||
}); | ||
const subscription = observable.subscribe({ | ||
next(value) { | ||
pushValue(value); | ||
}, | ||
error(err) { | ||
pushError(err); | ||
}, | ||
}); | ||
const emptyQueue = () => { | ||
if (listening) { | ||
listening = false; | ||
subscription.unsubscribe(); | ||
pullQueue.forEach(resolve => resolve({ value: undefined, done: true })); | ||
pullQueue.length = 0; | ||
pushQueue.length = 0; | ||
} | ||
}; | ||
return { | ||
next() { | ||
return listening ? pullValue() : this.return(); | ||
}, | ||
return() { | ||
emptyQueue(); | ||
return Promise.resolve({ value: undefined, done: true }); | ||
}, | ||
throw(error) { | ||
emptyQueue(); | ||
return Promise.reject(error); | ||
}, | ||
[Symbol.asyncIterator]() { | ||
return this; | ||
}, | ||
}; | ||
} | ||
const linkToSubscriber = (link) => async ({ document, variables, context, info, }) => observableToAsyncIterable(apolloLink.execute(link, { | ||
const linkToSubscriber = (link) => async ({ document, variables, context, info, }) => utils.observableToAsyncIterable(apolloLink.execute(link, { | ||
query: document, | ||
@@ -187,0 +120,0 @@ variables, |
@@ -5,2 +5,3 @@ import { ApolloLink, Observable, concat, toPromise, execute } from 'apollo-link'; | ||
import { fetch } from 'cross-fetch'; | ||
import { observableToAsyncIterable } from '@graphql-tools/utils'; | ||
import { GraphQLScalarType, GraphQLError } from 'graphql'; | ||
@@ -110,70 +111,2 @@ | ||
function observableToAsyncIterable(observable) { | ||
const pullQueue = []; | ||
const pushQueue = []; | ||
let listening = true; | ||
const pushValue = (value) => { | ||
if (pullQueue.length !== 0) { | ||
pullQueue.shift()({ value, done: false }); | ||
} | ||
else { | ||
pushQueue.push({ value }); | ||
} | ||
}; | ||
const pushError = (error) => { | ||
if (pullQueue.length !== 0) { | ||
pullQueue.shift()({ value: { errors: [error] }, done: false }); | ||
} | ||
else { | ||
pushQueue.push({ value: { errors: [error] } }); | ||
} | ||
}; | ||
const pullValue = () => new Promise(resolve => { | ||
if (pushQueue.length !== 0) { | ||
const element = pushQueue.shift(); | ||
// either {value: {errors: [...]}} or {value: ...} | ||
resolve({ | ||
...element, | ||
done: false, | ||
}); | ||
} | ||
else { | ||
pullQueue.push(resolve); | ||
} | ||
}); | ||
const subscription = observable.subscribe({ | ||
next(value) { | ||
pushValue(value); | ||
}, | ||
error(err) { | ||
pushError(err); | ||
}, | ||
}); | ||
const emptyQueue = () => { | ||
if (listening) { | ||
listening = false; | ||
subscription.unsubscribe(); | ||
pullQueue.forEach(resolve => resolve({ value: undefined, done: true })); | ||
pullQueue.length = 0; | ||
pushQueue.length = 0; | ||
} | ||
}; | ||
return { | ||
next() { | ||
return listening ? pullValue() : this.return(); | ||
}, | ||
return() { | ||
emptyQueue(); | ||
return Promise.resolve({ value: undefined, done: true }); | ||
}, | ||
throw(error) { | ||
emptyQueue(); | ||
return Promise.reject(error); | ||
}, | ||
[Symbol.asyncIterator]() { | ||
return this; | ||
}, | ||
}; | ||
} | ||
const linkToSubscriber = (link) => async ({ document, variables, context, info, }) => observableToAsyncIterable(execute(link, { | ||
@@ -180,0 +113,0 @@ query: document, |
@@ -1,3 +0,3 @@ | ||
import { ApolloLink, FetchResult, DocumentNode } from 'apollo-link'; | ||
import { GraphQLResolveInfo } from 'graphql/type'; | ||
import { ApolloLink, FetchResult } from 'apollo-link'; | ||
import { DocumentNode, GraphQLResolveInfo } from 'graphql'; | ||
export declare const linkToExecutor: (link: ApolloLink) => <TReturn, TArgs, TContext>({ document, variables, context, info, }: { | ||
@@ -4,0 +4,0 @@ document: DocumentNode; |
{ | ||
"name": "@graphql-tools/links", | ||
"version": "6.0.2-alpha-bc3fdcf.0", | ||
"version": "6.0.2-alpha-bf587ef.0", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
33388
11
308