Skip to content

adds requireSingleFieldsInWhereUniqueInput generator setting #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ Disable usage of graphql `ID` type and use `Int/Float` for fields marked as `@id
Type: `boolean`
Default: `false`

#### `requireSingleFieldsInWhereUniqueInput`

When a `Model`s `WhereUniqueInput` class has only a single field, mark that field as **required** (TypeScript) and **not nullable** (GraphQL). See [#58](https://github.com/unlight/prisma-nestjs-graphql/issues/58) for more details.
Type: `boolean`
Default: `false`

#### `useInputType`

Since GraphQL does not support input union type, this setting map
Expand Down
3 changes: 3 additions & 0 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { outputType } from './handlers/output-type';
import { purgeOutput } from './handlers/purge-output';
import { ReExport, reExport } from './handlers/re-export';
import { registerEnum } from './handlers/register-enum';
import { requireSingleFieldsInWhereUniqueInput } from './handlers/require-single-fields-in-whereunique-input';
import { warning } from './handlers/warning';
import { createConfig } from './helpers/create-config';
import { factoryGetSourceFile } from './helpers/factory-get-source-file';
Expand Down Expand Up @@ -82,6 +83,8 @@ export async function generate(
config.reExport !== ReExport.None && reExport(eventEmitter);
config.emitSingle && emitSingle(eventEmitter);
config.purgeOutput && purgeOutput(eventEmitter);
config.requireSingleFieldsInWhereUniqueInput &&
requireSingleFieldsInWhereUniqueInput(eventEmitter);

const models = new Map<string, Model>();
const modelNames: string[] = [];
Expand Down
24 changes: 24 additions & 0 deletions src/handlers/require-single-fields-in-whereunique-input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import AwaitEventEmitter from 'await-event-emitter';

import { EventArguments, InputType } from '../types';

export function requireSingleFieldsInWhereUniqueInput(eventEmitter: AwaitEventEmitter) {
eventEmitter.on('BeforeInputType', beforeInputType);
}

function beforeInputType(args: EventArguments & { inputType: InputType }) {
const { inputType } = args;

if (!isWhereUniqueInputType(inputType.name) || inputType.fields.length !== 1) {
return;
}

for (const field of inputType.fields) {
field.isRequired = true;
field.isNullable = false;
}
}

function isWhereUniqueInputType(name: string) {
return name.endsWith('WhereUniqueInput');
}
3 changes: 3 additions & 0 deletions src/helpers/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export function createConfig(data: Record<string, unknown>) {
purgeOutput: toBoolean(config.purgeOutput),
useInputType: createUseInputType(config.useInputType as any),
noTypeId: toBoolean(config.noTypeId),
requireSingleFieldsInWhereUniqueInput: toBoolean(
config.requireSingleFieldsInWhereUniqueInput,
),
decorate,
};
}
Expand Down