@@ -58,26 +58,7 @@ export async function subscribe(
58
58
'graphql@16 dropped long-deprecated support for positional arguments, please pass an object instead.' ,
59
59
) ;
60
60
61
- const {
62
- schema,
63
- document,
64
- rootValue,
65
- contextValue,
66
- variableValues,
67
- operationName,
68
- fieldResolver,
69
- subscribeFieldResolver,
70
- } = args ;
71
-
72
- const resultOrStream = await createSourceEventStream (
73
- schema ,
74
- document ,
75
- rootValue ,
76
- contextValue ,
77
- variableValues ,
78
- operationName ,
79
- subscribeFieldResolver ,
80
- ) ;
61
+ const resultOrStream = await createSourceEventStream ( args ) ;
81
62
82
63
if ( ! isAsyncIterable ( resultOrStream ) ) {
83
64
return resultOrStream ;
@@ -91,19 +72,44 @@ export async function subscribe(
91
72
// "ExecuteQuery" algorithm, for which `execute` is also used.
92
73
const mapSourceToResponse = ( payload : unknown ) =>
93
74
execute ( {
94
- schema,
95
- document,
75
+ ...args ,
96
76
rootValue : payload ,
97
- contextValue,
98
- variableValues,
99
- operationName,
100
- fieldResolver,
101
77
} ) ;
102
78
103
79
// Map every source value to a ExecutionResult value as described above.
104
80
return mapAsyncIterator ( resultOrStream , mapSourceToResponse ) ;
105
81
}
106
82
83
+ type BackwardsCompatibleArgs =
84
+ | [ options : ExecutionArgs ]
85
+ | [
86
+ schema : ExecutionArgs [ 'schema' ] ,
87
+ document : ExecutionArgs [ 'document' ] ,
88
+ rootValue ?: ExecutionArgs [ 'rootValue' ] ,
89
+ contextValue ?: ExecutionArgs [ 'contextValue' ] ,
90
+ variableValues ?: ExecutionArgs [ 'variableValues' ] ,
91
+ operationName ?: ExecutionArgs [ 'operationName' ] ,
92
+ subscribeFieldResolver ?: ExecutionArgs [ 'subscribeFieldResolver' ] ,
93
+ ] ;
94
+
95
+ function toNormalizedArgs ( args : BackwardsCompatibleArgs ) : ExecutionArgs {
96
+ const firstArg = args [ 0 ] ;
97
+ if ( firstArg && 'document' in firstArg ) {
98
+ return firstArg ;
99
+ }
100
+
101
+ return {
102
+ schema : firstArg ,
103
+ // FIXME: when underlying TS bug fixed, see https://github.com/microsoft/TypeScript/issues/31613
104
+ document : args [ 1 ] as DocumentNode ,
105
+ rootValue : args [ 2 ] ,
106
+ contextValue : args [ 3 ] ,
107
+ variableValues : args [ 4 ] ,
108
+ operationName : args [ 5 ] ,
109
+ subscribeFieldResolver : args [ 6 ] ,
110
+ } ;
111
+ }
112
+
107
113
/**
108
114
* Implements the "CreateSourceEventStream" algorithm described in the
109
115
* GraphQL specification, resolving the subscription source event stream.
@@ -132,6 +138,10 @@ export async function subscribe(
132
138
* or otherwise separating these two steps. For more on this, see the
133
139
* "Supporting Subscriptions at Scale" information in the GraphQL specification.
134
140
*/
141
+ export async function createSourceEventStream (
142
+ args : ExecutionArgs ,
143
+ ) : Promise < AsyncIterable < unknown > | ExecutionResult > ;
144
+ /** @deprecated will be removed in next major version in favor of named arguments */
135
145
export async function createSourceEventStream (
136
146
schema : GraphQLSchema ,
137
147
document : DocumentNode ,
@@ -140,22 +150,21 @@ export async function createSourceEventStream(
140
150
variableValues ?: Maybe < { readonly [ variable : string ] : unknown } > ,
141
151
operationName ?: Maybe < string > ,
142
152
subscribeFieldResolver ?: Maybe < GraphQLFieldResolver < any , any > > ,
143
- ) : Promise < AsyncIterable < unknown > | ExecutionResult > {
153
+ ) : Promise < AsyncIterable < unknown > | ExecutionResult > ;
154
+ export async function createSourceEventStream (
155
+ ...rawArgs : BackwardsCompatibleArgs
156
+ ) {
157
+ const args = toNormalizedArgs ( rawArgs ) ;
158
+
159
+ const { schema, document, variableValues } = args ;
160
+
144
161
// If arguments are missing or incorrectly typed, this is an internal
145
162
// developer mistake which should throw an early error.
146
163
assertValidExecutionArguments ( schema , document , variableValues ) ;
147
164
148
165
// If a valid execution context cannot be created due to incorrect arguments,
149
166
// 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
- } ) ;
167
+ const exeContext = buildExecutionContext ( args ) ;
159
168
160
169
// Return early errors if execution context failed.
161
170
if ( ! ( 'schema' in exeContext ) ) {
0 commit comments