Skip to content

Commit d072fe3

Browse files
committed
refactor and invoke onDone always
1 parent 2e2127f commit d072fe3

File tree

2 files changed

+72
-43
lines changed

2 files changed

+72
-43
lines changed

packages/core/src/orchestrator.ts

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,8 @@ export function createEnvelopOrchestrator<PluginsContext extends DefaultContext>
572572
return async (params, contextExtension) => {
573573
const context = await contextFactory(contextExtension);
574574

575-
let result: AsyncIterableIteratorOrValue<ExecutionResult> | null = null;
576-
577-
const doneFns: OnPerformDoneHook[] = [];
575+
let earlyResult: AsyncIterableIteratorOrValue<ExecutionResult> | null = null;
576+
const onDones: OnPerformDoneHook[] = [];
578577
for (const onPerform of beforeCallbacks.perform) {
579578
const after = await onPerform({
580579
context,
@@ -585,59 +584,59 @@ export function createEnvelopOrchestrator<PluginsContext extends DefaultContext>
585584
setParams: newParams => {
586585
params = newParams;
587586
},
588-
setResult: earlyResult => {
589-
result = earlyResult;
587+
setResult: result => {
588+
earlyResult = result;
590589
},
591590
});
591+
after?.onPerformDone && onDones.push(after.onPerformDone);
592+
}
593+
const done = (result: AsyncIterableIteratorOrValue<ExecutionResult>) => {
594+
for (const onDone of onDones) {
595+
onDone({
596+
result,
597+
setResult: newResult => {
598+
result = newResult;
599+
},
600+
});
601+
}
602+
return result;
603+
};
592604

593-
after?.onPerformDone && doneFns.push(after.onPerformDone);
605+
if (earlyResult) {
606+
return done(earlyResult);
594607
}
595608

596-
if (!result) {
597-
let document;
598-
try {
599-
document = parse(params.query);
600-
} catch (err) {
601-
return { errors: [err] };
602-
}
609+
let document;
610+
try {
611+
document = parse(params.query);
612+
} catch (err) {
613+
return done({ errors: [err] });
614+
}
603615

604-
const validationErrors = validate(schema, document);
605-
if (validationErrors.length) {
606-
return { errors: validationErrors };
607-
}
616+
const validationErrors = validate(schema, document);
617+
if (validationErrors.length) {
618+
return done({ errors: validationErrors });
619+
}
608620

609-
if (isSubscriptionOperation(document, params.operationName)) {
610-
result = await customSubscribe({
621+
if (isSubscriptionOperation(document, params.operationName)) {
622+
return done(
623+
await customSubscribe({
611624
document,
612625
schema,
613626
variableValues: params.variables,
614627
contextValue: context,
615-
});
616-
} else {
617-
result = await customExecute({
618-
document,
619-
schema,
620-
variableValues: params.variables,
621-
contextValue: context,
622-
});
623-
}
628+
})
629+
);
624630
}
625631

626-
if (!result) {
627-
// should never happen
628-
throw new Error('Result not available');
629-
}
630-
631-
for (const doneFn of doneFns) {
632-
doneFn({
633-
result,
634-
setResult: newResult => {
635-
result = newResult;
636-
},
637-
});
638-
}
639-
640-
return result;
632+
return done(
633+
await customExecute({
634+
document,
635+
schema,
636+
variableValues: params.variables,
637+
contextValue: context,
638+
})
639+
);
641640
};
642641
};
643642

packages/core/test/perform.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,34 @@ describe('perform', () => {
211211

212212
expect(result).toBe(earlyResult);
213213
});
214+
215+
it('should provide result with parsing errors to onPerformDone hook', async () => {
216+
const onPerformDoneFn = jest.fn((() => {
217+
// noop
218+
}) as OnPerformDoneHook);
219+
220+
const getEnveloped = envelop({
221+
...graphqlFuncs,
222+
plugins: [
223+
useSchema(schema),
224+
{
225+
onPerform: () => ({
226+
onPerformDone: onPerformDoneFn,
227+
}),
228+
},
229+
],
230+
});
231+
232+
const { perform } = getEnveloped();
233+
await perform({ query: '{' });
234+
235+
expect(onPerformDoneFn).toBeCalled();
236+
expect(onPerformDoneFn.mock.calls[0][0].result).toMatchInlineSnapshot(`
237+
Object {
238+
"errors": Array [
239+
[GraphQLError: Syntax Error: Expected Name, found <EOF>.],
240+
],
241+
}
242+
`);
243+
});
214244
});

0 commit comments

Comments
 (0)