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

execute: Correctly report missing root type error #3308

Merged
merged 1 commit into from Oct 11, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/execution/__tests__/executor-test.ts
Expand Up @@ -917,18 +917,42 @@ describe('Execute: Handles basic execution tasks', () => {
subscription S { __typename }
`);

// FIXME: errors should be wrapped into ExecutionResult
expect(() =>
expectJSON(
executeSync({ schema, document, operationName: 'Q' }),
).to.throw('Schema is not configured to execute query operation.');
).to.deep.equal({
data: null,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yaacovCR Probably it should be just errors without data, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec is unclear, sometimes it says that if execution fails because of missing information, you should just errors, sometimes it says that if execution starts, you should get some data, and it is internally contradictory on whether request execution is considered execution for these purposes, definitionally it calls it execution, but the parts of execution seem validation like, and so in the subsections it says to return only errors and that “execution” is considered to start only with operation execution @benjie has a bit in the glossary about this maybe he has thoughts

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of that ambiguity in requests, where it is called execution even though later not considered execution, I am inclined to say the same is possibly true in the execution operations section, so we can return just errors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the spec should be clarified, I think, so kind of doesnt matter what we choose, except if we can anticipate how we would like this to go, in terms of usefulness for clients, I don’t even know what the right answer is, in general, documents should be validated, so this case shouldn’t even come up, so I would say should be similar to any missing field where document not validated where I assume null is returned so data should be null here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although currently the subscribe code I think just returns errors, that’s the better option there because data for subscribe belongs only in payloads. So maybe consistency with subscribe for execute means data should be skipped for all the operations

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From Response/Response Format/Data:

If an error was raised during the execution that prevented a valid response, the data entry in the response should be null.

From Executing Requests:

  1. If operation is a query operation:
    • Return ExecuteQuery(operation, schema, coercedVariableValues, initialValue).

From ExecuteQuery():

  1. Assert: queryType is an Object type.

Assuming that this error is triggered by the assert above, then it's part of the execution process and thus the data should be null. Interesting that this is a should not a must, actually. Anyway; since we're the reference implementation we should follow the should.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so I understand that is because the assertion is made in the operation execution algorithm section, and not the request execution section? Or is either section considered execution? Variable coercion section says:

If a request error is encountered during input coercion of variable values, then the operation fails without execution.

That is ambiguous itself seems to mean operation execution, as opposed to request execution.

Anyways, we should align subscription behavior to whatever we do for queries

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you’re right; there is ambiguity there. I’ll have a go at adding some clarity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out that this is addressed a couple paragraphs lower down:

https://spec.graphql.org/draft/#sec-Errors.Request-errors

Namely: ExecuteRequest() can raise "request errors" which are deemed to occur "before execution begins". So "execution" refers to neither "operation execution" nor "request execution" but to the phase of request/operation execution that occurs after the last possible point at which a request error could be raised. Note this could be during the CreateSourceEventStream() algorithm.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's my attempt to clarify this with a non-normative note: graphql/graphql-spec#894

errors: [
{
message: 'Schema is not configured to execute query operation.',
locations: [{ line: 2, column: 7 }],
},
],
});

expect(() =>
expectJSON(
executeSync({ schema, document, operationName: 'M' }),
).to.throw('Schema is not configured to execute mutation operation.');
).to.deep.equal({
data: null,
errors: [
{
message: 'Schema is not configured to execute mutation operation.',
locations: [{ line: 3, column: 7 }],
},
],
});

expect(() =>
expectJSON(
executeSync({ schema, document, operationName: 'S' }),
).to.throw('Schema is not configured to execute subscription operation.');
).to.deep.equal({
data: null,
errors: [
{
message:
'Schema is not configured to execute subscription operation.',
locations: [{ line: 4, column: 7 }],
},
],
});
});

it('correct field ordering despite execution order', async () => {
Expand Down
76 changes: 40 additions & 36 deletions src/execution/execute.ts
Expand Up @@ -185,8 +185,27 @@ export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
// field and its descendants will be omitted, and sibling fields will still
// be executed. An execution which encounters errors will still result in a
// resolved Promise.
const data = executeOperation(exeContext, exeContext.operation, rootValue);
return buildResponse(exeContext, data);
//
// Errors from sub-fields of a NonNull type may propagate to the top level,
// at which point we still log the error and null the parent field, which
// in this case is the entire response.
try {
const { operation } = exeContext;
const result = executeOperation(exeContext, operation, rootValue);
if (isPromise(result)) {
return result.then(
(data) => buildResponse(data, exeContext.errors),
(error) => {
exeContext.errors.push(error);
return buildResponse(null, exeContext.errors);
},
);
}
return buildResponse(result, exeContext.errors);
} catch (error) {
exeContext.errors.push(error);
return buildResponse(null, exeContext.errors);
}
}

/**
Expand All @@ -210,15 +229,10 @@ export function executeSync(args: ExecutionArgs): ExecutionResult {
* response defined by the "Response" section of the GraphQL specification.
*/
function buildResponse(
exeContext: ExecutionContext,
data: PromiseOrValue<ObjMap<unknown> | null>,
): PromiseOrValue<ExecutionResult> {
if (isPromise(data)) {
return data.then((resolved) => buildResponse(exeContext, resolved));
}
return exeContext.errors.length === 0
? { data }
: { errors: exeContext.errors, data };
data: ObjMap<unknown> | null,
errors: ReadonlyArray<GraphQLError>,
): ExecutionResult {
return errors.length === 0 ? { data } : { errors, data };
}

/**
Expand Down Expand Up @@ -349,33 +363,23 @@ function executeOperation(
rootType,
operation.selectionSet,
);

const path = undefined;

// Errors from sub-fields of a NonNull type may propagate to the top level,
// at which point we still log the error and null the parent field, which
// in this case is the entire response.
try {
const result =
operation.operation === 'mutation'
? executeFieldsSerially(
exeContext,
rootType,
rootValue,
path,
rootFields,
)
: executeFields(exeContext, rootType, rootValue, path, rootFields);
if (isPromise(result)) {
return result.then(undefined, (error) => {
exeContext.errors.push(error);
return null;
});
}
return result;
} catch (error) {
exeContext.errors.push(error);
return null;
switch (operation.operation) {
case 'query':
return executeFields(exeContext, rootType, rootValue, path, rootFields);
case 'mutation':
return executeFieldsSerially(
exeContext,
rootType,
rootValue,
path,
rootFields,
);
case 'subscription':
// TODO: deprecate `subscribe` and move all logic here
// Temporary solution until we finish merging execute and subscribe together
return executeFields(exeContext, rootType, rootValue, path, rootFields);
}
}

Expand Down