-
Notifications
You must be signed in to change notification settings - Fork 2k
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 3 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 |
---|---|---|
|
@@ -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! |
||
errorClass: UserInputError, | ||
}); | ||
} | ||
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,7 +134,7 @@ export function toApolloError( | |
|
||
export interface ErrorOptions { | ||
code?: string; | ||
errorClass?: typeof ApolloError; | ||
errorClass?: new (message: string) => ApolloError; | ||
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. This seems fine, maybe a comment here that explains, though I also think it's pretty self-explanatory if you look at it for a second. FWIW, I tried to improve the type here a bit with no luck - I tried to make this "condition" into an interface that 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. Added a comment. FWIW I find this type to be easier to understand than |
||
} | ||
|
||
export function fromGraphQLError(error: GraphQLError, options?: ErrorOptions) { | ||
|
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).