Skip to content

Commit b667b3f

Browse files
committed
refactor: move package out of core + overwrite subscribe function.
1 parent 2bd41bd commit b667b3f

13 files changed

+258
-302
lines changed

packages/core/src/create.ts

Lines changed: 26 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
defaultFieldResolver,
33
DocumentNode,
44
execute,
5+
subscribe,
56
GraphQLError,
67
GraphQLFieldResolver,
78
GraphQLSchema,
@@ -18,16 +19,14 @@ import {
1819
AfterResolverPayload,
1920
Envelop,
2021
ExecuteFunction,
21-
OnExecuteSubscriptionEventHandler,
2222
OnResolverCalledHooks,
2323
Plugin,
2424
SubscribeFunction,
2525
} from '@envelop/types';
26-
import { subscribe } from './subscribe';
2726
import { makeSubscribe, makeExecute } from './util';
2827

2928
const trackedSchemaSymbol = Symbol('TRACKED_SCHEMA');
30-
const resolversHooksSymbol = Symbol('RESOLVERS_HOOKS');
29+
export const resolversHooksSymbol = Symbol('RESOLVERS_HOOKS');
3130

3231
export function envelop({ plugins }: { plugins: Plugin[] }): Envelop {
3332
let schema: GraphQLSchema | undefined | null = null;
@@ -220,7 +219,6 @@ export function envelop({ plugins }: { plugins: Plugin[] }): Envelop {
220219
result: AsyncIterableIterator<ExecutionResult> | ExecutionResult;
221220
setResult: (newResult: AsyncIterableIterator<ExecutionResult> | ExecutionResult) => void;
222221
}) => void)[] = [];
223-
const beforeExecuteSubscriptionHandlers: OnExecuteSubscriptionEventHandler[] = [];
224222
let context = args.contextValue;
225223

226224
for (const onSubscribe of onSubscribeCbs) {
@@ -251,29 +249,42 @@ export function envelop({ plugins }: { plugins: Plugin[] }): Envelop {
251249
if (after.onResolverCalled) {
252250
onResolversHandlers.push(after.onResolverCalled);
253251
}
254-
if (after.onExecuteSubscriptionEvent) {
255-
beforeExecuteSubscriptionHandlers.push(after.onExecuteSubscriptionEvent);
256-
}
257252
}
258253
}
259254

260255
if (onResolversHandlers.length) {
261256
context[resolversHooksSymbol] = onResolversHandlers;
262257
}
263258

