Skip to content

Commit

Permalink
Add check to ensure using experimental execute if experimental direct…
Browse files Browse the repository at this point in the history
…ives are present in schema.
  • Loading branch information
yaacovCR committed Sep 14, 2022
1 parent c9ae885 commit 215c826
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/execution/__tests__/executor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import {
GraphQLScalarType,
GraphQLUnionType,
} from '../../type/definition.js';
import {
GraphQLDeferDirective,
GraphQLStreamDirective,
} from '../../type/directives.js';
import {
GraphQLBoolean,
GraphQLInt,
Expand Down Expand Up @@ -863,6 +867,40 @@ describe('Execute: Handles basic execution tasks', () => {
expect(result).to.deep.equal({ data: { a: 'b' } });
});

it('errors when using original execute with schemas including experimental @defer directive', () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Q',
fields: {
a: { type: GraphQLString },
},
}),
directives: [GraphQLDeferDirective],
});
const document = parse('query Q { a }');

expect(() => execute({ schema, document })).to.throw(
'Use function `experimentalExecuteIncrementally` to execute operations against schemas that define the experimental @defer or @stream directives.',
);
});

it('errors when using original execute with schemas including experimental @stream directive', () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Q',
fields: {
a: { type: GraphQLString },
},
}),
directives: [GraphQLStreamDirective],
});
const document = parse('query Q { a }');

expect(() => execute({ schema, document })).to.throw(
'Use function `experimentalExecuteIncrementally` to execute operations against schemas that define the experimental @defer or @stream directives.',
);
});

it('resolves to an error if schema does not support operation', () => {
const schema = new GraphQLSchema({ assumeValid: true });

Expand Down
10 changes: 10 additions & 0 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,26 @@ const UNEXPECTED_MULTIPLE_PAYLOADS =
* delivery.
*/
export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
if (args.schema.getDirective('defer') || args.schema.getDirective('stream')) {
throw new Error(
'Use function `experimentalExecuteIncrementally` to execute operations against schemas that define the experimental @defer or @stream directives.',
);
}

const result = experimentalExecuteIncrementally(args);
if (!isPromise(result)) {
if ('initialResult' in result) {
// This can happen if the operation contains @defer or @stream directives
// and is not validated prior to execution
throw new Error(UNEXPECTED_MULTIPLE_PAYLOADS);
}
return result;
}

return result.then((incrementalResult) => {
if ('initialResult' in incrementalResult) {
// This can happen if the operation contains @defer or @stream directives
// and is not validated prior to execution
throw new Error(UNEXPECTED_MULTIPLE_PAYLOADS);
}
return incrementalResult;
Expand Down

0 comments on commit 215c826

Please sign in to comment.