Skip to content

Commit

Permalink
docs: Custom decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Apr 7, 2021
1 parent b14f0fe commit d72d390
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 15 deletions.
145 changes: 130 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Default: `false`

#### `types_*`

Map prisma types in [flatten](https://github.com/hughsk/flat) style
Map prisma scalar types in [flatten](https://github.com/hughsk/flat) style

- `types_{type}_fieldType` TypeScript field type name
- `types_{type}_fieldModule` Module to import
Expand Down Expand Up @@ -146,7 +146,7 @@ Generated fields:
field: Date;
```

## Field Metadata
## Field Settings

Special directives in triple slash comments for more precise code generation.

Expand All @@ -155,16 +155,18 @@ Special directives in triple slash comments for more precise code generation.
Removes field from GraphQL schema.
Alias: `@TypeGraphQL.omit(output: true)`

Note: Field will be decorated for hide only in output types (type in schema),
but not in inputs.
By default field will be decorated for hide only in output types (type in schema),
add `input: true` to hide field on input types.

Example:

```prisma
model User {
id String @id @default(cuid())
/// @HideField()
password String
password String
/// @HideField({ output: true, input: true })
secret String
}
```

Expand All @@ -175,9 +177,132 @@ Generates fields:
export class User {
@HideField()
password: string;
@HideField()
secret: string;
}
```

#### Custom Decorators

To applying custom decorators requires configuration of generator.

```sh
generator nestgraphql {
fields_{Namespace}_from = "module specifier"
fields_{Namespace}_input = true | false
fields_{Namespace}_output = true | false
fields_{Namespace}_defaultImport = "default import name"
fields_{Namespace}_namespaceImport = "namespace import name"
}
```

Create configuration map in [flatten](https://github.com/hughsk/flat) style for `{Namespace}`.
Where `{Namespace}` is a namespace used in field triple slash comment.

##### `fields_{Namespace}_from`

Required. Name of the module, which will be used in import (`class-validator`, `graphql-scalars`, etc.)

##### `fields_{Namespace}_input`

Optional, default: `false`. Means that it will be applied on input types (classes decorated by `InputType`)

##### `fields_{Namespace}_output`

Optional, default: `false`. Means that it will be applied on output types (classes decorated by `ObjectType`)

##### `fields_{Namespace}_defaultImport`

Default import name, if module have no namespace

##### `fields_{Namespace}_namespaceImport`

Optional. Import all as this namespace from module

Example:

```prisma
generator nestgraphql {
fields_Validator_from = "class-validator"
fields_Validator_input = true
}
model User {
id Int @id
/// @Validator.MinLength(3)
name String
}
```

May generate following class:

```ts
import { InputType, Field } from '@nestjs/graphql';
import * as Validator from 'class-validator';

@InputType()
export class UserCreateInput {
@Field(() => String, { nullable: false })
@Validator.MinLength(3)
name!: string;
}
```

#### @FieldType

Allow set custom type for field

```prisma
model User {
id Int @id
/// @FieldType({ name: 'Scalars.EmailAddress', input: true, from: 'graphql-scalars' })
email String
}
```

May generate class:

```ts
import { InputType, Field } from '@nestjs/graphql';
import * as Scalars from 'graphql-scalars';

@InputType()
export class UserCreateInput {
@Field(() => Scalars.GraphQLEmailAddress, { nullable: false })
email!: string;
}
```

And GraphQL schema:

```grapqhl
scalar EmailAddress
input UserCreateInput {
email: EmailAddress!
}
```

Same field type may be used in different models and it is not convenient to specify every time all options.
There is a shortcut:

```grapqhl
generator nestgraphql {
fields_Scalars_from = "graphql-scalars"
fields_Scalars_input = true
fields_Scalars_output = true
}
model User {
id Int @id
/// @FieldType('Scalars.EmailAddress')
email String
}
```

The result will be the same. `Scalars` is the namespace here.
Missing field options will merged from generator configuration.

## Similar Projects

- https://github.com/wSedlacek/prisma-generators/tree/master/libs/nestjs
Expand All @@ -194,13 +319,3 @@ export class User {
- JSON type for the code first approach - https://github.com/nestjs/graphql/issues/111#issuecomment-631452899
- https://github.com/paljs/prisma-tools/tree/master/packages/plugins
- https://github.com/wasp-lang/wasp

## TODO

- new format of custom type

```
type_{schemaType}_{key} = "{value}"
schemaType
```
6 changes: 6 additions & 0 deletions src/example/user/user.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { User } from '../../@generated/user/user.model';
import { UserUpdateInput } from '../../@generated/user/user-update.input';
import { UserWhereInput } from '../../@generated/user/user-where.input';
import { UserDateInput } from './user-date.input';
import { UserCreateInput } from '../../@generated/user/user-create.input';

const prisma = new PrismaClient({
errorFormat: 'colorless',
Expand Down Expand Up @@ -47,6 +48,11 @@ export class UserResolver {
return;
}

@Mutation(() => User, { nullable: true })
async userCreate(@Args('user') user: UserCreateInput): Promise<any> {
return;
}

@Mutation(() => User, { nullable: true })
async userInfo(@Args('user') user: UserDateInput): Promise<any> {
console.log(
Expand Down

0 comments on commit d72d390

Please sign in to comment.