Skip to content

Commit

Permalink
fix: Enum atomic operation are not processed
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Nov 14, 2020
1 parent 5ad04b2 commit 43a2506
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 84 deletions.
12 changes: 0 additions & 12 deletions src/@generated/prisma/enum-role-field-update-operations.input.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/@generated/user/user-update-many-mutation.input.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Field, Float, InputType, Int } from '@nestjs/graphql';

import { EnumRoleFieldUpdateOperationsInput } from '../prisma/enum-role-field-update-operations.input';
import { Role } from '../prisma/role.enum';

@InputType()
Expand Down Expand Up @@ -53,9 +52,9 @@ export class UserUpdateManyMutationInput {
})
rating?: number | null;

@Field(() => EnumRoleFieldUpdateOperationsInput, {
@Field(() => Role, {
nullable: true,
description: undefined,
})
role?: Role | EnumRoleFieldUpdateOperationsInput | null;
role?: Role | null;
}
5 changes: 2 additions & 3 deletions src/@generated/user/user-update-without-articles.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Field, Float, InputType, Int } from '@nestjs/graphql';

import { ArticleUpdateManyWithoutFavoritedByInput } from '../article/article-update-many-without-favorited-by.input';
import { CommentUpdateManyWithoutAuthorInput } from '../comment/comment-update-many-without-author.input';
import { EnumRoleFieldUpdateOperationsInput } from '../prisma/enum-role-field-update-operations.input';
import { Role } from '../prisma/role.enum';
import { UserUpdateManyWithoutFollowersInput } from './user-update-many-without-followers.input';
import { UserUpdateManyWithoutFollowingInput } from './user-update-many-without-following.input';
Expand Down Expand Up @@ -57,11 +56,11 @@ export class UserUpdateWithoutArticlesInput {
})
rating?: number | null;

