Skip to content

Commit

Permalink
fix: No atomic operations for scalar input list
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed May 22, 2022
1 parent 4be5d49 commit e55767b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
12 changes: 10 additions & 2 deletions src/handlers/no-atomic-operations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import AwaitEventEmitter from 'await-event-emitter';

import { isListInput } from '../helpers/is-list-input';
import { EventArguments, InputType } from '../types';

export function noAtomicOperations(eventEmitter: AwaitEventEmitter) {
Expand All @@ -12,12 +11,14 @@ function beforeInputType(args: EventArguments & { inputType: InputType }) {
const { inputType, getModelName } = args;

for (const field of inputType.fields) {
const fieldName = field.name;
field.inputTypes = field.inputTypes.filter(inputType => {
const inputTypeName = String(inputType.type);
const modelName = getModelName(inputTypeName);

if (
isAtomicOperation(inputTypeName) ||
(modelName && isListInput(inputTypeName, modelName))
(modelName && isListInput(inputTypeName, modelName, fieldName))
) {
return false;
}
Expand All @@ -44,3 +45,10 @@ function isAtomicOperation(typeName: string) {
}
return false;
}

function isListInput(typeName: string, model: string, field: string) {
return (
typeName === `${model}Create${field}Input` ||
typeName === `${model}Update${field}Input`
);
}
12 changes: 2 additions & 10 deletions src/helpers/get-model-name.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { first, memoize } from 'lodash';

import { isListInput } from './is-list-input';

export function createGetModelName(modelNames: string[]) {
return memoize(tryGetName);

Expand Down Expand Up @@ -55,14 +53,6 @@ function getModelName(args: {
}
}

if (name.slice(-5) === 'Input') {
for (const model of modelNames) {
if (isListInput(name, model)) {
return model;
}
}
}

// eslint-disable-next-line consistent-return, unicorn/no-useless-undefined
return undefined;
}
Expand Down Expand Up @@ -107,6 +97,8 @@ const splitKeywords = [
'MinOrderBy',
'MaxOrderBy',
'AvgOrderBy',
'Create',
'Update',
].sort((a, b) => b.length - a.length);

const endsWithKeywords = [
Expand Down
3 changes: 0 additions & 3 deletions src/helpers/is-list-input.ts

This file was deleted.

20 changes: 18 additions & 2 deletions src/test/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1284,9 +1284,15 @@ describe('scalar arrays with noAtomicOperations', () => {
before(async () => {
({ project } = await testGenerate({
schema: `
model Dummy {
model User {
id String @id
ints Int[]
articles Article[] @relation("ArticleAuthor")
}
model Article {
id String @id
author User @relation(name: "ArticleAuthor", fields: [authorId], references: [id])
authorId String
}
`,
options: [
Expand All @@ -1298,7 +1304,7 @@ describe('scalar arrays with noAtomicOperations', () => {
});

describe('ints should be array', () => {
for (const className of ['DummyCreateInput']) {
for (const className of ['UserCreateInput']) {
it(className, () => {
const s = testSourceFile({
project,
Expand All @@ -1310,6 +1316,16 @@ describe('scalar arrays with noAtomicOperations', () => {
});
}
});

it('create many inputs should not be deleted', () => {
const s = testSourceFile({
project,
class: 'UserCreateInput',
property: 'articles',
});

expect(s.property?.type).toBe('ArticleCreateNestedManyWithoutAuthorInput');
});
});

describe('combine scalar filters', () => {
Expand Down

0 comments on commit e55767b

Please sign in to comment.