@@ -69,15 +69,15 @@ export async function subscribe(
69
69
subscribeFieldResolver,
70
70
} = args ;
71
71
72
- const resultOrStream = await createSourceEventStream (
72
+ const resultOrStream = await createSourceEventStream ( {
73
73
schema,
74
74
document,
75
75
rootValue,
76
76
contextValue,
77
77
variableValues,
78
78
operationName,
79
79
subscribeFieldResolver,
80
- ) ;
80
+ } ) ;
81
81
82
82
if ( ! isAsyncIterable ( resultOrStream ) ) {
83
83
return resultOrStream ;
@@ -91,8 +91,7 @@ export async function subscribe(
91
91
// "ExecuteQuery" algorithm, for which `execute` is also used.
92
92
const mapSourceToResponse = ( payload : unknown ) =>
93
93
execute ( {
94
- schema,
95
- document,
94
+ ...args ,
96
95
rootValue : payload ,
97
96
contextValue,
98
97
variableValues,
@@ -104,6 +103,36 @@ export async function subscribe(
104
103
return mapAsyncIterator ( resultOrStream , mapSourceToResponse ) ;
105
104
}
106
105
106
+ type BackwardsCompatibleArgs =
107
+ | [ options : ExecutionArgs ]
108
+ | [
109
+ schema : ExecutionArgs [ 'schema' ] ,
110
+ document : ExecutionArgs [ 'document' ] ,
111
+ rootValue ?: ExecutionArgs [ 'rootValue' ] ,
112
+ contextValue ?: ExecutionArgs [ 'contextValue' ] ,
113
+ variableValues ?: ExecutionArgs [ 'variableValues' ] ,
114
+ operationName ?: ExecutionArgs [ 'operationName' ] ,
115
+ subscribeFieldResolver ?: ExecutionArgs [ 'subscribeFieldResolver' ] ,
116
+ ] ;
117
+
118
+ function toNormalizedArgs ( args : BackwardsCompatibleArgs ) : ExecutionArgs {
119
+ const firstArg = args [ 0 ] ;
120
+ if ( firstArg && 'document' in firstArg ) {
121
+ return firstArg ;
122
+ }
123
+
124
+ return {
125
+ schema : firstArg ,
126
+ // FIXME: when underlying TS bug fixed, see https://github.com/microsoft/TypeScript/issues/31613
127
+ document : args [ 1 ] as DocumentNode ,
128
+ rootValue : args [ 2 ] ,
129
+ contextValue : args [ 3 ] ,
130
+ variableValues : args [ 4 ] ,
131
+ operationName : args [ 5 ] ,
132
+ subscribeFieldResolver : args [ 6 ] ,
133
+ } ;
134
+ }
135
+
107
136
/**
108
137
* Implements the "CreateSourceEventStream" algorithm described in the
109
138
* GraphQL specification, resolving the subscription source event stream.
@@ -132,6 +161,10 @@ export async function subscribe(
132
161
* or otherwise separating these two steps. For more on this, see the
133
162
* "Supporting Subscriptions at Scale" information in the GraphQL specification.
134
163
*/
164
+ export async function createSourceEventStream (
165
+ args : ExecutionArgs ,
166
+ ) : Promise < AsyncIterable < unknown > | ExecutionResult > ;
167
+ /** @deprecated will be removed in next major version in favor of named arguments */
135
168
export async function createSourceEventStream (
136
169
schema : GraphQLSchema ,
137
170
document : DocumentNode ,
@@ -140,22 +173,21 @@ export async function createSourceEventStream(
140
173
variableValues ?: Maybe < { readonly [ variable : string ] : unknown } > ,
141
174
operationName ?: Maybe < string > ,
142
175
subscribeFieldResolver ?: Maybe < GraphQLFieldResolver < any , any > > ,
143
- ) : Promise < AsyncIterable < unknown > | ExecutionResult > {
176
+ ) : Promise < AsyncIterable < unknown > | ExecutionResult > ;
177
+ export async function createSourceEventStream (
178
+ ...rawArgs : BackwardsCompatibleArgs
179
+ ) {
180
+ const args = toNormalizedArgs ( rawArgs ) ;
181
+
182
+ const { schema, document, variableValues } = args ;
183
+
144
184
// If arguments are missing or incorrectly typed, this is an internal
145
185
// developer mistake which should throw an early error.
146
186
assertValidExecutionArguments ( schema , document , variableValues ) ;
147
187
148
188
// If a valid execution context cannot be created due to incorrect arguments,
149
189
// a "Response" with only errors is returned.
150
- const exeContext = buildExecutionContext ( {
151
- schema,
152
- document,
153
- rootValue,
154
- contextValue,
155
- variableValues,
156
- operationName,
157
- subscribeFieldResolver,
158
- } ) ;
190
+ const exeContext = buildExecutionContext ( args ) ;
159
191
160
192
// Return early errors if execution context failed.
161
193
if ( ! ( 'schema' in exeContext ) ) {
0 commit comments