@Field(() => EnumRoleFieldUpdateOperationsInput, {
@Field(() => Role, {
nullable: true,
description: undefined,
})
role?: Role | EnumRoleFieldUpdateOperationsInput | null;
role?: Role | null;

@Field(() => UserUpdateManyWithoutFollowersInput, {
nullable: true,
Expand Down
5 changes: 2 additions & 3 deletions src/@generated/user/user-update-without-comments.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Field, Float, InputType, Int } from '@nestjs/graphql';

import { ArticleUpdateManyWithoutAuthorInput } from '../article/article-update-many-without-author.input';
import { ArticleUpdateManyWithoutFavoritedByInput } from '../article/article-update-many-without-favorited-by.input';
import { EnumRoleFieldUpdateOperationsInput } from '../prisma/enum-role-field-update-operations.input';
import { Role } from '../prisma/role.enum';
import { UserUpdateManyWithoutFollowersInput } from './user-update-many-without-followers.input';
import { UserUpdateManyWithoutFollowingInput } from './user-update-many-without-following.input';
Expand Down Expand Up @@ -57,11 +56,11 @@ export class UserUpdateWithoutCommentsInput {
})
rating?: number | null;

@Field(() => EnumRoleFieldUpdateOperationsInput, {
@Field(() => Role, {
nullable: true,
description: undefined,
})
role?: Role | EnumRoleFieldUpdateOperationsInput | null;
role?: Role | null;

@Field(() => UserUpdateManyWithoutFollowersInput, {
nullable: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Field, Float, InputType, Int } from '@nestjs/graphql';

import { ArticleUpdateManyWithoutAuthorInput } from '../article/article-update-many-without-author.input';
import { CommentUpdateManyWithoutAuthorInput } from '../comment/comment-update-many-without-author.input';
import { EnumRoleFieldUpdateOperationsInput } from '../prisma/enum-role-field-update-operations.input';
import { Role } from '../prisma/role.enum';
import { UserUpdateManyWithoutFollowersInput } from './user-update-many-without-followers.input';
import { UserUpdateManyWithoutFollowingInput } from './user-update-many-without-following.input';
Expand Down Expand Up @@ -57,11 +56,11 @@ export class UserUpdateWithoutFavoriteArticlesInput {
})
rating?: number | null;

@Field(() => EnumRoleFieldUpdateOperationsInput, {
@Field(() => Role, {
nullable: true,
description: undefined,
})
role?: Role | EnumRoleFieldUpdateOperationsInput | null;
role?: Role | null;

@Field(() => UserUpdateManyWithoutFollowersInput, {
nullable: true,
Expand Down
5 changes: 2 additions & 3 deletions src/@generated/user/user-update-without-followers.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Field, Float, InputType, Int } from '@nestjs/graphql';
import { ArticleUpdateManyWithoutAuthorInput } from '../article/article-update-many-without-author.input';
import { ArticleUpdateManyWithoutFavoritedByInput } from '../article/article-update-many-without-favorited-by.input';
import { CommentUpdateManyWithoutAuthorInput } from '../comment/comment-update-many-without-author.input';
import { EnumRoleFieldUpdateOperationsInput } from '../prisma/enum-role-field-update-operations.input';
import { Role } from '../prisma/role.enum';
import { UserUpdateManyWithoutFollowersInput } from './user-update-many-without-followers.input';

Expand Down Expand Up @@ -57,11 +56,11 @@ export class UserUpdateWithoutFollowersInput {
})
rating?: number | null;

@Field(() => EnumRoleFieldUpdateOperationsInput, {
@Field(() => Role, {
nullable: true,
description: undefined,
})
role?: Role | EnumRoleFieldUpdateOperationsInput | null;
role?: Role | null;

@Field(() => UserUpdateManyWithoutFollowersInput, {
nullable: true,
Expand Down
5 changes: 2 additions & 3 deletions src/@generated/user/user-update-without-following.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Field, Float, InputType, Int } from '@nestjs/graphql';
import { ArticleUpdateManyWithoutAuthorInput } from '../article/article-update-many-without-author.input';
import { ArticleUpdateManyWithoutFavoritedByInput } from '../article/article-update-many-without-favorited-by.input';
import { CommentUpdateManyWithoutAuthorInput } from '../comment/comment-update-many-without-author.input';
import { EnumRoleFieldUpdateOperationsInput } from '../prisma/enum-role-field-update-operations.input';
import { Role } from '../prisma/role.enum';
import { UserUpdateManyWithoutFollowingInput } from './user-update-many-without-following.input';

Expand Down Expand Up @@ -57,11 +56,11 @@ export class UserUpdateWithoutFollowingInput {
})
rating?: number | null;

@Field(() => EnumRoleFieldUpdateOperationsInput, {
@Field(() => Role, {
nullable: true,
description: undefined,
})
role?: Role | EnumRoleFieldUpdateOperationsInput | null;
role?: Role | null;

@Field(() => UserUpdateManyWithoutFollowingInput, {
nullable: true,
Expand Down
5 changes: 2 additions & 3 deletions src/@generated/user/user-update.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Field, Float, InputType, Int } from '@nestjs/graphql';
import { ArticleUpdateManyWithoutAuthorInput } from '../article/article-update-many-without-author.input';
import { ArticleUpdateManyWithoutFavoritedByInput } from '../article/article-update-many-without-favorited-by.input';
import { CommentUpdateManyWithoutAuthorInput } from '../comment/comment-update-many-without-author.input';
import { EnumRoleFieldUpdateOperationsInput } from '../prisma/enum-role-field-update-operations.input';
import { Role } from '../prisma/role.enum';
import { UserUpdateManyWithoutFollowersInput } from './user-update-many-without-followers.input';
import { UserUpdateManyWithoutFollowingInput } from './user-update-many-without-following.input';
Expand Down Expand Up @@ -58,11 +57,11 @@ export class UserUpdateInput {
})
rating?: number | null;

@Field(() => EnumRoleFieldUpdateOperationsInput, {
@Field(() => Role, {
nullable: true,
description: undefined,
})
role?: Role | EnumRoleFieldUpdateOperationsInput | null;
role?: Role | null;

@Field(() => UserUpdateManyWithoutFollowersInput, {
nullable: true,
Expand Down
2 changes: 1 addition & 1 deletion src/generate-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'assert';
import { ClassDeclaration, Node, ObjectLiteralExpression, SourceFile } from 'ts-morph';

import { generateGraphqlImport } from './generate-graphql-import';
import { updateObjectProperty } from './update-object-property';
import { updateObjectProperty } from './utils';

export type DecoratorPropertyType = {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion src/generate-decorator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert';
import { Node, ObjectLiteralExpression, PropertyDeclaration } from 'ts-morph';

import { updateObjectProperty } from './update-object-property';
import { updateObjectProperty } from './utils';

type GenerateDecoratorArgs = {
propertyDeclaration: PropertyDeclaration;
Expand Down
2 changes: 1 addition & 1 deletion src/generate-enum/generate-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {

import { generateGraphqlImport } from '../generate-graphql-import';
import { PrismaDMMF } from '../types';
import { updateObjectProperty } from '../update-object-property';
import { updateObjectProperty } from '../utils';

type GenerateEnumArgs = {
enumerable: PrismaDMMF.SchemaEnum & { documentation?: string };
Expand Down
45 changes: 45 additions & 0 deletions src/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,49 @@ describe('main generate', () => {
});
}
});

it('option atomicNumberOperations false', async () => {
await getResult({
schema: `
model User {
id String @id
int1 Int
int2 Int?
f1 Float?
f2 Float
role1 Role?
role2 Role
}
enum Role {
USER
}
`,
atomicNumberOperations: false,
});
expect(sourceFiles.length).to.be.greaterThan(0);
for (const sourceFile of sourceFiles) {
sourceFile.getClasses().forEach((classDeclaration) => {
if (classDeclaration.getName()?.endsWith('FieldUpdateOperationsInput')) {
assert.fail(`Class should not exists ${classDeclaration.getName()!}`);
}
});
}
sourceFiles
.flatMap((s) => s.getClasses())
.filter((c) =>
['UserUpdateInput', 'UserUpdateManyMutationInput'].includes(c.getName()!),
)
.flatMap((c) => c.getProperties())
.map((p) => p.getStructure())
.map(({ name, type }) => ({
name,
type,
types: (type as string).split('|').map((s) => s.trim()),
}))
.forEach((struct) => {
if (struct.types.find((s) => s.endsWith('FieldUpdateOperationsInput'))) {
expect.fail(`Property ${struct.name} typed ${struct.type}`);
}
});
});
});
9 changes: 5 additions & 4 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ export async function generate(args: GenerateArgs) {
generateModel({ model, sourceFile, projectFilePath });
}
// Generate inputs
const inputTypes = prismaClientDmmf.schema.inputTypes.filter(
mutateFilters(prismaClientDmmf.schema.inputTypes, {
let inputTypes = prismaClientDmmf.schema.inputTypes;
inputTypes = inputTypes.filter(
mutateFilters(inputTypes, {
combineScalarFilters: JSON.parse(
(generator.config.combineScalarFilters as string | undefined) ?? 'true',
),
) as boolean,
atomicNumberOperations: JSON.parse(
(generator.config.atomicNumberOperations as string | undefined) ?? 'false',
),
) as boolean,
}),
);
// Create aggregate inputs
Expand Down
16 changes: 2 additions & 14 deletions src/mutate-filters/mutate-filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,8 @@ type MutateFiltersOptions = {

export function mutateFilters(inputTypes: PrismaDMMF.InputType[], options: MutateFiltersOptions) {
const mutations = [
options.combineScalarFilters && [combineScalarFilters(inputTypes)],
!options.atomicNumberOperations &&
noAtomicNumberOperations([
'StringFieldUpdateOperationsInput',
'NullableStringFieldUpdateOperationsInput',
'IntFieldUpdateOperationsInput',
'NullableIntFieldUpdateOperationsInput',
'FloatFieldUpdateOperationsInput',
'NullableFloatFieldUpdateOperationsInput',
'BoolFieldUpdateOperationsInput',
'NullableBoolFieldUpdateOperationsInput',
'DateTimeFieldUpdateOperationsInput',
'NullableDateTimeFieldUpdateOperationsInput',
]),
options.combineScalarFilters && combineScalarFilters(inputTypes),
!options.atomicNumberOperations && noAtomicNumberOperations(),
];

return function (inputType: PrismaDMMF.InputType) {
Expand Down
10 changes: 7 additions & 3 deletions src/mutate-filters/no-atomic-number-operations.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { PrismaDMMF } from '../types';

export function noAtomicNumberOperations(names: string[]) {
export function noAtomicNumberOperations() {
return (inputType: PrismaDMMF.InputType) => {
if (names.includes(inputType.name)) {
if (isAtomicOperation(inputType.name)) {
return false;
}
inputType.fields = inputType.fields.map((field) => {
field.inputTypes = field.inputTypes.filter((inputType) => {
return !names.includes(String(inputType.type));
return !isAtomicOperation(String(inputType.type));
});
return field;
});
return inputType;
};
}

function isAtomicOperation(name: string) {
return name.endsWith('FieldUpdateOperationsInput');
}
6 changes: 5 additions & 1 deletion src/testing/generator-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import fs from 'fs';

import { PrismaDMMF } from '../types';

const {
dependencies: { '@prisma/generator-helper': generatorVersion },
} = require('../../package.json');

const cachePath: string = findCacheDir({ name: 'createGeneratorOptions', create: true });

/**
Expand All @@ -16,7 +20,7 @@ export async function generatorOptions(
options?: Record<string, any>,
): Promise<GeneratorOptions & { prismaClientDmmf: PrismaDMMF.Document }> {
// eslint-disable-next-line prefer-rest-params
const data = JSON.stringify(arguments);
const data = JSON.stringify([generatorVersion, arguments]);
const hash = crypto.createHash('md5').update(data).digest('hex');
const optionsCacheFile = `${cachePath}/options-${hash}.js`;
if (!fs.existsSync(optionsCacheFile)) {
Expand Down
26 changes: 0 additions & 26 deletions src/update-object-property.ts

This file was deleted.

27 changes: 27 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ObjectLiteralExpression, PropertyAssignment, StructureKind } from 'ts-morph';

import { PrismaDMMF } from './types';

type ToGraphqlImportTypeArgs = {
Expand Down Expand Up @@ -111,3 +113,28 @@ export function schemaFieldToArgument(field: PrismaDMMF.SchemaField): PrismaDMMF
export function getOutputTypeName(name: string) {
return name.replace(/OutputType$/, '');
}

type UpdateObjectPropertyArgs = {
expression: ObjectLiteralExpression;
name: string;
value: string | number | boolean | undefined;
defaultValue?: string | number | boolean;
};

export function updateObjectProperty(args: UpdateObjectPropertyArgs) {
const { expression, name, value, defaultValue } = args;
let descriptionProperty = expression.getProperty(name) as PropertyAssignment | undefined;

if (!descriptionProperty) {
descriptionProperty = expression.addProperty({
name,
kind: StructureKind.PropertyAssignment,
initializer: defaultValue !== undefined ? String(defaultValue) : 'undefined',
}) as PropertyAssignment;
}
descriptionProperty.set({
initializer:
JSON.stringify(value) ||
(defaultValue !== undefined ? String(defaultValue) : 'undefined'),
});
}

0 comments on commit 43a2506

Please sign in to comment.