Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions packages/apollo-server-core/src/requestPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
PersistedQueryNotSupportedError,
PersistedQueryNotFoundError,
formatApolloErrors,
UserInputError,
} from 'apollo-server-errors';
import {
GraphQLRequest,
Expand Down Expand Up @@ -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' &&

Copy link
Copy Markdown
Contributor

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's Kind.VARIABLE_DEFINITION for this

Copy link
Copy Markdown
Member Author

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).

e.message.startsWith(
`Variable "$${e.nodes[0].variable.name.value}" got invalid value `,
)
) {
return fromGraphQLError(e, {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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);
Expand Down
6 changes: 5 additions & 1 deletion packages/apollo-server-errors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I did this because it turns out that UserInputError is not typeof ApolloError. The ApolloError constructor is (message: string, code?: string, extensions?: Record<string, any>) whereas the UserInputError constructor is (message: string, properties?: Record<string, any>) so there are valid calls to new ApolloError("x", "y") that can't be invoked on new UserInputError. (Other subclasses in this file just have (message: string) and I guess that's OK because additional arguments are just ignored?)

Now, fromGraphQLError only ever passes the first argument so both of those should be fine. But it doesn't work with typeof ApolloError. I did ask on internal Slack to see if anyone has any ideas.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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);

Expand Down
21 changes: 21 additions & 0 deletions packages/apollo-server-integration-testsuite/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,27 @@ export function testApolloServer<AS extends ApolloServerBase>(
});
});

it('variable coercion errors', async () => {
const { url: uri } = await createApolloServer({
typeDefs: gql`
type Query {
hello(x: String): String
}
`,
});

const apolloFetch = createApolloFetch({ uri });

const result = await apolloFetch({
query: `query ($x:String) {hello(x:$x)}`,
variables: { x: 2 },
});
expect(result.data).toBeUndefined();
expect(result.errors).toBeDefined();
expect(result.errors[0].message).toMatch(/got invalid value 2; Expected type String/);
expect(result.errors[0].extensions.code).toBe('BAD_USER_INPUT');
});

describe('schema creation', () => {
it('accepts typeDefs and resolvers', async () => {
const typeDefs = gql`
Expand Down