@@ -24,7 +24,7 @@ import {
24
24
assertValidExecutionArguments ,
25
25
buildExecutionContext ,
26
26
buildResolveInfo ,
27
- execute ,
27
+ executeQueryOrMutation ,
28
28
getFieldDef ,
29
29
} from './execute' ;
30
30
import { mapAsyncIterator } from './mapAsyncIterator' ;
@@ -64,26 +64,28 @@ export interface SubscriptionArgs extends ExecutionArgs {}
64
64
export async function subscribe (
65
65
args : SubscriptionArgs ,
66
66
) : Promise < AsyncGenerator < ExecutionResult , void , void > | ExecutionResult > {
67
- const {
68
- schema,
69
- document,
70
- rootValue,
71
- contextValue,
72
- variableValues,
73
- operationName,
74
- fieldResolver,
75
- subscribeFieldResolver,
76
- } = args ;
67
+ const { schema, document, variableValues } = args ;
77
68
78
- const resultOrStream = await createSourceEventStream (
79
- schema ,
80
- document ,
81
- rootValue ,
82
- contextValue ,
83
- variableValues ,
84
- operationName ,
85
- subscribeFieldResolver ,
86
- ) ;
69
+ // If arguments are missing or incorrectly typed, this is an internal
70
+ // developer mistake which should throw an early error.
71
+ assertValidExecutionArguments ( schema , document , variableValues ) ;
72
+
73
+ // If a valid execution context cannot be created due to incorrect arguments,
74
+ // a "Response" with only errors is returned.
75
+ const exeContext = buildExecutionContext ( args ) ;
76
+
77
+ // Return early errors if execution context failed.
78
+ if ( ! ( 'schema' in exeContext ) ) {
79
+ return { errors : exeContext } ;
80
+ }
81
+
82
+ return executeSubscription ( exeContext ) ;
83
+ }
84
+
85
+ async function executeSubscription (
86
+ exeContext : ExecutionContext ,
87
+ ) : Promise < AsyncGenerator < ExecutionResult , void , void > | ExecutionResult > {
88
+ const resultOrStream = await createSourceEventStreamImpl ( exeContext ) ;
87
89
88
90
if ( ! isAsyncIterable ( resultOrStream ) ) {
89
91
return resultOrStream ;
@@ -96,14 +98,10 @@ export async function subscribe(
96
98
// "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
97
99
// "ExecuteQuery" algorithm, for which `execute` is also used.
98
100
const mapSourceToResponse = ( payload : unknown ) =>
99
- execute ( {
100
- schema,
101
- document,
101
+ executeQueryOrMutation ( {
102
+ ...exeContext ,
102
103
rootValue : payload ,
103
- contextValue,
104
- variableValues,
105
- operationName,
106
- fieldResolver,
104
+ errors : [ ] ,
107
105
} ) ;
108
106
109
107
// Map every source value to a ExecutionResult value as described above.
@@ -168,6 +166,12 @@ export async function createSourceEventStream(
168
166
return { errors : exeContext } ;
169
167
}
170
168
169
+ return createSourceEventStreamImpl ( exeContext ) ;
170
+ }
171
+
172
+ export async function createSourceEventStreamImpl (
173
+ exeContext : ExecutionContext ,
174
+ ) : Promise < AsyncIterable < unknown > | ExecutionResult > {
171
175
try {
172
176
const eventStream = await executeSubscriptionRootField ( exeContext ) ;
173
177
0 commit comments