Skip to content

Commit

Permalink
feat(UserInputError): handle unhandled errors.
Browse files Browse the repository at this point in the history
required type & non-null errors handled.
  • Loading branch information
5achinJani authored and glasser committed Jul 21, 2021
1 parent e030e02 commit 652e14a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/apollo-server-core/src/requestPipeline.ts
Expand Up @@ -392,6 +392,30 @@ export async function processGraphQLRequest<TContext>(
return fromGraphQLError(e, {
errorClass: UserInputError,
});
} else if (
e.nodes?.length === 1 &&
e.nodes[0].kind === Kind.VARIABLE_DEFINITION &&
e.nodes[0].type.kind === Kind.NON_NULL_TYPE &&
e.nodes[0].type.type.kind === Kind.NAMED_TYPE &&
e.message.startsWith(
`Variable "$${e.nodes[0].variable.name.value}" of required type "${e.nodes[0].type.type.name.value}!" was not provided.`,
)
) {
return fromGraphQLError(e, {
errorClass: UserInputError,
});
} else if (
e.nodes?.length === 1 &&
e.nodes[0].kind === Kind.VARIABLE_DEFINITION &&
e.nodes[0].type.kind === Kind.NON_NULL_TYPE &&
e.nodes[0].type.type.kind === Kind.NAMED_TYPE &&
e.message.startsWith(
`Variable "$${e.nodes[0].variable.name.value}" of non-null type "${e.nodes[0].type.type.name.value}!" must not be null.`,
)
) {
return fromGraphQLError(e, {
errorClass: UserInputError,
});
}
return e;
});
Expand Down
45 changes: 45 additions & 0 deletions packages/apollo-server-integration-testsuite/src/ApolloServer.ts
Expand Up @@ -343,6 +343,51 @@ export function testApolloServer<AS extends ApolloServerBase>(
expect(result.errors[0].extensions.code).toBe('BAD_USER_INPUT');
});

it('catches required type variable error and returns UserInputError', 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)}`,
});
expect(result.data).toBeUndefined();
expect(result.errors).toBeDefined();
expect(result.errors[0].message).toMatch(
`Variable "$x" of required type "String!" was not provided.`
);
expect(result.errors[0].extensions.code).toBe('BAD_USER_INPUT');
});

it('catches non-null type variable error and returns UserInputError', 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: null },
});
expect(result.data).toBeUndefined();
expect(result.errors).toBeDefined();
expect(result.errors[0].message).toMatch(
`Variable "$x" of non-null type "String!" must not be null.`
);
expect(result.errors[0].extensions.code).toBe('BAD_USER_INPUT');
});

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

0 comments on commit 652e14a

Please sign in to comment.