diff --git a/.changeset/orange-buttons-crash.md b/.changeset/orange-buttons-crash.md new file mode 100644 index 00000000000..98d76832690 --- /dev/null +++ b/.changeset/orange-buttons-crash.md @@ -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. diff --git a/packages/keystone/src/lib/schema-type-printer.tsx b/packages/keystone/src/lib/schema-type-printer.tsx index 2f04b993321..0b05d902119 100644 --- a/packages/keystone/src/lib/schema-type-printer.tsx +++ b/packages/keystone/src/lib/schema-type-printer.tsx @@ -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, @@ -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; +export type KeystoneDbAPI = GenericKeystoneDbAPI; + +export type KeystoneContext = Omit & { + db: { lists: KeystoneDbAPI }; + lists: KeystoneListsAPI; + prisma: import('.prisma/client').PrismaClient; +}; +`; + return prettier.format(prelude + printedTypes + allListsStr + postlude, { parser: 'babel-ts', trailingComma: 'es5', singleQuote: true, diff --git a/packages/keystone/src/scripts/tests/__snapshots__/artifacts.test.ts.snap b/packages/keystone/src/scripts/tests/__snapshots__/artifacts.test.ts.snap index 0454cb3c9fc..b6fe7d393b9 100644 --- a/packages/keystone/src/scripts/tests/__snapshots__/artifacts.test.ts.snap +++ b/packages/keystone/src/scripts/tests/__snapshots__/artifacts.test.ts.snap @@ -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; @@ -113,6 +118,17 @@ export type TodoListFn = ( export type KeystoneListsTypeInfo = { readonly Todo: TodoListTypeInfo; }; +export type KeystoneListsAPI = GenericKeystoneListsAPI; +export type KeystoneDbAPI = GenericKeystoneDbAPI; + +export type KeystoneContext = Omit< + GenericKeystoneContext, + 'db' | 'lists' | 'prisma' +> & { + db: { lists: KeystoneDbAPI }; + lists: KeystoneListsAPI; + prisma: import('.prisma/client').PrismaClient; +}; ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ node_modules/.keystone/types.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