Skip to content

Commit

Permalink
extract isBadUserInputGraphQLError and replace RegExp with startsWith
Browse files Browse the repository at this point in the history
  • Loading branch information
gchen02 committed Feb 1, 2022
1 parent e1ee2ef commit 863fe02
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions packages/apollo-server-core/src/requestPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ export type DataSources<TContext> = {

type Mutable<T> = { -readonly [P in keyof T]: T[P] };

function isBadUserInputGraphQLError(error: GraphQLError): Boolean {
return (
error.nodes?.length === 1 &&
error.nodes[0].kind === Kind.VARIABLE_DEFINITION &&
(error.message.startsWith(
`Variable "$${error.nodes[0].variable.name.value}" got invalid value `,
) ||
error.message.startsWith(
`Variable "$${error.nodes[0].variable.name.value}" of required type `,
) ||
error.message.startsWith(
`Variable "$${error.nodes[0].variable.name.value}" of non-null type `,
))
);
}

export async function processGraphQLRequest<TContext>(
config: GraphQLRequestPipelineConfig<TContext>,
requestContext: Mutable<GraphQLRequestContext<TContext>>,
Expand Down Expand Up @@ -400,27 +416,7 @@ export async function processGraphQLRequest<TContext>(
// variable resolution from execution later; see
// https://github.com/graphql/graphql-js/issues/3169
const resultErrors = result.errors?.map((e) => {
if (
e.nodes?.length === 1 &&
e.nodes[0].kind === Kind.VARIABLE_DEFINITION &&
(e.message.startsWith(
`Variable "$${e.nodes[0].variable.name.value}" got invalid value `,
) ||
e.message.match(
new RegExp(
'^Variable "\\$' +
e.nodes[0].variable.name.value +
'" of required type ".*!" was not provided.',
),
) ||
e.message.match(
new RegExp(
'^Variable "\\$' +
e.nodes[0].variable.name.value +
'" of non-null type ".*!" must not be null.',
),
))
) {
if (isBadUserInputGraphQLError(e)) {
return fromGraphQLError(e, {
errorClass: UserInputError,
});
Expand Down

0 comments on commit 863fe02

Please sign in to comment.