From fb7328417e4bdb1243706819823364ae0efebcfa Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Wed, 11 Nov 2026 06:07:55 +0000 Subject: [PATCH 1/3] fix(codegen): no CreateInput import in the ORM model or CLI command of a table without a create mutation --- .../__tests__/codegen/cli-generator.test.ts | 29 +++++++++++++++++++ .../__tests__/codegen/model-generator.test.ts | 1 + .../codegen/cli/table-command-generator.ts | 2 +- .../src/core/codegen/orm/model-generator.ts | 2 +- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/graphql/codegen/src/__tests__/codegen/cli-generator.test.ts b/graphql/codegen/src/__tests__/codegen/cli-generator.test.ts index 5fd6e84ffa..d02c21d918 100644 --- a/graphql/codegen/src/__tests__/codegen/cli-generator.test.ts +++ b/graphql/codegen/src/__tests__/codegen/cli-generator.test.ts @@ -252,6 +252,35 @@ describe('cli-generator', () => { const allCustomOps: Operation[] = [currentUserQuery, loginMutation]; +describe('cli-generator without a create mutation', () => { + it('neither offers create nor imports Create
Input', () => { + const fileTable = createTable({ + name: 'File', + fields: [ + { name: 'id', type: fieldTypes.uuid }, + { name: 'filename', type: fieldTypes.string }, + ], + query: { + all: 'files', + one: 'file', + create: null, + update: 'updateFile', + delete: 'deleteFile', + }, + }); + const { files } = generateCli({ + tables: [fileTable], + customOperations: { queries: [], mutations: [] }, + config: { cli: { toolName: 'myapp' } }, + }); + const file = files.find((f) => f.fileName === 'commands/file.ts'); + expect(file).toBeDefined(); + expect(file!.content).not.toContain('CreateFileInput'); + expect(file!.content).not.toContain("'create'"); + expect(file!.content).toContain('FilePatch'); + }); +}); + describe('cli docs generator', () => { it('generates CLI README', () => { const readme = generateCliReadme([carTable, driverTable], allCustomOps, 'myapp'); diff --git a/graphql/codegen/src/__tests__/codegen/model-generator.test.ts b/graphql/codegen/src/__tests__/codegen/model-generator.test.ts index f14579e4a6..0cc97a5f2d 100644 --- a/graphql/codegen/src/__tests__/codegen/model-generator.test.ts +++ b/graphql/codegen/src/__tests__/codegen/model-generator.test.ts @@ -120,6 +120,7 @@ describe('model-generator', () => { expect(result.content).not.toContain('create<'); expect(result.content).not.toContain('"createFile"'); + expect(result.content).not.toContain('CreateFileInput'); expect(result.content).toContain('update<'); expect(result.content).toContain('delete<'); }); diff --git a/graphql/codegen/src/core/codegen/cli/table-command-generator.ts b/graphql/codegen/src/core/codegen/cli/table-command-generator.ts index 5585e84f62..8eba6a0c5c 100644 --- a/graphql/codegen/src/core/codegen/cli/table-command-generator.ts +++ b/graphql/codegen/src/core/codegen/cli/table-command-generator.ts @@ -1683,7 +1683,7 @@ export function generateTableCommand(table: Table, options?: TableCommandOptions const orderByTypeName = getOrderByTypeName(table); statements.push( createImportDeclaration(inputTypesPath, [ - createInputTypeName, + ...(table.query?.create !== null ? [createInputTypeName] : []), patchTypeName, selectTypeName, whereTypeName, diff --git a/graphql/codegen/src/core/codegen/orm/model-generator.ts b/graphql/codegen/src/core/codegen/orm/model-generator.ts index 240fb31814..13ea41eee6 100644 --- a/graphql/codegen/src/core/codegen/orm/model-generator.ts +++ b/graphql/codegen/src/core/codegen/orm/model-generator.ts @@ -265,7 +265,7 @@ export function generateModelFile( selectTypeName, whereTypeName, orderByTypeName, - createInputTypeName, + ...(createMutationName ? [createInputTypeName] : []), updateInputTypeName, patchTypeName, ]; From b9473c6bd0931e19b2a695b21e55f5c1049e8106 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Wed, 11 Nov 2026 06:19:00 +0000 Subject: [PATCH 2/3] fix(codegen): a custom mutation's Create
Input is generated from the schema when the table has no create mutation --- .../__tests__/codegen/input-types-generator.test.ts | 12 ++++++++++++ .../src/core/codegen/orm/input-types-generator.ts | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/graphql/codegen/src/__tests__/codegen/input-types-generator.test.ts b/graphql/codegen/src/__tests__/codegen/input-types-generator.test.ts index fb5e3a3dbe..37b2d6f8f4 100644 --- a/graphql/codegen/src/__tests__/codegen/input-types-generator.test.ts +++ b/graphql/codegen/src/__tests__/codegen/input-types-generator.test.ts @@ -418,6 +418,18 @@ describe('generateInputTypesFile', () => { expect(result.content).toContain('UpdateFileInput'); }); + it('generates a custom mutation\'s Create
Input from the schema when the table has no create mutation', () => { + const principalTable = { ...userTable, name: 'Principal', query: { ...userTable.query, create: null as string | null } }; + const registry = new Map(sampleTypeRegistry); + registry.set('CreatePrincipalInput', { + kind: 'INPUT_OBJECT', + name: 'CreatePrincipalInput', + inputFields: [{ name: 'name', type: createNonNull(createTypeRef('SCALAR', 'String')) }], + }); + const result = generateInputTypesFile(registry, new Set(['CreatePrincipalInput']), [principalTable]); + expect(result.content).toMatch(/export interface CreatePrincipalInput \{\s*name: string;\s*\}/); + }); + it('handles empty tables array', () => { const result = generateInputTypesFile(new Map(), new Set()); expect(result.content).toMatchSnapshot(); diff --git a/graphql/codegen/src/core/codegen/orm/input-types-generator.ts b/graphql/codegen/src/core/codegen/orm/input-types-generator.ts index 47649a4ca4..52a5ac3f6a 100644 --- a/graphql/codegen/src/core/codegen/orm/input-types-generator.ts +++ b/graphql/codegen/src/core/codegen/orm/input-types-generator.ts @@ -1657,7 +1657,7 @@ function buildTableCrudTypeNames(tables: Table[]): Set { const crudTypes = new Set(); for (const table of tables) { const { typeName } = getTableNames(table); - crudTypes.add(`Create${typeName}Input`); + if (hasCreateMutation(table)) crudTypes.add(`Create${typeName}Input`); crudTypes.add(`Update${typeName}Input`); crudTypes.add(`Delete${typeName}Input`); crudTypes.add(`${typeName}Filter`); From a8a0176df8d9196cf3921795c38f1e90efd1965c Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Wed, 11 Nov 2026 06:29:12 +0000 Subject: [PATCH 3/3] fix(codegen): a table without a create mutation has no bulkCreate/bulkUpsert; generated ORM for it type-checks --- .../__tests__/codegen/orm-typecheck.test.ts | 71 +++++++++++++++++++ graphql/codegen/src/core/codegen/barrel.ts | 2 +- .../src/core/codegen/cli/docs-generator.ts | 14 ++-- .../codegen/cli/table-command-generator.ts | 6 +- .../src/core/codegen/hooks-docs-generator.ts | 12 ++-- .../codegen/src/core/codegen/mutation-keys.ts | 6 +- graphql/codegen/src/core/codegen/mutations.ts | 6 +- .../src/core/codegen/orm/docs-generator.ts | 16 +++-- .../src/core/codegen/orm/model-generator.ts | 6 +- graphql/codegen/src/core/codegen/utils.ts | 9 +++ 10 files changed, 120 insertions(+), 28 deletions(-) create mode 100644 graphql/codegen/src/__tests__/codegen/orm-typecheck.test.ts diff --git a/graphql/codegen/src/__tests__/codegen/orm-typecheck.test.ts b/graphql/codegen/src/__tests__/codegen/orm-typecheck.test.ts new file mode 100644 index 0000000000..ab6102a9e4 --- /dev/null +++ b/graphql/codegen/src/__tests__/codegen/orm-typecheck.test.ts @@ -0,0 +1,71 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import * as ts from 'typescript'; + +import { generateOrm } from '../../core/codegen/orm'; +import type { FieldType, Table } from '../../types/schema'; + +const uuid = { gqlType: 'UUID', isArray: false } as FieldType; +const string = { gqlType: 'String', isArray: false } as FieldType; + +function typeCheck(files: { path: string; content: string }[]): string[] { + const dir = fs.mkdtempSync(path.join(__dirname, '.orm-typecheck-')); + try { + const fileNames = files.map((file) => { + const fileName = path.join(dir, file.path); + fs.mkdirSync(path.dirname(fileName), { recursive: true }); + fs.writeFileSync(fileName, file.content); + return fileName; + }); + const program = ts.createProgram(fileNames, { + noEmit: true, + skipLibCheck: true, + target: ts.ScriptTarget.ES2022, + module: ts.ModuleKind.CommonJS, + moduleResolution: ts.ModuleResolutionKind.Node10, + esModuleInterop: true, + strict: true, + strictNullChecks: false + }); + return ts + .getPreEmitDiagnostics(program) + .filter((d) => d.file?.fileName.startsWith(dir)) + .map((d) => `${path.relative(dir, d.file!.fileName)}: ${ts.flattenDiagnosticMessageText(d.messageText, '\n')}`); + } finally { + fs.rmSync(dir, { recursive: true, force: true }); + } +} + +describe('generated ORM for a table without a create mutation', () => { + const fileTable: Table = { + name: 'File', + fields: [ + { name: 'id', type: uuid }, + { name: 'filename', type: string } + ], + relations: { belongsTo: [], hasOne: [], hasMany: [], manyToMany: [] }, + query: { + all: 'files', + one: 'file', + create: null, + update: 'updateFile', + delete: 'deleteFile', + bulkInsert: 'bulkCreateFiles', + bulkUpsert: 'bulkUpsertFiles' + } + }; + const { files } = generateOrm({ tables: [fileTable], config: {} as any }); + const model = files.find((f) => f.path === 'models/file.ts'); + + it('has no create, bulkCreate or bulkUpsert', () => { + expect(model).toBeDefined(); + expect(model!.content).not.toContain('CreateFileInput'); + expect(model!.content).not.toContain('bulkCreate'); + expect(model!.content).not.toContain('bulkUpsert'); + expect(model!.content).toContain('update<'); + }); + + it('type-checks', () => { + expect(typeCheck(files)).toEqual([]); + }); +}); diff --git a/graphql/codegen/src/core/codegen/barrel.ts b/graphql/codegen/src/core/codegen/barrel.ts index 74bca315a7..3e7ba434e4 100644 --- a/graphql/codegen/src/core/codegen/barrel.ts +++ b/graphql/codegen/src/core/codegen/barrel.ts @@ -10,12 +10,12 @@ import { addJSDocComment, generateCode } from './babel-ast'; import { getOperationHookName } from './type-resolver'; import { getCreateMutationHookName, - hasCreateMutation, getDeleteMutationHookName, getListQueryHookName, getSingleQueryHookName, getSubscriptionHookName, getUpdateMutationHookName, + hasCreateMutation, hasValidPrimaryKey, } from './utils'; diff --git a/graphql/codegen/src/core/codegen/cli/docs-generator.ts b/graphql/codegen/src/core/codegen/cli/docs-generator.ts index 4fdee3d4ac..bdab552385 100644 --- a/graphql/codegen/src/core/codegen/cli/docs-generator.ts +++ b/graphql/codegen/src/core/codegen/cli/docs-generator.ts @@ -16,6 +16,8 @@ import { getReadmeHeader, } from '../docs-utils'; import { + getBulkInsertMutationName, + getBulkUpsertMutationName, getPrimaryKeyInfo, getScalarFields, getTableNames, @@ -137,10 +139,10 @@ export function generateReadme( } lines.push(`| \`update\` | Update an existing ${singularName} |`); lines.push(`| \`delete\` | Delete a ${singularName} |`); - if (table.query?.bulkInsert) { + if (getBulkInsertMutationName(table)) { lines.push(`| \`bulk-create\` | Bulk create ${singularName} records |`); } - if (table.query?.bulkUpsert) { + if (getBulkUpsertMutationName(table)) { lines.push(`| \`bulk-upsert\` | Bulk upsert ${singularName} records |`); } if (table.query?.bulkUpdate) { @@ -474,8 +476,8 @@ export function generateSkills( ...(table.query?.create !== null ? [`${toolName} ${kebab} create ${createFlags}`] : []), `${toolName} ${kebab} update --${pk.name} <${cleanTypeName(pk.gqlType)}> ${editableFields.map((f) => `[--${f.name} <${cleanTypeName(f.type.gqlType)}>]`).join(' ')}`, `${toolName} ${kebab} delete --${pk.name} <${cleanTypeName(pk.gqlType)}>`, - ...(table.query?.bulkInsert ? [`${toolName} ${kebab} bulk-create --data ''`] : []), - ...(table.query?.bulkUpsert ? [`${toolName} ${kebab} bulk-upsert --data ''`] : []), + ...(getBulkInsertMutationName(table) ? [`${toolName} ${kebab} bulk-create --data ''`] : []), + ...(getBulkUpsertMutationName(table) ? [`${toolName} ${kebab} bulk-upsert --data ''`] : []), ...(table.query?.bulkUpdate ? [`${toolName} ${kebab} bulk-update --where '' --data ''`] : []), ...(table.query?.bulkDelete ? [`${toolName} ${kebab} bulk-delete --where ''`] : []), ], @@ -812,10 +814,10 @@ export function generateMultiTargetReadme( lines.push(`| \`create\` | Create a new ${singularName} |`); lines.push(`| \`update\` | Update an existing ${singularName} |`); lines.push(`| \`delete\` | Delete a ${singularName} |`); - if (table.query?.bulkInsert) { + if (getBulkInsertMutationName(table)) { lines.push(`| \`bulk-create\` | Bulk create ${singularName} records |`); } - if (table.query?.bulkUpsert) { + if (getBulkUpsertMutationName(table)) { lines.push(`| \`bulk-upsert\` | Bulk upsert ${singularName} records |`); } if (table.query?.bulkUpdate) { diff --git a/graphql/codegen/src/core/codegen/cli/table-command-generator.ts b/graphql/codegen/src/core/codegen/cli/table-command-generator.ts index 8eba6a0c5c..73ba2f507a 100644 --- a/graphql/codegen/src/core/codegen/cli/table-command-generator.ts +++ b/graphql/codegen/src/core/codegen/cli/table-command-generator.ts @@ -6,6 +6,8 @@ import { generateCode } from '../babel-ast'; import type { SpecialFieldGroup } from '../docs-utils'; import { categorizeSpecialFields } from '../docs-utils'; import { + getBulkInsertMutationName, + getBulkUpsertMutationName, getCreateInputTypeName, getDeleteInputTypeName, getExtraInputKeys, @@ -1752,8 +1754,8 @@ export function generateTableCommand(table: Table, options?: TableCommandOptions } // Detect bulk mutations - const hasBulkCreate = !!table.query?.bulkInsert; - const hasBulkUpsert = !!table.query?.bulkUpsert; + const hasBulkCreate = !!getBulkInsertMutationName(table); + const hasBulkUpsert = !!getBulkUpsertMutationName(table); const hasBulkUpdate = !!table.query?.bulkUpdate; const hasBulkDelete = !!table.query?.bulkDelete; diff --git a/graphql/codegen/src/core/codegen/hooks-docs-generator.ts b/graphql/codegen/src/core/codegen/hooks-docs-generator.ts index eb010e1fbb..c45ddd506d 100644 --- a/graphql/codegen/src/core/codegen/hooks-docs-generator.ts +++ b/graphql/codegen/src/core/codegen/hooks-docs-generator.ts @@ -14,8 +14,10 @@ import { import { getBulkCreateMutationHookName, getBulkDeleteMutationHookName, + getBulkInsertMutationName, getBulkUpdateMutationHookName, getBulkUpsertMutationHookName, + getBulkUpsertMutationName, getCreateMutationHookName, getDeleteMutationHookName, getListQueryHookName, @@ -93,12 +95,12 @@ export function generateHooksReadme( `| \`${getDeleteMutationHookName(table)}\` | Mutation | ${table.description || `Delete a ${singularName}`} |`, ); } - if (table.query?.bulkInsert) { + if (getBulkInsertMutationName(table)) { lines.push( `| \`${getBulkCreateMutationHookName(table)}\` | Mutation | Bulk create ${pluralName} |`, ); } - if (table.query?.bulkUpsert) { + if (getBulkUpsertMutationName(table)) { lines.push( `| \`${getBulkUpsertMutationHookName(table)}\` | Mutation | Bulk upsert ${pluralName} |`, ); @@ -169,7 +171,7 @@ export function generateHooksReadme( `create({ ${scalarFields.filter((f) => f.name !== pk.name && f.name !== 'nodeId' && f.name !== 'createdAt' && f.name !== 'updatedAt').map((f) => `${f.name}: ${fieldPlaceholder(f)}`).join(', ')} });`, ); } - if (table.query?.bulkInsert) { + if (getBulkInsertMutationName(table)) { lines.push(''); lines.push(`// Bulk create ${pluralName}`); lines.push( @@ -320,8 +322,8 @@ export function generateHooksSkills( `${getDeleteMutationHookName(table)}({})`, ] : []), - ...(table.query?.bulkInsert ? [`${getBulkCreateMutationHookName(table)}() — bulk create with data array`] : []), - ...(table.query?.bulkUpsert ? [`${getBulkUpsertMutationHookName(table)}() — bulk upsert with onConflict`] : []), + ...(getBulkInsertMutationName(table) ? [`${getBulkCreateMutationHookName(table)}() — bulk create with data array`] : []), + ...(getBulkUpsertMutationName(table) ? [`${getBulkUpsertMutationHookName(table)}() — bulk upsert with onConflict`] : []), ...(table.query?.bulkUpdate ? [`${getBulkUpdateMutationHookName(table)}() — bulk update with where + data`] : []), ...(table.query?.bulkDelete ? [`${getBulkDeleteMutationHookName(table)}() — bulk delete with where`] : []), ], diff --git a/graphql/codegen/src/core/codegen/mutation-keys.ts b/graphql/codegen/src/core/codegen/mutation-keys.ts index f78807fed2..4cd56987e4 100644 --- a/graphql/codegen/src/core/codegen/mutation-keys.ts +++ b/graphql/codegen/src/core/codegen/mutation-keys.ts @@ -18,7 +18,7 @@ import { generateCode, typedParam, } from './babel-ast'; -import { getGeneratedFileHeader, getTableNames, lcFirst } from './utils'; +import { getBulkInsertMutationName, getBulkUpsertMutationName, getGeneratedFileHeader, getTableNames, lcFirst } from './utils'; export interface MutationKeyGeneratorOptions { tables: Table[]; @@ -142,8 +142,8 @@ function generateEntityMutationKeysDeclaration( // Bulk mutation keys (only if table has bulk operations) const bulkOps: Array<{ key: string; queryField: string | null | undefined }> = [ - { key: 'bulkCreate', queryField: table.query?.bulkInsert }, - { key: 'bulkUpsert', queryField: table.query?.bulkUpsert }, + { key: 'bulkCreate', queryField: getBulkInsertMutationName(table) }, + { key: 'bulkUpsert', queryField: getBulkUpsertMutationName(table) }, { key: 'bulkUpdate', queryField: table.query?.bulkUpdate }, { key: 'bulkDelete', queryField: table.query?.bulkDelete }, ]; diff --git a/graphql/codegen/src/core/codegen/mutations.ts b/graphql/codegen/src/core/codegen/mutations.ts index a20021c3dc..d97b1d7a25 100644 --- a/graphql/codegen/src/core/codegen/mutations.ts +++ b/graphql/codegen/src/core/codegen/mutations.ts @@ -43,10 +43,12 @@ import { getBulkCreateMutationHookName, getBulkDeleteMutationFileName, getBulkDeleteMutationHookName, + getBulkInsertMutationName, getBulkUpdateMutationFileName, getBulkUpdateMutationHookName, getBulkUpsertMutationFileName, getBulkUpsertMutationHookName, + getBulkUpsertMutationName, getCreateMutationFileName, getCreateMutationHookName, getCreateMutationName, @@ -903,8 +905,8 @@ function generateBulkMutationHook( const mutationFieldName = (() => { switch (op) { - case 'bulkCreate': return table.query?.bulkInsert; - case 'bulkUpsert': return table.query?.bulkUpsert; + case 'bulkCreate': return getBulkInsertMutationName(table); + case 'bulkUpsert': return getBulkUpsertMutationName(table); case 'bulkUpdate': return table.query?.bulkUpdate; case 'bulkDelete': return table.query?.bulkDelete; } diff --git a/graphql/codegen/src/core/codegen/orm/docs-generator.ts b/graphql/codegen/src/core/codegen/orm/docs-generator.ts index a786047582..420dd3778e 100644 --- a/graphql/codegen/src/core/codegen/orm/docs-generator.ts +++ b/graphql/codegen/src/core/codegen/orm/docs-generator.ts @@ -15,6 +15,8 @@ import { pkPlaceholder, } from '../docs-utils'; import { + getBulkInsertMutationName, + getBulkUpsertMutationName, getPrimaryKeyInfo, getScalarFields, getTableNames, @@ -48,8 +50,8 @@ export function generateOrmReadme( for (const table of tables) { const { singularName } = getTableNames(table); const bulkOps: string[] = []; - if (table.query?.bulkInsert) bulkOps.push('bulkCreate'); - if (table.query?.bulkUpsert) bulkOps.push('bulkUpsert'); + if (getBulkInsertMutationName(table)) bulkOps.push('bulkCreate'); + if (getBulkUpsertMutationName(table)) bulkOps.push('bulkUpsert'); if (table.query?.bulkUpdate) bulkOps.push('bulkUpdate'); if (table.query?.bulkDelete) bulkOps.push('bulkDelete'); const ops = ['findMany', 'findOne', ...(table.query?.create !== null ? ['create'] : []), 'update', 'delete', ...bulkOps].join(', '); @@ -113,14 +115,14 @@ export function generateOrmReadme( lines.push( `const deleted = await db.${singularName}.delete({ where: { ${pk.name}: ${pkPlaceholder(pk)} } }).execute();`, ); - if (table.query?.bulkInsert) { + if (getBulkInsertMutationName(table)) { lines.push(''); lines.push(`// Bulk Create`); lines.push( `const bulkCreated = await db.${singularName}.bulkCreate({ data: [{ ${editableFields.map((f) => `${f.name}: ${fieldPlaceholder(f)}`).join(', ')} }], select: { ${pk.name}: true } }).execute();`, ); } - if (table.query?.bulkUpsert) { + if (getBulkUpsertMutationName(table)) { lines.push(''); lines.push(`// Bulk Upsert`); lines.push( @@ -303,10 +305,10 @@ export function generateOrmSkills( : ormSkillBaseDesc; const bulkUsageLines: string[] = []; - if (table.query?.bulkInsert) { + if (getBulkInsertMutationName(table)) { bulkUsageLines.push(`db.${modelName}.bulkCreate({ data: [...], select: { id: true } }).execute()`); } - if (table.query?.bulkUpsert) { + if (getBulkUpsertMutationName(table)) { bulkUsageLines.push(`db.${modelName}.bulkUpsert({ data: [...], onConflict: { constraint: '...', action: 'UPDATE' }, select: { id: true } }).execute()`); } if (table.query?.bulkUpdate) { @@ -385,7 +387,7 @@ export function generateOrmSkills( // Generate the overview SKILL.md const tableNames = tables.map((t) => lcFirst(getTableNames(t).singularName)); - const hasBulkTables = tables.some((t) => t.query?.bulkInsert || t.query?.bulkUpsert || t.query?.bulkUpdate || t.query?.bulkDelete); + const hasBulkTables = tables.some((t) => getBulkInsertMutationName(t) || getBulkUpsertMutationName(t) || t.query?.bulkUpdate || t.query?.bulkDelete); files.push({ fileName: `${skillName}/SKILL.md`, content: buildSkillFile( diff --git a/graphql/codegen/src/core/codegen/orm/model-generator.ts b/graphql/codegen/src/core/codegen/orm/model-generator.ts index 13ea41eee6..b5b2a57283 100644 --- a/graphql/codegen/src/core/codegen/orm/model-generator.ts +++ b/graphql/codegen/src/core/codegen/orm/model-generator.ts @@ -10,6 +10,8 @@ import { singularize } from 'inflekt'; import type { Table, TypeRegistry } from '../../../types/schema'; import { generateCode } from '../babel-ast'; import { + getBulkInsertMutationName, + getBulkUpsertMutationName, getCreateInputTypeName, getCreateMutationName, getDeleteInputTypeName, @@ -219,8 +221,8 @@ export function generateModelFile( }); // Detect which bulk mutations are available for this table - const bulkInsertMutationName = table.query?.bulkInsert ?? null; - const bulkUpsertMutationName = table.query?.bulkUpsert ?? null; + const bulkInsertMutationName = getBulkInsertMutationName(table) ?? null; + const bulkUpsertMutationName = getBulkUpsertMutationName(table) ?? null; const bulkUpdateMutationName = table.query?.bulkUpdate ?? null; const bulkDeleteMutationName = table.query?.bulkDelete ?? null; const hasBulk = !!(bulkInsertMutationName || bulkUpsertMutationName || bulkUpdateMutationName || bulkDeleteMutationName); diff --git a/graphql/codegen/src/core/codegen/utils.ts b/graphql/codegen/src/core/codegen/utils.ts index 76af358ab1..2fe6fc3edb 100644 --- a/graphql/codegen/src/core/codegen/utils.ts +++ b/graphql/codegen/src/core/codegen/utils.ts @@ -196,6 +196,15 @@ export function hasCreateMutation(table: Table): boolean { return table.query?.create !== null; } +/** Bulk create/upsert take `Create
Input`, so a table without create has neither. */ +export function getBulkInsertMutationName(table: Table): string | null { + return hasCreateMutation(table) ? table.query?.bulkInsert ?? null : null; +} + +export function getBulkUpsertMutationName(table: Table): string | null { + return hasCreateMutation(table) ? table.query?.bulkUpsert ?? null : null; +} + /** * Get the GraphQL mutation name for creating */