Gathering detailed insights and metrics for prisma-nestjs-graphql-swagger
Gathering detailed insights and metrics for prisma-nestjs-graphql-swagger
Generate object types, inputs, args, etc. from prisma schema file for usage with @nestjs/graphql module
npm install prisma-nestjs-graphql-swagger
Typescript
Module System
TypeScript (98.82%)
JavaScript (0.85%)
Shell (0.33%)
Total Downloads
7,971
Last Day
4
Last Week
25
Last Month
166
Last Year
5,980
1 Stars
428 Commits
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.6
Package Id
prisma-nestjs-graphql-swagger@1.0.6
Unpacked Size
1.30 MB
Size
130.92 kB
File Count
601
Cumulative downloads
Total Downloads
Last day
33.3%
4
Compared to previous day
Last week
-47.9%
25
Compared to previous week
Last month
-23.9%
166
Compared to previous month
Last year
956.5%
5,980
Compared to previous year
11
62
Generate object types, inputs, args, etc. from prisma schema file for usage with @nestjs/graphql module.
npm install --save-dev prisma-nestjs-graphql
schema.prisma
file1generator nestgraphql { 2 provider = "node node_modules/prisma-nestjs-graphql" 3 // for yarn monorepos 4 // provider = "prisma-nestjs-graphql" 5 output = "../src/@generated" 6}
1npx prisma generate
Decimal
and Json
types, you need install:1npm install graphql-type-json prisma-graphql-type-decimal 2
Or write you own graphql scalar types, read more on docs.nestjs.com.
output
Output folder relative to this schema file
Type: string
outputFilePattern
File path and name pattern
Type: string
Default: {model}/{name}.{type}.ts
Possible tokens:
{model}
Model name in dashed case or 'prisma' if unknown{name}
Dashed-case name of model/input/arg without suffix{type}
Short type name (model, input, args, output){plural.type}
Plural short type name (models, inputs, enums)tsConfigFilePath
Path to tsconfig.json
(absolute path or relative to current working directory)
Type: string | undefined
Default: tsconfig.json
if exists, undefined
otherwise
combineScalarFilters
Combine nested/nullable scalar filters to single
Type: boolean
Default: false
noAtomicOperations
Remove input types for atomic operations
Type: boolean
Default: false
reExport
Create index.ts
file with re-export
Type: enum
Values:
None
Default, create nothing
Directories
Create index file in all root directories
Single
Create single index file in output directory
All
Create index file in all root directories and in output directory
Example configuration:
1generator nestgraphql { 2 provider = "node node_modules/prisma-nestjs-graphql" 3 output = "../src/@generated" 4 reExport = Directories 5}
emitSingle
Generate single file with merged classes and enums.
Type: boolean
Default: false
emitCompiled
Emit compiled JavaScript and definitions instead of TypeScript sources,
files will be compiled with emitDecoratorMetadata:false
, because there is a problem
with temporal dead zone when generating merged file.
Type: boolean
Default: false
purgeOutput
Delete all files in output
folder
Type: boolean
Default: false
noTypeId
Disable usage of graphql ID
type and use Int/Float
for fields marked as @id
in schema.
Type: boolean
Default: false
requireSingleFieldsInWhereUniqueInput
When a model *WhereUniqueInput
class has only a single field, mark that field as required (TypeScript) and not nullable (GraphQL).
See #58 for more details.
Type: boolean
Default: false
Note: It will break compatiblity between Prisma types and generated classes.
useInputType
Since GraphQL does not support input union type, this setting map allow to choose which input type is preferable.
1generator nestgraphql { 2 useInputType_{typeName}_{property} = "{pattern}" 3}
Where:
typeName
Full name or partial name of the class where need to choose input type.UserCreateInput
full name, WhereInput
partial name, matches UserWhereInput
, PostWhereInput
, etc.property
Property of the class for which need to choose type. Special case name ALL
means any / all properties.pattern
Part of name (or full) of type which should be chosen, you can use
wild card or negate symbols, in this case pattern should starts with match:
,
e.g. match:*UncheckedCreateInput
see outmatch for details.Example:
1export type PostWhereInput = { 2 author?: XOR<UserRelationFilter, UserWhereInput>; 3}; 4export type UserRelationFilter = { 5 is?: UserWhereInput; 6 isNot?: UserWhereInput; 7}; 8 9export type UserWhereInput = { 10 AND?: Enumerable<UserWhereInput>; 11 OR?: Enumerable<UserWhereInput>; 12 NOT?: Enumerable<UserWhereInput>; 13 id?: StringFilter | string; 14 name?: StringFilter | string; 15};
We have generated types above, by default property author
will be decorated as UserRelationFilter
,
to set UserWhereInput
need to configure generator the following way:
1generator nestgraphql { 2 provider = "node node_modules/prisma-nestjs-graphql" 3 output = "../src/@generated" 4 useInputType_WhereInput_ALL = "WhereInput" 5}
1@InputType() 2export class PostWhereInput { 3 @Field(() => UserWhereInput, { nullable: true }) 4 author?: UserWhereInput; 5}
decorate
Allow to attach multiple decorators to any field of any type.
1generator nestgraphql { 2 decorate_{key}_type = "outmatch pattern" 3 decorate_{key}_field = "outmatch pattern" 4 decorate_{key}_from = "module specifier" 5 decorate_{key}_name = "import name" 6 decorate_{key}_arguments = "[argument1, argument2]" 7 decorate_{key}_defaultImport = "default import name" | true 8 decorate_{key}_namespaceImport = "namespace import name" 9 decorate_{key}_namedImport = "import name" | true 10}
Where {key}
any identifier to group values (written in flatten style)
decorate_{key}_type
- outmatch pattern to match class namedecorate_{key}_field
- outmatch pattern to match field namedecorate_{key}_from
- module specifier to import from (e.g class-validator
)decorate_{key}_name
- import name or name with namespacedecorate_{key}_defaultImport
- import as defaultdecorate_{key}_namespaceImport
- use this name as import namespacedecorate_{key}_namedImport
- named import (without namespace)decorate_{key}_arguments
- arguments for decorator (if decorator need to be called as function){propertyType.0}
- field's type (TypeScript type annotation)Example of generated class:
1@ArgsType() 2export class CreateOneUserArgs { 3 @Field(() => UserCreateInput, { nullable: false }) 4 data!: UserCreateInput; 5}
To make it validateable (assuming UserCreateInput
already contains validation decorators from class-validator
),
it is necessary to add @ValidateNested()
and @Type()
from class-transformer
.
1decorate_1_type = "CreateOneUserArgs" 2decorate_1_field = data 3decorate_1_name = ValidateNested 4decorate_1_from = "class-validator" 5decorate_1_arguments = "[]" 6decorate_2_type = "CreateOneUserArgs" 7decorate_2_field = data 8decorate_2_from = "class-transformer" 9decorate_2_arguments = "['() => {propertyType.0}']" 10decorate_2_name = Type
Result:
1import { ValidateNested } from 'class-validator'; 2import { Type } from 'class-transformer'; 3 4@ArgsType() 5export class CreateOneUserArgs { 6 @Field(() => UserCreateInput, { nullable: false }) 7 @ValidateNested() 8 @Type(() => UserCreateInput) 9 data!: UserCreateInput; 10}
Another example:
1decorate_2_namespaceImport = "Transform" 2decorate_2_name = "Transform.Type"
1import * as Transform from 'class-transformer'; 2 3@Transform.Type(() => UserCreateInput) 4data!: UserCreateInput; 5
Add @HideField()
decorator to nested types:
decorate_3_type = "*CreateNestedOneWithoutUserInput"
decorate_3_field = "!(create)"
decorate_3_name = "HideField"
decorate_3_from = "@nestjs/graphql"
decorate_3_arguments = "[]"
May generate following class:
1@Field(() => ProfileCreateWithoutUserInput, { nullable: true }) 2create?: ProfileCreateWithoutUserInput; 3 4@HideField() 5connectOrCreate?: ProfileCreateOrConnectWithoutUserInput; 6 7@HideField() 8connect?: ProfileWhereUniqueInput;
graphqlScalars
Allow to set custom graphql type for Prisma scalar type. Format:
graphqlScalars_{type}_name = "string"
graphqlScalars_{type}_specifier = "string"
where {type}
is a prisma scalar type name (e.g. BigInt)
Example:
graphqlScalars_BigInt_name = "GraphQLBigInt"
graphqlScalars_BigInt_specifier = "graphql-scalars"
May generate:
1import { GraphQLBigInt } from 'graphql-scalars'; 2 3export class BigIntFilter { 4 @Field(() => GraphQLBigInt, { nullable: true }) 5 equals?: bigint | number; 6}
It will affect all inputs and outputs types (including models).
Comments with triple slash will projected to typescript code comments
and some @Field()
decorator options
For example:
1model Product { 2 /// Old description 3 /// @deprecated Use new name instead 4 oldName String 5}
May produce:
1@ObjectType() 2export class Product { 3 /** 4 * Old description 5 * @deprecated Use new name instead 6 */ 7 @Field(() => String, { 8 description: 'Old description', 9 deprecationReason: 'Use new name instead', 10 }) 11 oldName: string; 12}
Special directives in triple slash comments for more precise code generation.
Removes field from GraphQL schema.
Alias: @TypeGraphQL.omit(output: true)
By default (without arguments) field will be decorated for hide only in output types (type in schema).
To hide field in input types add input: true
.
To hide field in specific type you can use glob pattern match: string | string[]
see outmatch for details.
Examples:
@HideField()
same as @HideField({ output: true })
@HideField({ input: true, output: true })
@HideField({ match: 'UserCreate*Input' })
1model User { 2 id String @id @default(cuid()) 3 /// @HideField() 4 password String 5 /// @HideField({ output: true, input: true }) 6 secret String 7 /// @HideField({ match: '@(User|Comment)Create*Input' }) 8 createdAt DateTime @default(now()) 9}
May generate classes:
1@ObjectType() 2export class User { 3 @HideField() 4 password: string; 5 @HideField() 6 secret: string; 7 @Field(() => Date, { nullable: false }) 8 createdAt: Date; 9}
1@InputType() 2export class UserCreateInput { 3 @Field() 4 password: string; 5 @HideField() 6 secret: string; 7 @HideField() 8 createdAt: Date; 9}
Applying custom decorators requires configuration of generator.
1generator nestgraphql { 2 fields_{namespace}_from = "module specifier" 3 fields_{namespace}_input = true | false 4 fields_{namespace}_output = true | false 5 fields_{namespace}_model = true | false 6 fields_{namespace}_defaultImport = "default import name" | true 7 fields_{namespace}_namespaceImport = "namespace import name" 8 fields_{namespace}_namedImport = true | false 9}
Create configuration map in flatten 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.)
Type: string
fields_{namespace}_input
Means that it will be applied on input types (classes decorated by InputType
)
Type: boolean
Default: false
fields_{namespace}_output
Means that it will be applied on output types (classes decorated by ObjectType
),
including models
Type: boolean
Default: false
fields_{namespace}_model
Means that it will be applied only on model types (classes decorated by ObjectType
)
Type: boolean
Default: false
fields_{namespace}_defaultImport
Default import name, if module have no namespace.
Type: undefined | string | true
Default: undefined
If defined as true
then import name will be same as {namespace}
fields_{namespace}_namespaceImport
Import all as this namespace from module
Type: undefined | string
Default: Equals to {namespace}
fields_{namespace}_namedImport
If imported module has internal namespace, this allow to generate named import,
imported name will be equal to {namespace}
, see example of usage
Type: boolean
Default: false
Custom decorators example:
1generator nestgraphql { 2 fields_Validator_from = "class-validator" 3 fields_Validator_input = true 4} 5 6model User { 7 id Int @id 8 /// @Validator.MinLength(3) 9 name String 10}
May generate following class:
1import { InputType, Field } from '@nestjs/graphql'; 2import * as Validator from 'class-validator'; 3 4@InputType() 5export class UserCreateInput { 6 @Field(() => String, { nullable: false }) 7 @Validator.MinLength(3) 8 name!: string; 9}
Custom decorators can be applied on classes (models):
/// @NG.Directive('@extends')
/// @NG.Directive('@key(fields: "id")')
model User {
/// @NG.Directive('@external')
id String @id
}
generator nestgraphql {
fields_NG_from = "@nestjs/graphql"
fields_NG_output = false
fields_NG_model = true
}
May generate:
1import * as NG from '@nestjs/graphql'; 2 3@NG.Directive('@extends') 4@NG.Directive('@key(fields: "id")') 5export class User { 6 @Field(() => ID, { nullable: false }) 7 @NG.Directive('@external') 8 id!: string;
Allow set custom GraphQL scalar type for field
To override scalar type in specific classes, you can use glob pattern match: string | string[]
see outmatch for details.
1model User { 2 id Int @id 3 /// @FieldType({ name: 'Scalars.GraphQLEmailAddress', from: 'graphql-scalars', input: true }) 4 email String 5}
May generate following class:
1import { InputType, Field } from '@nestjs/graphql'; 2import * as Scalars from 'graphql-scalars'; 3 4@InputType() 5export class UserCreateInput { 6 @Field(() => Scalars.GraphQLEmailAddress, { nullable: false }) 7 email!: string; 8}
And following GraphQL schema:
1scalar EmailAddress 2 3input UserCreateInput { 4 email: EmailAddress! 5}
Same field type may be used in different models and it is not convenient to specify every time all options. There is a shortcut:
1generator nestgraphql { 2 fields_Scalars_from = "graphql-scalars" 3 fields_Scalars_input = true 4 fields_Scalars_output = true 5} 6 7model User { 8 id Int @id 9 /// @FieldType('Scalars.GraphQLEmailAddress') 10 email String 11}
The result will be the same. Scalars
is the namespace here.
Missing field options will merged from generator configuration.
Similar to @FieldType()
but refer to TypeScript property (actually field too).
To override TypeScript type in specific classes, you can use glob pattern match: string | string[]
see outmatch for details.
Example:
generator nestgraphql {
fields_TF_from = "type-fest"
}
model User {
id String @id
/// @PropertyType('TF.JsonObject')
data Json
}
May generate:
1import * as TF from 'type-fest'; 2 3@ObjectType() 4export class User { 5 @Field(() => GraphQLJSON) 6 data!: TF.JsonObject; 7}
Allow attach @Directive
decorator from @nestjs/graphql
GraphQL federation example:
/// @Directive({ arguments: ['@extends'] })
/// @Directive({ arguments: ['@key(fields: "id")'] })
model User {
/// @Directive({ arguments: ['@external'] })
id String @id
}
May generate:
1@ObjectType() 2@Directive('@extends') 3@Directive('@key(fields: "id")') 4export class User { 5 @Field(() => ID, { nullable: false }) 6 @Directive('@external') 7 id!: string; 8}
Allow rename type in schema and mark as abstract.
Example 1:
// schema.prisma
/// @ObjectType({ isAbstract: true })
model User {
id Int @id
}
1@ObjectType({ isAbstract: true }) 2export class User {}
Example 2:
// schema.prisma
/// @ObjectType('Human', { isAbstract: true })
model User {
id Int @id
}
1@ObjectType('Human', { isAbstract: true }) 2export class User {}
No vulnerabilities found.
No security vulnerabilities found.