Skip to content

incrementalDelivery: use single execute function with directives as flags #3732

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

Closed
wants to merge 1 commit into from
Closed
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
48 changes: 2 additions & 46 deletions src/execution/__tests__/defer-test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { expectJSON } from '../../__testUtils__/expectJSON.js';
import { expectPromise } from '../../__testUtils__/expectPromise.js';
import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick.js';

import type { DocumentNode } from '../../language/ast.js';
Expand All @@ -20,7 +18,7 @@ import type {
InitialIncrementalExecutionResult,
SubsequentIncrementalExecutionResult,
} from '../execute.js';
import { execute, experimentalExecuteIncrementally } from '../execute.js';
import { execute } from '../execute.js';

const friendType = new GraphQLObjectType({
fields: {
Expand Down Expand Up @@ -84,7 +82,7 @@ const query = new GraphQLObjectType({
const schema = new GraphQLSchema({ query });

async function complete(document: DocumentNode) {
const result = await experimentalExecuteIncrementally({
const result = await execute<true>({
schema,
document,
rootValue: {},
Expand Down Expand Up @@ -656,46 +654,4 @@ describe('Execute: defer directive', () => {
},
]);
});

it('original execute function throws error if anything is deferred and everything else is sync', () => {
const doc = `
query Deferred {
... @defer { hero { id } }
}
`;
expect(() =>
execute({
schema,
document: parse(doc),
rootValue: {},
}),
).to.throw(
'Executing this GraphQL operation would unexpectedly produce multiple payloads (due to @defer or @stream directive)',
);
});

it('original execute function resolves to error if anything is deferred and something else is async', async () => {
const doc = `
query Deferred {
hero { slowField }
... @defer { hero { id } }
}
`;
expectJSON(
await expectPromise(
execute({
schema,
document: parse(doc),
rootValue: {},
}),
).toResolve(),
).toDeepEqual({
errors: [
{
message:
'Executing this GraphQL operation would unexpectedly produce multiple payloads (due to @defer or @stream directive)',
},
],
});
});
});
7 changes: 6 additions & 1 deletion src/execution/__tests__/executor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,12 @@ describe('Execute: Handles basic execution tasks', () => {
const rootValue = { a: 'b', c: 'd' };
const operationName = 'Q';

const result = executeSync({ schema, document, rootValue, operationName });
const result = executeSync({
schema,
document,
rootValue,
operationName,
});
expect(result).to.deep.equal({ data: { a: 'b' } });
});

Expand Down
10 changes: 3 additions & 7 deletions src/execution/__tests__/mutations-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import { GraphQLObjectType } from '../../type/definition.js';
import { GraphQLInt } from '../../type/scalars.js';
import { GraphQLSchema } from '../../type/schema.js';

import {
execute,
executeSync,
experimentalExecuteIncrementally,
} from '../execute.js';
import { execute, executeSync } from '../execute.js';

class NumberHolder {
theNumber: number;
Expand Down Expand Up @@ -218,7 +214,7 @@ describe('Execute: Handles mutation execution ordering', () => {
`);

const rootValue = new Root(6);
const mutationResult = await experimentalExecuteIncrementally({
const mutationResult = await execute<true>({
schema,
document,
rootValue,
Expand Down Expand Up @@ -294,7 +290,7 @@ describe('Execute: Handles mutation execution ordering', () => {
`);

const rootValue = new Root(6);
const mutationResult = await experimentalExecuteIncrementally({
const mutationResult = await execute<true>({
schema,
document,
rootValue,
Expand Down
20 changes: 10 additions & 10 deletions src/execution/__tests__/stream-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {
InitialIncrementalExecutionResult,
SubsequentIncrementalExecutionResult,
} from '../execute.js';
import { experimentalExecuteIncrementally } from '../execute.js';
import { execute } from '../execute.js';

const friendType = new GraphQLObjectType({
fields: {
Expand Down Expand Up @@ -83,7 +83,7 @@ const query = new GraphQLObjectType({
const schema = new GraphQLSchema({ query });

async function complete(document: DocumentNode, rootValue: unknown = {}) {
const result = await experimentalExecuteIncrementally({
const result = await execute<true>({
schema,
document,
rootValue,
Expand All @@ -106,7 +106,7 @@ async function completeAsync(
numCalls: number,
rootValue: unknown = {},
) {
const result = await experimentalExecuteIncrementally({
const result = await execute<true>({
schema,
document,
rootValue,
Expand Down Expand Up @@ -1187,7 +1187,7 @@ describe('Execute: stream directive', () => {
}
`);

const executeResult = await experimentalExecuteIncrementally({
const executeResult = await execute<true>({
schema,
document,
rootValue: {
Expand Down Expand Up @@ -1312,7 +1312,7 @@ describe('Execute: stream directive', () => {
}
}
`);
const executeResult = await experimentalExecuteIncrementally({
const executeResult = await execute<true>({
schema,
document,
rootValue: {
Expand Down Expand Up @@ -1405,7 +1405,7 @@ describe('Execute: stream directive', () => {
}
`);

const executeResult = await experimentalExecuteIncrementally({
const executeResult = await execute<true>({
schema,
document,
rootValue: {
Expand Down Expand Up @@ -1491,7 +1491,7 @@ describe('Execute: stream directive', () => {
}
`);

const executeResult = await experimentalExecuteIncrementally({
const executeResult = await execute<true>({
schema,
document,
rootValue: {
Expand Down Expand Up @@ -1595,7 +1595,7 @@ describe('Execute: stream directive', () => {
}
`);

const executeResult = await experimentalExecuteIncrementally({
const executeResult = await execute<true>({
schema,
document,
rootValue: {
Expand Down Expand Up @@ -1649,7 +1649,7 @@ describe('Execute: stream directive', () => {
}
`);

const executeResult = await experimentalExecuteIncrementally({
const executeResult = await execute<true>({
schema,
document,
rootValue: {
Expand Down Expand Up @@ -1709,7 +1709,7 @@ describe('Execute: stream directive', () => {
}
`);

const executeResult = await experimentalExecuteIncrementally({
const executeResult = await execute<true>({
schema,
document,
rootValue: {
Expand Down
78 changes: 2 additions & 76 deletions src/execution/__tests__/subscribe-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ import {
import { GraphQLSchema } from '../../type/schema.js';

import type { ExecutionArgs, ExecutionResult } from '../execute.js';
import {
createSourceEventStream,
experimentalSubscribeIncrementally,
subscribe,
} from '../execute.js';
import { createSourceEventStream, subscribe } from '../execute.js';

import { SimplePubSub } from './simplePubSub.js';

Expand Down Expand Up @@ -99,7 +95,6 @@ const emailSchema = new GraphQLSchema({
function createSubscription(
pubsub: SimplePubSub<Email>,
variableValues?: { readonly [variable: string]: unknown },
originalSubscribe: boolean = false,
) {
const document = parse(`
subscription ($priority: Int = 0, $shouldDefer: Boolean = false, $asyncResolver: Boolean = false) {
Expand Down Expand Up @@ -145,7 +140,7 @@ function createSubscription(
}),
};

return (originalSubscribe ? subscribe : experimentalSubscribeIncrementally)({
return subscribe({
schema: emailSchema,
document,
rootValue: data,
Expand Down Expand Up @@ -841,75 +836,6 @@ describe('Subscription Publish Phase', () => {
});
});

it('original subscribe function returns errors with @defer', async () => {
const pubsub = new SimplePubSub<Email>();
const subscription = await createSubscription(
pubsub,
{
shouldDefer: true,
},
true,
);
assert(isAsyncIterable(subscription));
// Wait for the next subscription payload.
const payload = subscription.next();

// A new email arrives!
expect(
pubsub.emit({
from: 'yuzhi@graphql.org',
subject: 'Alright',
message: 'Tests are good',
unread: true,
}),
).to.equal(true);

const errorPayload = {
done: false,
value: {
errors: [
{
message:
'Executing this GraphQL operation would unexpectedly produce multiple payloads (due to @defer or @stream directive)',
},
],
},
};

// The previously waited on payload now has a value.
expectJSON(await payload).toDeepEqual(errorPayload);

// Wait for the next payload from @defer
expectJSON(await subscription.next()).toDeepEqual(errorPayload);

// Another new email arrives, after all incrementally delivered payloads are received.
expect(
pubsub.emit({
from: 'hyo@graphql.org',
subject: 'Tools',
message: 'I <3 making things',
unread: true,
}),
).to.equal(true);

// The next waited on payload will have a value.
expectJSON(await subscription.next()).toDeepEqual(errorPayload);
// The next waited on payload will have a value.
expectJSON(await subscription.next()).toDeepEqual(errorPayload);

// The client disconnects before the deferred payload is consumed.
expectJSON(await subscription.return()).toDeepEqual({
done: true,
value: undefined,
});

// Awaiting a subscription after closing it results in completed results.
expectJSON(await subscription.next()).toDeepEqual({
done: true,
value: undefined,
});
});

it('produces a payload when there are multiple events', async () => {
const pubsub = new SimplePubSub<Email>();
const subscription = createSubscription(pubsub);
Expand Down
10 changes: 10 additions & 0 deletions src/execution/__tests__/sync-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ describe('Execute: synchronously when possible', () => {
}).to.throw('GraphQL execution failed to complete synchronously.');
});

it('return successfully if incremental delivery is enabled but async iterable is not returned', () => {
const doc = 'query Example { syncField }';
const result = executeSync({
schema,
document: parse(doc),
rootValue: 'rootValue',
});
expect(result).to.deep.equal({ data: { syncField: 'rootValue' } });
});

it('throws if encountering async iterable execution', () => {
const doc = `
query Example {
Expand Down
Loading