From 97a1ae482edaff8ed873c65ab4f0ccb2ce1c51c5 Mon Sep 17 00:00:00 2001 From: Roman_Vasilev Date: Thu, 13 May 2021 22:16:24 +0400 Subject: [PATCH] fix: Get graphql type for scalar list close: #30 --- src/helpers/get-graphql-input-type.ts | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/helpers/get-graphql-input-type.ts b/src/helpers/get-graphql-input-type.ts index 268c4508..0b936f85 100644 --- a/src/helpers/get-graphql-input-type.ts +++ b/src/helpers/get-graphql-input-type.ts @@ -1,34 +1,23 @@ +import { countBy } from 'lodash'; + import { DMMF } from '../types'; /** * Find input type for graphql field decorator. */ export function getGraphqlInputType(inputTypes: DMMF.SchemaArgInputType[]) { - if (inputTypes.length === 0) { - throw new TypeError('Cannot get matching input type from empty array'); - } - if (inputTypes.length === 1) { - return inputTypes[0]; - } - inputTypes = inputTypes.filter(t => !['null', 'Null'].includes(String(t.type))); - let result: DMMF.SchemaArgInputType | undefined; - const isAllObjects = inputTypes.every(x => x.location === 'inputObjectTypes'); - if (isAllObjects) { - result = inputTypes.find(x => x.isList); - if (result) { - return result; - } - } + inputTypes = inputTypes.filter(t => !['null', 'Null'].includes(String(t.type))); if (inputTypes.length === 1) { return inputTypes[0]; } - const isAllEnums = inputTypes.every(x => x.location === 'enumTypes'); + const countTypes = countBy(inputTypes, x => x.location); + const isOneType = Object.keys(countTypes).length === 1; - if (isAllEnums) { + if (isOneType) { result = inputTypes.find(x => x.isList); if (result) { return result;