Skip to content

createSourceEventStream: remove deprecated positional argument overload #3635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions src/execution/__tests__/subscribe-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,49 +421,6 @@ describe('Subscription Initialization Phase', () => {
expect(() => subscribe({ schema })).to.throw('Must provide document.');
});

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

const schema = new GraphQLSchema({
query: DummyQueryType,
subscription: new GraphQLObjectType({
name: 'Subscription',
fields: {
foo: { type: GraphQLString, subscribe: fooGenerator },
},
}),
});
const document = parse('subscription { foo }');

const eventStream = createSourceEventStream(schema, document);
assert(isAsyncIterable(eventStream));
});

it('Deprecated: throws an error if document is missing when using positional arguments', async () => {
const schema = new GraphQLSchema({
query: DummyQueryType,
subscription: new GraphQLObjectType({
name: 'Subscription',
fields: {
foo: { type: GraphQLString },
},
}),
});

// @ts-expect-error
expect(() => createSourceEventStream(schema, null)).to.throw(
'Must provide document.',
);

// @ts-expect-error
expect(() => createSourceEventStream(schema)).to.throw(
'Must provide document.',
);
});

it('resolves to an error if schema does not support subscriptions', async () => {
const schema = new GraphQLSchema({ query: DummyQueryType });
const document = parse('subscription { unknownField }');
Expand Down
51 changes: 2 additions & 49 deletions src/execution/subscribe.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { inspect } from '../jsutils/inspect';
import { isAsyncIterable } from '../jsutils/isAsyncIterable';
import { isPromise } from '../jsutils/isPromise';
import type { Maybe } from '../jsutils/Maybe';
import { addPath, pathToArray } from '../jsutils/Path';
import type { PromiseOrValue } from '../jsutils/PromiseOrValue';

import { GraphQLError } from '../error/GraphQLError';
import { locatedError } from '../error/locatedError';

import type { DocumentNode } from '../language/ast';

import type { GraphQLFieldResolver } from '../type/definition';
import type { GraphQLSchema } from '../type/schema';

import { collectFields } from './collectFields';
import type {
ExecutionArgs,
Expand Down Expand Up @@ -89,36 +83,6 @@ function mapSourceToResponse(
);
}

type BackwardsCompatibleArgs =
| [options: ExecutionArgs]
| [
schema: ExecutionArgs['schema'],
document: ExecutionArgs['document'],
rootValue?: ExecutionArgs['rootValue'],
contextValue?: ExecutionArgs['contextValue'],
variableValues?: ExecutionArgs['variableValues'],
operationName?: ExecutionArgs['operationName'],
subscribeFieldResolver?: ExecutionArgs['subscribeFieldResolver'],
];

function toNormalizedArgs(args: BackwardsCompatibleArgs): ExecutionArgs {
const firstArg = args[0];
if ('document' in firstArg) {
return firstArg;
}

return {
schema: firstArg,
// FIXME: when underlying TS bug fixed, see https://github.com/microsoft/TypeScript/issues/31613
document: args[1] as DocumentNode,
rootValue: args[2],
contextValue: args[3],
variableValues: args[4],
operationName: args[5],
subscribeFieldResolver: args[6],
};
}

/**
* Implements the "CreateSourceEventStream" algorithm described in the
* GraphQL specification, resolving the subscription source event stream.
Expand Down Expand Up @@ -149,18 +113,7 @@ function toNormalizedArgs(args: BackwardsCompatibleArgs): ExecutionArgs {
*/
export function createSourceEventStream(
args: ExecutionArgs,
): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult>;
/** @deprecated will be removed in next major version in favor of named arguments */
export function createSourceEventStream(
schema: GraphQLSchema,
document: DocumentNode,
rootValue?: unknown,
contextValue?: unknown,
variableValues?: Maybe<{ readonly [variable: string]: unknown }>,
operationName?: Maybe<string>,
subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult>;
export function createSourceEventStream(...rawArgs: BackwardsCompatibleArgs) {
): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult> {
const {
schema,
document,
Expand All @@ -169,7 +122,7 @@ export function createSourceEventStream(...rawArgs: BackwardsCompatibleArgs) {
variableValues,
operationName,
subscribeFieldResolver,
} = toNormalizedArgs(rawArgs);
} = args;

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