Skip to content

Commit

Permalink
Return prisma type errors in dev (keystonejs#6880)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown authored Nov 2, 2021
1 parent aa98b1c commit 400f4e1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-eggs-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': patch
---

Runtime type errors from Prisma are now actually returned instead of being returned as `Prisma error:`
7 changes: 7 additions & 0 deletions packages/keystone/src/lib/core/graphql-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ export const accessDeniedError = (msg: string) =>
new ApolloError(`Access denied: ${msg}`, 'KS_ACCESS_DENIED');

export const prismaError = (err: Error) => {
if ((err as any).code === undefined) {
return new ApolloError(`Prisma error`, 'KS_PRISMA_ERROR', {
debug: {
message: err.message,
},
});
}
return new ApolloError(
`Prisma error: ${err.message.split('\n').slice(-1)[0].trim()}`,
'KS_PRISMA_ERROR',
Expand Down
41 changes: 39 additions & 2 deletions tests/api-tests/hooks/hook-errors.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { text } from '@keystone-next/keystone/fields';
import { relationship, text } from '@keystone-next/keystone/fields';
import { list } from '@keystone-next/keystone';
import { GraphQLRequest, setupTestRunner } from '@keystone-next/keystone/testing';
import { KeystoneContext } from '@keystone-next/keystone/types';
import { apiTestConfig, expectExtensionError } from '../utils';
import { apiTestConfig, expectExtensionError, unpackErrors } from '../utils';

const runner = (debug: boolean | undefined) =>
setupTestRunner({
Expand Down Expand Up @@ -91,6 +91,18 @@ const runner = (debug: boolean | undefined) =>
}),
},
}),
BadResolveInput: list({
fields: {
badResolveInput: relationship({
ref: 'Post',
hooks: {
resolveInput() {
return { blah: true };
},
},
}),
},
}),
},
graphql: { debug },
}),
Expand Down Expand Up @@ -585,3 +597,28 @@ const runner = (debug: boolean | undefined) =>
);
});
});

test(
'bad resolve input',
runner(true)(async ({ context }) => {
const { data, errors } = await context.graphql.raw({
query: `mutation { createBadResolveInput(data: {}) { id } }`,
});
expect(data).toEqual({ createBadResolveInput: null });
const unpackedErrors = unpackErrors(errors);
expect(unpackedErrors).toEqual([
{
extensions: {
code: 'KS_PRISMA_ERROR',
debug: {
message: expect.stringMatching(
/Unknown arg `blah` in data\.badResolveInput\.blah for type PostCreateNestedOneWithoutFrom_BadResolveInput_badResolveInputInput\./
),
},
},
path: ['createBadResolveInput'],
message: 'Prisma error',
},
]);
})
);
2 changes: 1 addition & 1 deletion tests/api-tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const apiTestConfig = (
},
});

const unpackErrors = (errors: readonly any[] | undefined) =>
export const unpackErrors = (errors: readonly any[] | undefined) =>
(errors || []).map(({ locations, ...unpacked }) => unpacked);

const j = (messages: string[]) => messages.map(m => ` - ${m}`).join('\n');
Expand Down

0 comments on commit 400f4e1

Please sign in to comment.