Skip to content

Commit

Permalink
fix(tanstack,swr): create/upsert hooks shouldn't be generated for del…
Browse files Browse the repository at this point in the history
…egate models
  • Loading branch information
ymc9 committed Jul 9, 2024
1 parent 4f56c15 commit cb979ab
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
9 changes: 6 additions & 3 deletions packages/plugins/swr/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ensureEmptyDir,
generateModelMeta,
getDataModels,
isDelegateModel,
requireOption,
resolvePath,
saveProject,
Expand Down Expand Up @@ -77,15 +78,17 @@ function generateModelHooks(

const mutationFuncs: string[] = [];

// Note: delegate models don't support create and upsert operations

// create is somehow named "createOne" in the DMMF
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (mapping.create || (mapping as any).createOne) {
if (!isDelegateModel(model) && (mapping.create || (mapping as any).createOne)) {
const argsType = `Prisma.${model.name}CreateArgs`;
mutationFuncs.push(generateMutation(sf, model, 'POST', 'create', argsType, false));
}

// createMany
if (mapping.createMany && supportCreateMany(model.$container)) {
if (!isDelegateModel(model) && mapping.createMany && supportCreateMany(model.$container)) {
const argsType = `Prisma.${model.name}CreateManyArgs`;
mutationFuncs.push(generateMutation(sf, model, 'POST', 'createMany', argsType, true));
}
Expand Down Expand Up @@ -138,7 +141,7 @@ function generateModelHooks(
// upsert
// upsert is somehow named "upsertOne" in the DMMF
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (mapping.upsert || (mapping as any).upsertOne) {
if (!isDelegateModel(model) && (mapping.upsert || (mapping as any).upsertOne)) {
const argsType = `Prisma.${model.name}UpsertArgs`;
mutationFuncs.push(generateMutation(sf, model, 'POST', 'upsert', argsType, false));
}
Expand Down
9 changes: 6 additions & 3 deletions packages/plugins/tanstack-query/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ensureEmptyDir,
generateModelMeta,
getDataModels,
isDelegateModel,
requireOption,
resolvePath,
saveProject,
Expand Down Expand Up @@ -341,14 +342,16 @@ function generateModelHooks(
});
sf.addStatements(makeBaseImports(target, version));

// Note: delegate models don't support create and upsert operations

// create is somehow named "createOne" in the DMMF
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (mapping.create || (mapping as any).createOne) {
if (!isDelegateModel(model) && (mapping.create || (mapping as any).createOne)) {
generateMutationHook(target, sf, model.name, 'create', 'post', true);
}

// createMany
if (mapping.createMany && supportCreateMany(model.$container)) {
if (!isDelegateModel(model) && mapping.createMany && supportCreateMany(model.$container)) {
generateMutationHook(target, sf, model.name, 'createMany', 'post', false, 'Prisma.BatchPayload');
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,36 @@ describe('Polymorphic Plugin Interaction Test', () => {
});
});

it('swr', async () => {
const schema = `
${POLYMORPHIC_SCHEMA}
plugin hooks {
provider = '@zenstackhq/swr'
output = '$projectRoot/hooks'
}
`;

await loadSchema(schema, {
compile: true,
extraDependencies: ['swr'],
});
});

it('trpc', async () => {
const schema = `
${POLYMORPHIC_SCHEMA}
plugin trpc {
provider = '@zenstackhq/trpc'
output = '$projectRoot/routers'
generateClientHelpers = 'react'
}
`;

await loadSchema(schema, {
compile: true,
extraDependencies: ['@trpc/client', '@trpc/server'],
extraDependencies: ['@trpc/client', '@trpc/server', '@trpc/react-query'],
});
});
});

0 comments on commit cb979ab

Please sign in to comment.