@@ -54,60 +54,20 @@ export function subscribe(
54
54
) : PromiseOrValue <
55
55
AsyncGenerator < ExecutionResult , void , void > | ExecutionResult
56
56
> {
57
- const {
58
- schema,
59
- document,
60
- rootValue,
61
- contextValue,
62
- variableValues,
63
- operationName,
64
- fieldResolver,
65
- subscribeFieldResolver,
66
- } = args ;
67
-
68
- const resultOrStream = createSourceEventStream (
69
- schema ,
70
- document ,
71
- rootValue ,
72
- contextValue ,
73
- variableValues ,
74
- operationName ,
75
- subscribeFieldResolver ,
76
- ) ;
57
+ const resultOrStream = createSourceEventStream ( args ) ;
77
58
78
59
if ( isPromise ( resultOrStream ) ) {
79
60
return resultOrStream . then ( ( resolvedResultOrStream ) =>
80
- mapSourceToResponse (
81
- schema ,
82
- document ,
83
- resolvedResultOrStream ,
84
- contextValue ,
85
- variableValues ,
86
- operationName ,
87
- fieldResolver ,
88
- ) ,
61
+ mapSourceToResponse ( resolvedResultOrStream , args ) ,
89
62
) ;
90
63
}
91
64
92
- return mapSourceToResponse (
93
- schema ,
94
- document ,
95
- resultOrStream ,
96
- contextValue ,
97
- variableValues ,
98
- operationName ,
99
- fieldResolver ,
100
- ) ;
65
+ return mapSourceToResponse ( resultOrStream , args ) ;
101
66
}
102
67
103
68
function mapSourceToResponse (
104
- schema : GraphQLSchema ,
105
- document : DocumentNode ,
106
69
resultOrStream : ExecutionResult | AsyncIterable < unknown > ,
107
- contextValue ?: unknown ,
108
- variableValues ?: Maybe < { readonly [ variable : string ] : unknown } > ,
109
- operationName ?: Maybe < string > ,
110
- fieldResolver ?: Maybe < GraphQLFieldResolver < any , any > > ,
70
+ args : ExecutionArgs ,
111
71
) : PromiseOrValue <
112
72
AsyncGenerator < ExecutionResult , void , void > | ExecutionResult
113
73
> {
@@ -123,17 +83,42 @@ function mapSourceToResponse(
123
83
// "ExecuteQuery" algorithm, for which `execute` is also used.
124
84
return mapAsyncIterator ( resultOrStream , ( payload : unknown ) =>
125
85
execute ( {
126
- schema,
127
- document,
86
+ ...args ,
128
87
rootValue : payload ,
129
- contextValue,
130
- variableValues,
131
- operationName,
132
- fieldResolver,
133
88
} ) ,
134
89
) ;
135
90
}
136
91
92
+ type BackwardsCompatibleArgs =
93
+ | [ options : ExecutionArgs ]
94
+ | [
95
+ schema : ExecutionArgs [ 'schema' ] ,
96
+ document : ExecutionArgs [ 'document' ] ,
97
+ rootValue ?: ExecutionArgs [ 'rootValue' ] ,
98
+ contextValue ?: ExecutionArgs [ 'contextValue' ] ,
99
+ variableValues ?: ExecutionArgs [ 'variableValues' ] ,
100
+ operationName ?: ExecutionArgs [ 'operationName' ] ,
101
+ subscribeFieldResolver ?: ExecutionArgs [ 'subscribeFieldResolver' ] ,
102
+ ] ;
103
+
104
+ function toNormalizedArgs ( args : BackwardsCompatibleArgs ) : ExecutionArgs {
105
+ const firstArg = args [ 0 ] ;
106
+ if ( 'document' in firstArg ) {
107
+ return firstArg ;
108
+ }
109
+
110
+ return {
111
+ schema : firstArg ,
112
+ // FIXME: when underlying TS bug fixed, see https://github.com/microsoft/TypeScript/issues/31613
113
+ document : args [ 1 ] as DocumentNode ,
114
+ rootValue : args [ 2 ] ,
115
+ contextValue : args [ 3 ] ,
116
+ variableValues : args [ 4 ] ,
117
+ operationName : args [ 5 ] ,
118
+ subscribeFieldResolver : args [ 6 ] ,
119
+ } ;
120
+ }
121
+
137
122
/**
138
123
* Implements the "CreateSourceEventStream" algorithm described in the
139
124
* GraphQL specification, resolving the subscription source event stream.
@@ -162,6 +147,10 @@ function mapSourceToResponse(
162
147
* or otherwise separating these two steps. For more on this, see the
163
148
* "Supporting Subscriptions at Scale" information in the GraphQL specification.
164
149
*/
150
+ export function createSourceEventStream (
151
+ args : ExecutionArgs ,
152
+ ) : PromiseOrValue < AsyncIterable < unknown > | ExecutionResult > ;
153
+ /** @deprecated will be removed in next major version in favor of named arguments */
165
154
export function createSourceEventStream (
166
155
schema : GraphQLSchema ,
167
156
document : DocumentNode ,
@@ -170,7 +159,18 @@ export function createSourceEventStream(
170
159
variableValues ?: Maybe < { readonly [ variable : string ] : unknown } > ,
171
160
operationName ?: Maybe < string > ,
172
161
subscribeFieldResolver ?: Maybe < GraphQLFieldResolver < any , any > > ,
173
- ) : PromiseOrValue < AsyncIterable < unknown > | ExecutionResult > {
162
+ ) : PromiseOrValue < AsyncIterable < unknown > | ExecutionResult > ;
163
+ export function createSourceEventStream ( ...rawArgs : BackwardsCompatibleArgs ) {
164
+ const {
165
+ schema,
166
+ document,
167
+ rootValue,
168
+ contextValue,
169
+ variableValues,
170
+ operationName,
171
+ subscribeFieldResolver,
172
+ } = toNormalizedArgs ( rawArgs ) ;
173
+
174
174
// If arguments are missing or incorrectly typed, this is an internal
175
175
// developer mistake which should throw an early error.
176
176
assertValidExecutionArguments ( schema , document , variableValues ) ;
0 commit comments