Skip to content

Commit

Permalink
Generate specific types for KeystoneContext and List APIs (#6093)
Browse files Browse the repository at this point in the history
  • Loading branch information
JedWatson committed Jul 13, 2021
1 parent 139d7a8 commit f482db6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/orange-buttons-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@keystone-next/keystone": patch
---

Added generated types for KeystoneContext, KeystoneListsAPI and KeystoneDbAPI.

You can now import these from `.keystone/types` (instead of `@keystone-next/types`) to get types that are pre-bound to your project's schema.
22 changes: 21 additions & 1 deletion packages/keystone/src/lib/schema-type-printer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ export function printGeneratedTypes(
Float: 'number',
JSON: 'import("@keystone-next/types").JSONValue',
};

let prelude = `import {
KeystoneListsAPI as GenericKeystoneListsAPI,
KeystoneDbAPI as GenericKeystoneDbAPI,
KeystoneContext as GenericKeystoneContext,
} from '@keystone-next/types';
`;

let { printedTypes, ast, printTypeNode } = printInputTypesFromSchema(
printedSchema,
graphQLSchema,
Expand Down Expand Up @@ -154,7 +162,19 @@ export type ${listKey}ListFn = (
`;
allListsStr += `\n readonly ${JSON.stringify(listKey)}: ${listTypeInfoName};`;
}
return prettier.format(printedTypes + allListsStr + '\n};\n', {
allListsStr += '\n};';

const postlude = `
export type KeystoneListsAPI = GenericKeystoneListsAPI<KeystoneListsTypeInfo>;
export type KeystoneDbAPI = GenericKeystoneDbAPI<KeystoneListsTypeInfo>;
export type KeystoneContext = Omit<GenericKeystoneContext, 'db' | 'lists' | 'prisma'> & {
db: { lists: KeystoneDbAPI };
lists: KeystoneListsAPI;
prisma: import('.prisma/client').PrismaClient;
};
`;
return prettier.format(prelude + printedTypes + allListsStr + postlude, {
parser: 'babel-ts',
trailingComma: 'es5',
singleQuote: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

exports[`postinstall writes the correct node_modules files 1`] = `
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ node_modules/.keystone/types.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
import {
KeystoneListsAPI as GenericKeystoneListsAPI,
KeystoneDbAPI as GenericKeystoneDbAPI,
KeystoneContext as GenericKeystoneContext,
} from '@keystone-next/types';
type Scalars = {
readonly ID: string;
readonly Boolean: boolean;
Expand Down Expand Up @@ -113,6 +118,17 @@ export type TodoListFn = (
export type KeystoneListsTypeInfo = {
readonly Todo: TodoListTypeInfo;
};
export type KeystoneListsAPI = GenericKeystoneListsAPI<KeystoneListsTypeInfo>;
export type KeystoneDbAPI = GenericKeystoneDbAPI<KeystoneListsTypeInfo>;
export type KeystoneContext = Omit<
GenericKeystoneContext,
'db' | 'lists' | 'prisma'
> & {
db: { lists: KeystoneDbAPI };
lists: KeystoneListsAPI;
prisma: import('.prisma/client').PrismaClient;
};
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ node_modules/.keystone/types.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Expand Down

1 comment on commit f482db6

@vercel
Copy link

@vercel vercel bot commented on f482db6 Jul 13, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.