Skip to content

Commit

Permalink
Add emitIsAbstract generator option
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Jun 19, 2023
1 parent 55aff80 commit 19e841b
Show file tree
Hide file tree
Showing 20 changed files with 452 additions and 781 deletions.
4 changes: 1 addition & 3 deletions docs/advanced/emit-id-type.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ generator typegraphql {
It will generate then the id fields using the `TypeGraphQL.ID` GraphQL scalar, e.g.:

```ts {5}
@TypeGraphQL.ObjectType("Post", {
isAbstract: true,
})
@TypeGraphQL.ObjectType("Post", {})
export class Post {
@TypeGraphQL.Field(_type => TypeGraphQL.ID, {
nullable: false,
Expand Down
37 changes: 37 additions & 0 deletions docs/advanced/emit-is-abstract.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: Emit `isAbstract` decorator option
sidebar_label: Emit isAbstract
---

By default, TypeGraphQL Prisma generator aims to work with newest release of TypeGraphQL.
However, for backward compatibility reasons, there are some config options introduced.

One of such options is `emitIsAbstract` generator option:

```prisma {3}
generator typegraphql {
provider = "typegraphql-prisma"
emitIsAbstract = true
}
```

When this option is set to true, it generates `isAbstract: true` decorator option for `@ObjectType` (models, outputs) and `@InputType` classes:

```ts {2}
@TypeGraphQL.ObjectType("Post", {
isAbstract: true,
})
export class Post {
@TypeGraphQL.Field(_type => TypeGraphQL.ID, {
nullable: false,
})
uuid!: string;

// ...
}
```

This decorator option prevents from emitting those types in GraphQL schema if they are not referenced directly in the other types consumed by resolvers.
It only matters if you use the resolvers auto discovery feature (e.g. `resolvers: ["./src/**/*.resolver.ts"]`), that was supported in TypeGraphQL up to the `v2.0.0-beta.1` and removed in newer releases.

Hence this option is set to `false` by default, so if you still using some old version of TypeGraphQL together with resolvers auto discovery feature, you need to enable `emitIsAbstract = true` in your Prisma schema.
16 changes: 4 additions & 12 deletions docs/advanced/simple-inputs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ By using this option, instead of generating nested inputs with `IntFieldUpdateOp
<TabItem value="true" label="useSimpleInputs = true">

```ts
@TypeGraphQL.InputType("CategoryUpdateInput", {
isAbstract: true,
})
@TypeGraphQL.InputType("CategoryUpdateInput", {})
export class CategoryUpdateInput {
@TypeGraphQL.Field(_type => String, {
nullable: true,
Expand All @@ -50,9 +48,7 @@ export class CategoryUpdateInput {
<TabItem value="false" label="useSimpleInputs = false">

```ts
@TypeGraphQL.InputType("CategoryUpdateInput", {
isAbstract: true,
})
@TypeGraphQL.InputType("CategoryUpdateInput", {})
export class CategoryUpdateInput {
@TypeGraphQL.Field(_type => StringFieldUpdateOperationsInput, {
nullable: true,
Expand Down Expand Up @@ -81,9 +77,7 @@ By using this option, instead of generating nested inputs with `XYZEnvelopeInput
<TabItem value="true" label="useSimpleInputs = true">

```ts
@TypeGraphQL.InputType("UserUpdateInput", {
isAbstract: true,
})
@TypeGraphQL.InputType("UserUpdateInput", {})
export class UserUpdateInput {
@TypeGraphQL.Field(_type => [TypeGraphQL.Int], {
nullable: true,
Expand All @@ -101,9 +95,7 @@ export class UserUpdateInput {
<TabItem value="false" label="useSimpleInputs = false">

```ts
@TypeGraphQL.InputType("UserUpdateInput", {
isAbstract: true,
})
@TypeGraphQL.InputType("UserUpdateInput", {})
export class UserUpdateInput {
@TypeGraphQL.Field(_type => UserCreateluckyNumbersInput, {
nullable: true,
Expand Down
1 change: 0 additions & 1 deletion docs/advanced/simple-resolvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ It will generate then all the output type and model type classes with `simpleRes

```ts {4}
@TypeGraphQL.ObjectType({
isAbstract: true,
description: undefined,
simpleResolvers: true,
})
Expand Down
4 changes: 1 addition & 3 deletions docs/advanced/unchecked-scalars.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ generator typegraphql {
This way there will be generate input classes with relation id fields, instead of the normal "nested" inputs for creating/updating relations, e.g.:

```ts
@TypeGraphQL.InputType({
isAbstract: true,
})
@TypeGraphQL.InputType({})
export class PostUncheckedCreateInput {
@TypeGraphQL.Field(_type => String, {
nullable: false,
Expand Down
1 change: 0 additions & 1 deletion docs/basics/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ TypeGraphQL.registerEnumType(UserKind, {

```ts title=generated/type-graphql/models/User.ts
@TypeGraphQL.ObjectType({
isAbstract: true,
description: undefined,
})
export class User {
Expand Down
1 change: 1 addition & 0 deletions src/cli/prisma-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export async function generate(options: GeneratorOptions) {
"formatGeneratedCode",
["prettier", "tsc"] as const,
),
emitIsAbstract: parseStringBoolean(generatorConfig.emitIsAbstract) ?? false,
};
const internalConfig: InternalGeneratorOptions = {
outputDirPath: outputDir,
Expand Down
4 changes: 3 additions & 1 deletion src/generator/model-type-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export default function generateObjectTypeClassFromModel(
arguments: [
`"${model.typeName}"`,
Writers.object({
isAbstract: "true",
...(dmmfDocument.options.emitIsAbstract && {
isAbstract: "true",
}),
...(model.docs && { description: `"${model.docs}"` }),
...(dmmfDocument.options.simpleResolvers && {
simpleResolvers: "true",
Expand Down
1 change: 1 addition & 0 deletions src/generator/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface ExternalGeneratorOptions {
omitInputFieldsByDefault?: string[];
omitOutputFieldsByDefault?: string[];
formatGeneratedCode?: boolean | "prettier" | "tsc";
emitIsAbstract?: boolean;
}

export interface InternalGeneratorOptions {
Expand Down
8 changes: 6 additions & 2 deletions src/generator/type-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ export function generateOutputTypeClassFromType(
arguments: [
`"${type.typeName}"`,
Writers.object({
isAbstract: "true",
...(dmmfDocument.options.emitIsAbstract && {
isAbstract: "true",
}),
...(dmmfDocument.options.simpleResolvers && {
simpleResolvers: "true",
}),
Expand Down Expand Up @@ -190,7 +192,9 @@ export function generateInputTypeClassFromType(
arguments: [
`"${inputType.typeName}"`,
Writers.object({
isAbstract: "true",
...(options.emitIsAbstract && {
isAbstract: "true",
}),
}),
],
},
Expand Down
5 changes: 2 additions & 3 deletions tests/regression/__snapshots__/crud.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2099,7 +2099,7 @@ const User_1 = require(\\"../../../models/User\\");
const AffectedRowsOutput_1 = require(\\"../../outputs/AffectedRowsOutput\\");
const AggregateUser_1 = require(\\"../../outputs/AggregateUser\\");
const UserGroupBy_1 = require(\\"../../outputs/UserGroupBy\\");
let UserCrudResolver = class UserCrudResolver {
let UserCrudResolver = exports.UserCrudResolver = class UserCrudResolver {
async aggregateUser(ctx, info, args) {
return (0, helpers_1.getPrismaFromContext)(ctx).user.aggregate({
...args,
Expand Down Expand Up @@ -2352,10 +2352,9 @@ tslib_1.__decorate([
tslib_1.__metadata(\\"design:paramtypes\\", [Object, Object, UpsertOneUserArgs_1.UpsertOneUserArgs]),
tslib_1.__metadata(\\"design:returntype\\", Promise)
], UserCrudResolver.prototype, \\"upsertOneUser\\", null);
UserCrudResolver = tslib_1.__decorate([
exports.UserCrudResolver = UserCrudResolver = tslib_1.__decorate([
TypeGraphQL.Resolver(_of => User_1.User)
], UserCrudResolver);
exports.UserCrudResolver = UserCrudResolver;
"
`;

Expand Down
4 changes: 1 addition & 3 deletions tests/regression/__snapshots__/emit-only.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1615,9 +1615,7 @@ import { Prisma } from \\"../../../helpers/prisma-client-mock\\";
import { DecimalJSScalar } from \\"../scalars\\";
import { Post } from \\"../models/Post\\";
@TypeGraphQL.ObjectType(\\"User\\", {
isAbstract: true
})
@TypeGraphQL.ObjectType(\\"User\\", {})
export class User {
@TypeGraphQL.Field(_type => TypeGraphQL.Int, {
nullable: false
Expand Down
Loading

0 comments on commit 19e841b

Please sign in to comment.