Skip to content
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

createSourceEventStream: introduce named arguments and deprecate positional arguments #3634

Merged
merged 5 commits into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 43 additions & 1 deletion src/execution/__tests__/subscribe-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function subscribeWithBadFn(

return expectEqualPromisesOrValues(
subscribe({ schema, document }),
createSourceEventStream(schema, document),
createSourceEventStream({ schema, document }),
);
}

Expand Down Expand Up @@ -421,6 +421,48 @@ describe('Subscription Initialization Phase', () => {
expect(() => subscribe({ schema })).to.throw('Must provide document.');
});

it('allows positional arguments to createSourceEventStream', () => {
yaacovCR marked this conversation as resolved.
Show resolved Hide resolved
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('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.',
);

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
97 changes: 45 additions & 52 deletions src/execution/subscribe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { inspect } from '../jsutils/inspect';
import { invariant } from '../jsutils/invariant';
import { isAsyncIterable } from '../jsutils/isAsyncIterable';
import { isPromise } from '../jsutils/isPromise';
import type { Maybe } from '../jsutils/Maybe';
Expand All @@ -12,6 +13,7 @@ import type { DocumentNode } from '../language/ast';

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

import { collectFields } from './collectFields';
import type {
Expand Down Expand Up @@ -54,60 +56,20 @@ export function subscribe(
): PromiseOrValue<
AsyncGenerator<ExecutionResult, void, void> | ExecutionResult
> {
const {
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
subscribeFieldResolver,
} = args;

const resultOrStream = createSourceEventStream(
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
subscribeFieldResolver,
);
const resultOrStream = createSourceEventStream(args);

if (isPromise(resultOrStream)) {
return resultOrStream.then((resolvedResultOrStream) =>
mapSourceToResponse(
schema,
document,
resolvedResultOrStream,
contextValue,
variableValues,
operationName,
fieldResolver,
),
mapSourceToResponse(resolvedResultOrStream, args),
);
}

return mapSourceToResponse(
schema,
document,
resultOrStream,
contextValue,
variableValues,
operationName,
fieldResolver,
);
return mapSourceToResponse(resultOrStream, args);
}

function mapSourceToResponse(
schema: GraphQLSchema,
document: DocumentNode,
resultOrStream: ExecutionResult | AsyncIterable<unknown>,
contextValue?: unknown,
variableValues?: Maybe<{ readonly [variable: string]: unknown }>,
operationName?: Maybe<string>,
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
args: ExecutionArgs,
): PromiseOrValue<
AsyncGenerator<ExecutionResult, void, void> | ExecutionResult
> {
Expand All @@ -123,13 +85,8 @@ function mapSourceToResponse(
// "ExecuteQuery" algorithm, for which `execute` is also used.
return mapAsyncIterator(resultOrStream, (payload: unknown) =>
execute({
schema,
document,
...args,
rootValue: payload,
contextValue,
variableValues,
operationName,
fieldResolver,
}),
);
}
Expand Down Expand Up @@ -163,14 +120,50 @@ function mapSourceToResponse(
* "Supporting Subscriptions at Scale" information in the GraphQL specification.
*/
export function createSourceEventStream(
schema: GraphQLSchema,
document: DocumentNode,
argsOrSchema: ExecutionArgs | GraphQLSchema,
/** Note: document argument is required if positional arguments are used */
/** @deprecated will be removed in next major version in favor of named arguments */
yaacovCR marked this conversation as resolved.
Show resolved Hide resolved
document?: DocumentNode,
/** @deprecated will be removed in next major version in favor of named arguments */
rootValue?: unknown,
/** @deprecated will be removed in next major version in favor of named arguments */
contextValue?: unknown,
/** @deprecated will be removed in next major version in favor of named arguments */
variableValues?: Maybe<{ readonly [variable: string]: unknown }>,
/** @deprecated will be removed in next major version in favor of named arguments */
operationName?: Maybe<string>,
/** @deprecated will be removed in next major version in favor of named arguments */
subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult> {
if (isSchema(argsOrSchema)) {
invariant(document != null, 'Must provide document.');
yaacovCR marked this conversation as resolved.
Show resolved Hide resolved
return createSourceEventStreamImpl({
schema: argsOrSchema,
document,
rootValue,
contextValue,
variableValues,
operationName,
subscribeFieldResolver,
});
}

return createSourceEventStreamImpl(argsOrSchema);
}

export function createSourceEventStreamImpl(
args: ExecutionArgs,
): PromiseOrValue<AsyncIterable<unknown> | ExecutionResult> {
const {
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
subscribeFieldResolver,
} = args;

// If arguments are missing or incorrectly typed, this is an internal
// developer mistake which should throw an early error.
assertValidExecutionArguments(schema, document, variableValues);
Expand Down