264-
const subscribeExecute = beforeExecuteSubscriptionHandlers.length
259+
let result = await subscribeFn({
260+
...args,
261+
contextValue: context,
262+
});
263+
264+
for (const afterCb of afterCalls) {
265+
afterCb({
266+
result,
267+
setResult: newResult => {
268+
result = newResult;
269+
},
270+
});
271+
}
272+
273+
return result;
274+
});
275+
276+
const customExecute = (
277+
onExecuteCbs.length
265278
? makeExecute(async args => {
266279
const onResolversHandlers: OnResolverCalledHooks[] = [];
267280
let executeFn: ExecuteFunction = execute as ExecuteFunction;
268281
let result: ExecutionResult;
269282

270-
const afterCalls: ((options: {
271-
result: ExecutionResult;
272-
setResult: (newResult: ExecutionResult) => void;
273-
}) => void)[] = [];
283+
const afterCalls: ((options: { result: ExecutionResult; setResult: (newResult: ExecutionResult) => void }) => void)[] =
284+
[];
274285
let context = args.contextValue;
275286

276-
for (const onExecute of beforeExecuteSubscriptionHandlers) {
287+
for (const onExecute of onExecuteCbs) {
277288
let stopCalled = false;
278289

279290
const after = onExecute({
@@ -336,102 +347,8 @@ export function envelop({ plugins }: { plugins: Plugin[] }): Envelop {
336347

337348
return result;
338349
})
339-
: ((args.execute ?? execute) as ExecuteFunction);
340-
341-
let result = await subscribeFn({
342-
...args,
343-
contextValue: context,
344-
execute: subscribeExecute,
345-
});
346-
347-
for (const afterCb of afterCalls) {
348-
afterCb({
349-
result,
350-
setResult: newResult => {
351-
result = newResult;
352-
},
353-
});
354-
}
355-
356-
return result;
357-
});
358-
359-
const customExecute = (onExecuteCbs.length
360-
? makeExecute(async args => {
361-
const onResolversHandlers: OnResolverCalledHooks[] = [];
362-
let executeFn: ExecuteFunction = execute as ExecuteFunction;
363-
let result: ExecutionResult;
364-
365-
const afterCalls: ((options: {
366-
result: ExecutionResult;
367-
setResult: (newResult: ExecutionResult) => void;
368-
}) => void)[] = [];
369-
let context = args.contextValue;
370-
371-
for (const onExecute of onExecuteCbs) {
372-
let stopCalled = false;
373-
374-
const after = onExecute({
375-
executeFn,
376-
setExecuteFn: newExecuteFn => {
377-
executeFn = newExecuteFn;
378-
},
379-
setResultAndStopExecution: stopResult => {
380-
stopCalled = true;
381-
result = stopResult;
382-
},
383-
extendContext: extension => {
384-
if (typeof extension === 'object') {
385-
context = {
386-
...(context || {}),
387-
...extension,
388-
};
389-
} else {
390-
throw new Error(
391-
`Invalid context extension provided! Expected "object", got: "${JSON.stringify(
392-
extension
393-
)}" (${typeof extension})`
394-
);
395-
}
396-
},
397-
args,
398-
});
399-
400-
if (stopCalled) {
401-
return result!;
402-
}
403-
404-
if (after) {
405-
if (after.onExecuteDone) {
406-
afterCalls.push(after.onExecuteDone);
407-
}
408-
if (after.onResolverCalled) {
409-
onResolversHandlers.push(after.onResolverCalled);
410-
}
411-
}
412-
}
413-
414-
if (onResolversHandlers.length) {
415-
context[resolversHooksSymbol] = onResolversHandlers;
416-
}
417-
418-
result = await executeFn({
419-
...args,
420-
contextValue: context,
421-
});
422-
423-
for (const afterCb of afterCalls) {
424-
afterCb({
425-
result,
426-
setResult: newResult => {
427-
result = newResult;
428-
},
429-
});
430-
}
431-
432-
return result;
433-
})
434-
: execute) as ExecuteFunction;
350+
: execute
351+
) as ExecuteFunction;
435352

436353
function prepareSchema() {
437354
if (!schema || schema[trackedSchemaSymbol]) {

packages/core/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ export * from './plugins/use-error-handler';
88
export * from './plugins/use-extend-context';
99
export * from './plugins/use-payload-formatter';
1010
export * from './plugins/use-masked-errors';
11-
export * from './plugins/use-context-per-subscription-event';

packages/core/src/plugins/use-context-per-subscription-event.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

packages/core/src/subscribe.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

packages/core/src/util.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { PolymorphicExecuteArguments, PolymorphicSubscribeArguments, SubscriptionArgs } from '@envelop/types';
2-
import { ExecutionArgs, ExecutionResult } from 'graphql';
1+
import { PolymorphicExecuteArguments, PolymorphicSubscribeArguments } from '@envelop/types';
2+
import { ExecutionArgs, ExecutionResult, SubscriptionArgs } from 'graphql';
33
import { PromiseOrValue } from 'graphql/jsutils/PromiseOrValue';
44

55
export function getExecuteArgs(args: PolymorphicExecuteArguments): ExecutionArgs {
@@ -20,9 +20,10 @@ export function getExecuteArgs(args: PolymorphicExecuteArguments): ExecutionArgs
2020
/**
2121
* Utility function for making a execute function that handles polymorphic arguments.
2222
*/
23-
export const makeExecute = (subscribeFn: (args: ExecutionArgs) => PromiseOrValue<ExecutionResult>) => (
24-
...polyArgs: PolymorphicExecuteArguments
25-
): PromiseOrValue<ExecutionResult> => subscribeFn(getExecuteArgs(polyArgs));
23+
export const makeExecute =
24+
(executeFn: (args: ExecutionArgs) => PromiseOrValue<ExecutionResult>) =>
25+
(...polyArgs: PolymorphicExecuteArguments): PromiseOrValue<ExecutionResult> =>
26+
executeFn(getExecuteArgs(polyArgs));
2627

2728
export function getSubscribeArgs(args: PolymorphicSubscribeArguments): SubscriptionArgs {
2829
return args.length === 1
@@ -36,14 +37,13 @@ export function getSubscribeArgs(args: PolymorphicSubscribeArguments): Subscript
3637
operationName: args[5],
3738
fieldResolver: args[6],
3839
subscribeFieldResolver: args[7],
39-
execute: args[8],
4040
};
4141
}
4242

4343
/**
4444
* Utility function for making a subscribe function that handles polymorphic arguments.
4545
*/
46-
export const makeSubscribe = (
47-
subscribeFn: (args: SubscriptionArgs) => PromiseOrValue<AsyncIterableIterator<ExecutionResult> | ExecutionResult>
48-
) => (...polyArgs: PolymorphicSubscribeArguments): PromiseOrValue<AsyncIterableIterator<ExecutionResult> | ExecutionResult> =>
49-
subscribeFn(getSubscribeArgs(polyArgs));
46+
export const makeSubscribe =
47+
(subscribeFn: (args: SubscriptionArgs) => PromiseOrValue<AsyncIterableIterator<ExecutionResult> | ExecutionResult>) =>
48+
(...polyArgs: PolymorphicSubscribeArguments): PromiseOrValue<AsyncIterableIterator<ExecutionResult> | ExecutionResult> =>
49+
subscribeFn(getSubscribeArgs(polyArgs));

0 commit comments

Comments
 (0)