Skip to content

Commit 120ae79

Browse files
committed
apply feedback
1 parent e8a2a74 commit 120ae79

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

src/execution/__tests__/subscribe-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ describe('Subscription Initialization Phase', () => {
421421
expect(() => subscribe({ schema })).to.throw('Must provide document.');
422422
});
423423

424-
it('allows positional arguments to createSourceEventStream', () => {
424+
it('Deprecated: allows positional arguments to createSourceEventStream', () => {
425425
async function* fooGenerator() {
426426
/* c8 ignore next 2 */
427427
yield { foo: 'FooValue' };

src/execution/subscribe.ts

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import type { DocumentNode } from '../language/ast';
1313

1414
import type { GraphQLFieldResolver } from '../type/definition';
1515
import type { GraphQLSchema } from '../type/schema';
16-
import { isSchema } from '../type/schema';
1716

1817
import { collectFields } from './collectFields';
1918
import type {
@@ -91,6 +90,37 @@ function mapSourceToResponse(
9190
);
9291
}
9392

93+
type BackwardsCompatibleArgs =
94+
| [options: ExecutionArgs]
95+
| [
96+
schema: ExecutionArgs['schema'],
97+
document: ExecutionArgs['document'],
98+
rootValue?: ExecutionArgs['rootValue'],
99+
contextValue?: ExecutionArgs['contextValue'],
100+
variableValues?: ExecutionArgs['variableValues'],
101+
operationName?: ExecutionArgs['operationName'],
102+
subscribeFieldResolver?: ExecutionArgs['subscribeFieldResolver'],
103+
];
104+
105+
function toNormalizedArgs(args: BackwardsCompatibleArgs): ExecutionArgs {
106+
const firstArg = args[0];
107+
if ('document' in firstArg) {
108+
return firstArg;
109+
}
110+
111+
invariant(args[1] != null, 'Must provide document.');
112+
113+
return {
114+
schema: firstArg,
115+
document: args[1],
116+
rootValue: args[2],
117+
contextValue: args[3],
118+
variableValues: args[4],
119+
operationName: args[5],
120+
subscribeFieldResolver: args[6],
121+
};
122+
}
123+
94124
/**
95125
* Implements the "CreateSourceEventStream" algorithm described in the
96126
* GraphQL specification, resolving the subscription source event stream.
@@ -132,34 +162,7 @@ export function createSourceEventStream(
132162
operationName?: Maybe<string>,
133163
subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
134164
): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult>;
135-
export function createSourceEventStream(
136-
argsOrSchema: ExecutionArgs | GraphQLSchema,
137-
document?: DocumentNode,
138-
rootValue?: unknown,
139-
contextValue?: unknown,
140-
variableValues?: Maybe<{ readonly [variable: string]: unknown }>,
141-
operationName?: Maybe<string>,
142-
subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
143-
): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult> {
144-
if (isSchema(argsOrSchema)) {
145-
invariant(document != null, 'Must provide document.');
146-
return createSourceEventStreamImpl({
147-
schema: argsOrSchema,
148-
document,
149-
rootValue,
150-
contextValue,
151-
variableValues,
152-
operationName,
153-
subscribeFieldResolver,
154-
});
155-
}
156-
157-
return createSourceEventStreamImpl(argsOrSchema);
158-
}
159-
160-
export function createSourceEventStreamImpl(
161-
args: ExecutionArgs,
162-
): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult> {
165+
export function createSourceEventStream(...rawArgs: BackwardsCompatibleArgs) {
163166
const {
164167
schema,
165168
document,
@@ -168,7 +171,7 @@ export function createSourceEventStreamImpl(
168171
variableValues,
169172
operationName,
170173
subscribeFieldResolver,
171-
} = args;
174+
} = toNormalizedArgs(rawArgs);
172175

173176
// If arguments are missing or incorrectly typed, this is an internal
174177
// developer mistake which should throw an early error.

0 commit comments

Comments
 (0)