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
apollo-server-core: use UserInputError for variable coercion errors #5091
Changes from 1 commit
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 |
---|---|---|
|
@@ -31,6 +31,7 @@ import { | |
PersistedQueryNotSupportedError, | ||
PersistedQueryNotFoundError, | ||
formatApolloErrors, | ||
UserInputError, | ||
} from 'apollo-server-errors'; | ||
import { | ||
GraphQLRequest, | ||
|
@@ -451,16 +452,36 @@ export async function processGraphQLRequest<TContext>( | |
requestContext as GraphQLRequestContextExecutionDidStart<TContext>, | ||
); | ||
|
||
if (result.errors) { | ||
await didEncounterErrors(result.errors); | ||
// The first thing that execution does is coerce the request's variables | ||
// to the types declared in the operation, which can lead to errors if | ||
// they are of the wrong type. We change any such errors into | ||
// UserInputError so that their code doesn't end up being | ||
// INTERNAL_SERVER_ERROR, since these are client errors. | ||
const resultErrors = result.errors?.map((e) => { | ||
if ( | ||
e.nodes?.length === 1 && | ||
e.nodes[0].kind === 'VariableDefinition' && | ||
e.message.startsWith( | ||
`Variable "$${e.nodes[0].variable.name.value}" got invalid value `, | ||
) | ||
) { | ||
return fromGraphQLError(e, { | ||
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. I don't know why codecov thinks this is uncovered. There's a test! |
||
errorConstructor: (message) => new UserInputError(message), | ||
}); | ||
} | ||
return e; | ||
}); | ||
|
||
if (resultErrors) { | ||
await didEncounterErrors(resultErrors); | ||
} | ||
|
||
response = { | ||
...result, | ||
errors: result.errors ? formatErrors(result.errors) : undefined, | ||
errors: resultErrors ? formatErrors(resultErrors) : undefined, | ||
}; | ||
|
||
executionDispatcher.reverseInvokeHookSync("executionDidEnd"); | ||
executionDispatcher.reverseInvokeHookSync('executionDidEnd'); | ||
} catch (executionError) { | ||
executionDispatcher.reverseInvokeHookSync("executionDidEnd", executionError); | ||
return await sendErrorResponse(executionError); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,12 +134,16 @@ export function toApolloError( | |
|
||
export interface ErrorOptions { | ||
code?: string; | ||
// Sometimes it's more convenient to pass a function than a class name. | ||
errorConstructor?: (message: string) => ApolloError; | ||
errorClass?: typeof ApolloError; | ||
} | ||
|
||
export function fromGraphQLError(error: GraphQLError, options?: ErrorOptions) { | ||
const copy: ApolloError = | ||
options && options.errorClass | ||
options && options.errorConstructor | ||
? options.errorConstructor(error.message) | ||
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. I did this because it turns out that Now, fromGraphQLError only ever passes the first argument so both of those should be fine. But it doesn't work with 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 OK I think I figured it out. |
||
: options && options.errorClass | ||
? new options.errorClass(error.message) | ||
: new ApolloError(error.message); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use
graphql-js
'sKind.VARIABLE_DEFINITION
for thisThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, although TS actually does require the string constant here to be one of the possible valid strings (ie, typos won't compile).