Skip to content

Commit 814d64e

Browse files
authored
fix(tanstack,swr): create/upsert hooks shouldn't be generated for delegate models (#1567)
1 parent 4f56c15 commit 814d64e

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

packages/plugins/swr/src/generator.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ensureEmptyDir,
66
generateModelMeta,
77
getDataModels,
8+
isDelegateModel,
89
requireOption,
910
resolvePath,
1011
saveProject,
@@ -77,15 +78,17 @@ function generateModelHooks(
7778

7879
const mutationFuncs: string[] = [];
7980

81+
// Note: delegate models don't support create and upsert operations
82+
8083
// create is somehow named "createOne" in the DMMF
8184
// eslint-disable-next-line @typescript-eslint/no-explicit-any
82-
if (mapping.create || (mapping as any).createOne) {
85+
if (!isDelegateModel(model) && (mapping.create || (mapping as any).createOne)) {
8386
const argsType = `Prisma.${model.name}CreateArgs`;
8487
mutationFuncs.push(generateMutation(sf, model, 'POST', 'create', argsType, false));
8588
}
8689

8790
// createMany
88-
if (mapping.createMany && supportCreateMany(model.$container)) {
91+
if (!isDelegateModel(model) && mapping.createMany && supportCreateMany(model.$container)) {
8992
const argsType = `Prisma.${model.name}CreateManyArgs`;
9093
mutationFuncs.push(generateMutation(sf, model, 'POST', 'createMany', argsType, true));
9194
}
@@ -138,7 +141,7 @@ function generateModelHooks(
138141
// upsert
139142
// upsert is somehow named "upsertOne" in the DMMF
140143
// eslint-disable-next-line @typescript-eslint/no-explicit-any
141-
if (mapping.upsert || (mapping as any).upsertOne) {
144+
if (!isDelegateModel(model) && (mapping.upsert || (mapping as any).upsertOne)) {
142145
const argsType = `Prisma.${model.name}UpsertArgs`;
143146
mutationFuncs.push(generateMutation(sf, model, 'POST', 'upsert', argsType, false));
144147
}

packages/plugins/tanstack-query/src/generator.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ensureEmptyDir,
77
generateModelMeta,
88
getDataModels,
9+
isDelegateModel,
910
requireOption,
1011
resolvePath,
1112
saveProject,
@@ -341,14 +342,16 @@ function generateModelHooks(
341342
});
342343
sf.addStatements(makeBaseImports(target, version));
343344

345+
// Note: delegate models don't support create and upsert operations
346+
344347
// create is somehow named "createOne" in the DMMF
345348
// eslint-disable-next-line @typescript-eslint/no-explicit-any
346-
if (mapping.create || (mapping as any).createOne) {
349+
if (!isDelegateModel(model) && (mapping.create || (mapping as any).createOne)) {
347350
generateMutationHook(target, sf, model.name, 'create', 'post', true);
348351
}
349352

350353
// createMany
351-
if (mapping.createMany && supportCreateMany(model.$container)) {
354+
if (!isDelegateModel(model) && mapping.createMany && supportCreateMany(model.$container)) {
352355
generateMutationHook(target, sf, model.name, 'createMany', 'post', false, 'Prisma.BatchPayload');
353356
}
354357

@@ -422,7 +425,7 @@ function generateModelHooks(
422425
// upsert
423426
// upsert is somehow named "upsertOne" in the DMMF
424427
// eslint-disable-next-line @typescript-eslint/no-explicit-any
425-
if (mapping.upsert || (mapping as any).upsertOne) {
428+
if (!isDelegateModel(model) && (mapping.upsert || (mapping as any).upsertOne)) {
426429
generateMutationHook(target, sf, model.name, 'upsert', 'post', true);
427430
}
428431

tests/integration/tests/enhancements/with-delegate/plugin-interaction.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,36 @@ describe('Polymorphic Plugin Interaction Test', () => {
2323
});
2424
});
2525

26+
it('swr', async () => {
27+
const schema = `
28+
${POLYMORPHIC_SCHEMA}
29+
30+
plugin hooks {
31+
provider = '@zenstackhq/swr'
32+
output = '$projectRoot/hooks'
33+
}
34+
`;
35+
36+
await loadSchema(schema, {
37+
compile: true,
38+
extraDependencies: ['swr'],
39+
});
40+
});
41+
2642
it('trpc', async () => {
2743
const schema = `
2844
${POLYMORPHIC_SCHEMA}
2945
3046
plugin trpc {
3147
provider = '@zenstackhq/trpc'
3248
output = '$projectRoot/routers'
49+
generateClientHelpers = 'react'
3350
}
3451
`;
3552

3653
await loadSchema(schema, {
3754
compile: true,
38-
extraDependencies: ['@trpc/client', '@trpc/server'],
55+
extraDependencies: ['@trpc/client', '@trpc/server', '@trpc/react-query'],
3956
});
4057
});
4158
});

0 commit comments

Comments
 (0)