Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
before(async () => {
mockRestRequest("product", "402-5806");
fetchMock.spy();
try {
const asyncIterator = await client.request(
ecom.requests.singleSubscription,
{ awaitDataCached: true },
);
if (isAsyncIterable(asyncIterator)) {
forAwaitEach(asyncIterator, (value) => {
result = value as RequestResultData;
const deferredValue = deferred[0] as DeferPromise.Deferred;
deferredValue.resolve();
});
}
} catch (error) {
console.log(error); // tslint:disable-line
}
});
var debug = require('debug')('primus-graphql:fixtures:user-changes-iterator')
var iterall = require('iterall')
var db = require('./mem-db.js') // In memory database
var $$asyncIterator = iterall.$$asyncIterator
var i = 0
function createExposedPromise () {
var _resolve, _reject
var promise = new Promise(function (resolve, reject) {
_resolve = resolve
_reject = reject
})
promise.id = ++i
promise.resolve = function (data) {
_resolve(data)
promise.val = data
return promise
}
promise.reject = function (err) {
_reject(err)
const req = proto.request(opts, res => {
let data = '';
const contentType = res.headers['content-type'];
const types =
typeof contentType === 'string'
? contentType.split(';').map(h => h.trim().toLowerCase())
: [];
const iterator = AsyncIterator.fromStream(res, {
closeEvent: 'end',
});
// iterate through the data stream
return forAwaitEach(iterator, value => {
data += value;
})
.then(() => {
clearTimeout(timeout);
if (types.indexOf('application/json') !== -1) {
try {
resolve(JSON.parse(data));
} catch (parseErr) {
clearTimeout(timeout);
return reject(parseErr);
}
}
})
.catch(err => {
clearTimeout(timeout);
return reject(err);
schema: options.schema,
event: {} as any, // we don't have api gateway event here
lambdaContext: lambdaContext as any, // we don't have a lambda's context here
context: options.context,
connection: subscriber.connection,
operation: subscriber.operation,
pubSub,
registerSubscriptions: false,
});
if (!isAsyncIterable(iterable)) {
// something went wrong, probably there is an error
return Promise.resolve();
}
const iterator = getAsyncIterator(iterable);
const result: IteratorResult = await iterator.next();
if (result.value != null) {
return connectionManager.sendToConnection(
subscriber.connection,
formatMessage({
id: subscriber.operationId,
payload: result.value,
type: SERVER_EVENT_TYPES.GQL_DATA,
}),
);
}
return Promise.resolve();
})
.map(promise => promise.catch(e => console.log(e)));
[name]: value,
};
}, {});
const C = isContextFn(this.sofa.context)
? await this.sofa.context({ req, res })
: this.sofa.context;
const execution = await subscribe({
schema: this.sofa.schema,
document,
operationName,
variableValues,
contextValue: C,
});
if (isAsyncIterable(execution)) {
// successful
// add execution to clients
this.clients.set(id, {
name,
url,
iterator: execution as any,
});
// success
forAwaitEach(execution, async result => {
await this.sendData({
id,
result,
});
}).then(
return sourcePromise.then(function (resultOrStream) {
return (
// Note: Flow can't refine isAsyncIterable, so explicit casts are used.
isAsyncIterable(resultOrStream) ? mapAsyncIterator(resultOrStream, mapSourceToResponse, reportGraphQLError) : resultOrStream
);
}, reportGraphQLError);
}
);
const result = await execute({
...options,
connection,
connectionManager: this.connectionManager,
event,
lambdaContext,
onOperation,
operation,
pubSub,
// tell execute to register subscriptions
registerSubscriptions: true,
subscriptionManager: this.subscriptionManager,
});
if (!isAsyncIterable(result)) {
// send response to client so it can finish operation in case of query or mutation
if (onOperationComplete) {
onOperationComplete(
connection,
(operation as IdentifiedOperationRequest).operationId,
);
}
const response = formatMessage({
id: (operation as IdentifiedOperationRequest).operationId,
payload: result as ExecutionResult,
type: SERVER_EVENT_TYPES.GQL_DATA,
});
await this.connectionManager.sendToConnection(
connection,
response,
);
if (isAsyncIterable(result) && useLegacyProtocol) {
// if result is async iterator, then it means that subscriptions was registered
// legacy protocol requires that GQL_SUBSCRIBED should be sent back
const response = formatMessage({
id: (operation as IdentifiedOperationRequest).operationId,
payload: {},
type: SERVER_EVENT_TYPES.GQL_SUBSCRIBED,
});
await connectionManager.sendToConnection(connection, response);
return {
body: response,
statusCode: 200,
};
}
if (!isAsyncIterable(result)) {
// send response to client so it can finish operation in case of query or mutation
if (onOperationComplete) {
onOperationComplete(
connection,
(operation as IdentifiedOperationRequest).operationId,
);
}
const response = formatMessage({
id: (operation as IdentifiedOperationRequest).operationId,
payload: result as ExecutionResult,
type: SERVER_EVENT_TYPES.GQL_DATA,
});
await connectionManager.sendToConnection(connection, response);
return {
body: response,
statusCode: 200,
function completeListValue(
exeContext: ExecutionContext,
returnType: GraphQLList<*>,
fieldNodes: Array,
info: GraphQLResolveInfo,
path: ResponsePath,
result: mixed
): mixed {
invariant(
isCollection(result),
`Expected Iterable, but did not find one for field ${
info.parentType.name}.${info.fieldName}.`
);
// This is specified as a simple map, however we're optimizing the path
// where the list contains no Promises by avoiding creating another Promise.
const itemType = returnType.ofType;
let containsPromise = false;
const completedResults = [];
forEach((result: any), (item, index) => {
// No need to modify the info object containing the path,
// since from here on it is not ever accessed by resolver functions.
const fieldPath = addPath(path, index);
const completedItem = completeValueCatchingError(
exeContext,
itemType,
function completeListValue(
exeContext: ExecutionContext,
returnType: GraphQLList,
fieldNodes: ReadonlyArray,
info: GraphQLResolveInfo,
path: ResponsePath,
result: {},
): MaybePromise> {
invariant(
isCollection(result),
`Expected Iterable, but did not find one for field ${
info.parentType.name
}.${info.fieldName}.`,
);
// This is specified as a simple map, however we're optimizing the path
// where the list contains no Promises by avoiding creating another Promise.
const itemType = returnType.ofType;
let containsPromise = false;
const completedResults = [];
forEach(result as any, (item, index) => {
// No need to modify the info object containing the path,
// since from here on it is not ever accessed by resolver functions.
const fieldPath = addPath(path, index);
const completedItem = completeValueCatchingError(
exeContext,