Skip to content

Commit

Permalink
fix: Combine scalar filters with fieldReference
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Dec 14, 2022
1 parent 89a59cc commit 1f1ef9c
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 254 deletions.
8 changes: 4 additions & 4 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,10 @@ export async function generate(
outputFilePattern: config.outputFilePattern,
eventEmitter,
});
const {
datamodel,
schema: { inputObjectTypes, outputObjectTypes, enumTypes },
} = JSON.parse(JSON.stringify(dmmf)) as DMMF.Document;
const { datamodel, schema } = JSON.parse(JSON.stringify(dmmf)) as DMMF.Document;
const removeTypes = new Set<string>();
const eventArguments: EventArguments = {
schema,
models,
config,
modelNames,
Expand Down Expand Up @@ -135,6 +133,8 @@ export async function generate(
await eventEmitter.emit('Model', model, eventArguments);
}

const { inputObjectTypes, outputObjectTypes, enumTypes } = schema;

await eventEmitter.emit('PostBegin', eventArguments);

for (const enumType of enumTypes.prisma.concat(enumTypes.model || [])) {
Expand Down
6 changes: 4 additions & 2 deletions src/handlers/args-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export function argsType(field: SchemaField, args: EventArguments) {
const modelName = getModelName(className) || '';

switch (className) {
case `Aggregate${modelName}Args`:
case `Aggregate${modelName}Args`: {
className = `${modelName}AggregateArgs`;
break;
case `GroupBy${modelName}Args`:
}
case `GroupBy${modelName}Args`: {
className = `${modelName}GroupByArgs`;
break;
}
}

const inputType: InputType = {
Expand Down
52 changes: 50 additions & 2 deletions src/handlers/combine-scalar-filters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AwaitEventEmitter from 'await-event-emitter';
import { cloneDeep, keyBy, remove } from 'lodash';

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

Expand All @@ -8,6 +9,7 @@ import { DMMF, EventArguments, InputType } from '../types';
export function combineScalarFilters(eventEmitter: AwaitEventEmitter) {
eventEmitter.on('BeforeInputType', beforeInputType);
eventEmitter.on('BeforeGenerateField', beforeGenerateField);
eventEmitter.on('PostBegin', postBegin);
}

function beforeInputType(
Expand All @@ -17,10 +19,11 @@ function beforeInputType(
classDecoratorName: string;
},
) {
const { inputType } = args;
const { inputType, removeTypes } = args;

if (isContainBogus(inputType.name) && isScalarFilter(inputType)) {
inputType.name = replaceBogus(String(inputType.name));
removeTypes.add(inputType.name);
inputType.name = replaceBogus(inputType.name);
}
}

Expand Down Expand Up @@ -61,3 +64,48 @@ function isScalarFilter(inputType: InputType) {
}
return result;
}

function postBegin(args: EventArguments) {
const { schema } = args;
const inputTypes = schema.inputObjectTypes.prisma;
const enumTypes = schema.enumTypes.model || [];
const types = ['Bool', 'Int', 'String', 'DateTime', 'Decimal', 'Float', 'Json'];

for (const enumType of enumTypes) {
const { name } = enumType;
types.push(`Enum${name}`);
}

const inputTypeByName = keyBy(inputTypes, inputType => inputType.name);
const replaceBogusFilters = (filterName: string, filterNameCandidates: string[]) => {
for (const filterNameCandidate of filterNameCandidates) {
const candidate = inputTypeByName[filterNameCandidate];
if (candidate as InputType | undefined) {
const inputType = cloneDeep({ ...candidate, name: filterName });
inputTypes.push(inputType);
inputTypeByName[filterName] = inputType;
break;
}
}
};

for (const type of types) {
// Scalar filters
replaceBogusFilters(`${type}Filter`, [
`${type}NullableFilter`,
`Nested${type}NullableFilter`,
]);

replaceBogusFilters(`${type}WithAggregatesFilter`, [
`${type}NullableWithAggregatesFilter`,
`Nested${type}NullableWithAggregatesFilter`,
]);

replaceBogusFilters(`${type}ListFilter`, [
`${type}NullableListFilter`,
`Nested${type}NullableListFilter`,
]);
}

remove(inputTypes, inputType => isContainBogus(inputType.name));
}
9 changes: 6 additions & 3 deletions src/handlers/generate-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function generateFiles(args: EventArguments) {
continue;
}
switch (statement.kind) {
case StructureKind.ImportDeclaration:
case StructureKind.ImportDeclaration: {
if (
statement.moduleSpecifier.startsWith('./') ||
statement.moduleSpecifier.startsWith('..')
Expand All @@ -89,12 +89,15 @@ export async function generateFiles(args: EventArguments) {
});
}
break;
case StructureKind.Enum:
}
case StructureKind.Enum: {
enums.unshift(statement);
break;
case StructureKind.Class:
}
case StructureKind.Class: {
classes.push(statement);
break;
}
}
}
sourceFile.set({
Expand Down
1 change: 1 addition & 0 deletions src/handlers/input-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export function inputType(

for (const field of inputType.fields) {
field.inputTypes = field.inputTypes.filter(t => !removeTypes.has(String(t.type)));

eventEmitter.emitSync('BeforeGenerateField', field, args);

const { inputTypes, isRequired, name } = field;
Expand Down
9 changes: 6 additions & 3 deletions src/helpers/file-type-by-location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { FieldLocation } from '../types';

export function fileTypeByLocation(fieldLocation: FieldLocation) {
switch (fieldLocation) {
case 'inputObjectTypes':
case 'inputObjectTypes': {
return 'input';
case 'outputObjectTypes':
}
case 'outputObjectTypes': {
return 'output';
case 'enumTypes':
}
case 'enumTypes': {
return 'enum';
}
}
return 'object';
}
15 changes: 10 additions & 5 deletions src/helpers/get-graphql-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,25 @@ export function getGraphqlImport(args: {

switch (typeName) {
case 'Float':
case 'Int':
case 'Int': {
return { name: typeName, specifier: '@nestjs/graphql' };
case 'DateTime':
}
case 'DateTime': {
return { name: 'Date', specifier: undefined };
}
case 'true':
case 'Boolean':
case 'Boolean': {
return { name: 'Boolean', specifier: undefined };
case 'Decimal':
}
case 'Decimal': {
return {
name: 'GraphQLDecimal',
specifier: 'prisma-graphql-type-decimal',
};
case 'Json':
}
case 'Json': {
return { name: 'GraphQLJSON', specifier: 'graphql-type-json' };
}
}

return { name: 'String', specifier: undefined };
Expand Down
27 changes: 18 additions & 9 deletions src/helpers/get-property-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,33 @@ export function getPropertyType(args: {
const { type, location } = args;
switch (type) {
case 'Float':
case 'Int':
case 'Int': {
return ['number'];
case 'String':
}
case 'String': {
return ['string'];
case 'Boolean':
}
case 'Boolean': {
return ['boolean'];
case 'DateTime':
}
case 'DateTime': {
return ['Date', 'string'];
case 'Decimal':
}
case 'Decimal': {
return ['Decimal'];
case 'Json':
}
case 'Json': {
return ['any'];
case 'Null':
}
case 'Null': {
return ['null'];
case 'Bytes':
}
case 'Bytes': {
return ['Buffer'];
case 'BigInt':
}
case 'BigInt': {
return ['bigint', 'number'];
}
}
if (['inputObjectTypes', 'outputObjectTypes'].includes(location)) {
return [type];
Expand Down
Loading

0 comments on commit 1f1ef9c

Please sign in to comment.