-
Notifications
You must be signed in to change notification settings - Fork 45
Throw on undefined root fields #632
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "minor", | ||
| "comment": "throw on executing undefined root field", | ||
| "packageName": "@graphitation/supermassive", | ||
| "email": "dsamsonov@microsoft.com", | ||
| "dependentChangeType": "patch" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -460,6 +460,10 @@ function executeFieldsSerially( | |
| } | ||
| if (isPromise(result)) { | ||
| return result.then((resolvedResult: unknown) => { | ||
| if (resolvedResult === undefined) { | ||
| return results; | ||
| } | ||
|
|
||
| results[responseName] = resolvedResult; | ||
| return results; | ||
| }); | ||
|
|
@@ -566,8 +570,10 @@ function executeField( | |
| fieldName, | ||
| }); | ||
| if (!loading) { | ||
| throwIfRootField(parentTypeName, fieldName, fieldGroup, "typeDefinition"); | ||
| return undefined; | ||
| } | ||
|
|
||
| return loading.then(() => { | ||
| const fieldDef = Definitions.getField( | ||
| exeContext.schemaFragment.definitions, | ||
|
|
@@ -585,10 +591,31 @@ function executeField( | |
| incrementalDataRecord, | ||
| ); | ||
| } | ||
|
|
||
| throwIfRootField(parentTypeName, fieldName, fieldGroup, "typeDefinition"); | ||
| return undefined; | ||
| }); | ||
| } | ||
|
|
||
| function throwIfRootField( | ||
| parentTypeName: string, | ||
| fieldName: string, | ||
| fieldGroup: FieldGroup, | ||
| validationTarget: "typeDefinition" | "resolver", | ||
| ) { | ||
| const isRootField = | ||
| parentTypeName === "Mutation" || parentTypeName === "Query"; | ||
|
Comment on lines
+606
to
+607
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Subscriptions have a separate execution and this function is not called there. As for the path, it's probably not what you think it is. For example, for new test cases I get: {
"prev": undefined,
"key": "downloadFile",
"typename": "Mutation"
}
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right, path is an array is in graphql-js, we changed it to linked list for perf I guess. In our case it's just |
||
| if (isRootField) { | ||
| const message = | ||
| validationTarget === "typeDefinition" | ||
| ? `Type definition for ${parentTypeName}.${fieldName} is missing` | ||
| : validationTarget === "resolver" | ||
| ? `Resolver for ${parentTypeName}.${fieldName} is missing` | ||
| : "Unexpected validation target"; | ||
| throw locatedError(new Error(message), fieldGroup); | ||
| } | ||
| } | ||
|
|
||
| function requestSchemaFragment( | ||
| exeContext: ExecutionContext, | ||
| request: SchemaFragmentRequest, | ||
|
|
@@ -999,6 +1026,18 @@ function resolveAndCompleteField( | |
|
|
||
| const isDefaultResolverUsed = | ||
| resolveFn === exeContext.fieldResolver || fieldName === "__typename"; | ||
|
|
||
| if (resolveFn === exeContext.fieldResolver && typeof source === "undefined") { | ||
| /** | ||
| * Resolving a root field with default resolver makes operation look succsessful, even though nothing was actually done. | ||
| * This leads to problems that are hard to debug, especially for mutations. | ||
| * | ||
| * NOTE1: executing Mutation.__typename or Query.__typename with default resolver is fine | ||
| * NOTE2: source check is required to account for systems like Grats that work exclusively on default resolvers | ||
| */ | ||
| throwIfRootField(parentTypeName, fieldName, fieldGroup, "resolver"); | ||
| } | ||
|
|
||
| const hooks = exeContext.fieldExecutionHooks; | ||
| let hookContext: unknown = undefined; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.