Skip to content

Commit

Permalink
fix: Get graphql type for scalar list
Browse files Browse the repository at this point in the history
close: unlight#30
  • Loading branch information
unlight committed May 13, 2021
1 parent d8c64f3 commit 97a1ae4
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions src/helpers/get-graphql-input-type.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 97a1ae4

Please sign in to comment.