From ac79462552fab97cdc9b937e137e5b0bf3580995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Tkaczy=C5=84ski?= Date: Sat, 7 Oct 2023 08:39:29 +0200 Subject: [PATCH] fix: ensure subentities are defined in where type (#268) * fix: accept Int IDs when building subentity where object * fix: use unique names for nested where type --- src/graphql/controller.ts | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/graphql/controller.ts b/src/graphql/controller.ts index 9db2e8e..450c8cd 100644 --- a/src/graphql/controller.ts +++ b/src/graphql/controller.ts @@ -56,8 +56,6 @@ type WhereResult = { orderByValues: Record; }; -const cache = new Map(); - /** * Controller for performing actions based on the graphql schema provided to its * constructor. It exposes public functions to generate graphql or database @@ -306,11 +304,11 @@ export class GqlEntityController { type: GraphQLObjectType, resolver: GraphQLFieldResolver ): GraphQLFieldConfig { - const getWhereType = (type: GraphQLObjectType, level = 0): WhereResult => { - const name = `${type.name}_filter`; - - const cachedValue = cache.get(name); - if (cachedValue) return cachedValue; + const getWhereType = ( + nestedType: GraphQLObjectType, + prefix?: string + ): WhereResult => { + const name = prefix ? `${prefix}_${nestedType.name}_filter` : `${nestedType.name}_filter`; const orderByValues = {}; const whereInputConfig: GraphQLInputObjectTypeConfig = { @@ -318,25 +316,25 @@ export class GqlEntityController { fields: {} }; - this.getTypeFields(type).forEach(field => { + this.getTypeFields(nestedType).forEach(field => { // all field types in a where input variable must be optional // so we try to extract the non null type here. let nonNullFieldType = getNonNullType(field.type); if (nonNullFieldType instanceof GraphQLObjectType) { - const fields = type.getFields(); + const fields = nestedType.getFields(); const idField = fields['id']; if ( idField && idField.type instanceof GraphQLNonNull && idField.type.ofType instanceof GraphQLScalarType && - ['String', 'ID'].includes(idField.type.ofType.name) + ['String', 'Int', 'ID'].includes(idField.type.ofType.name) ) { - if (level === 0) { + if (!prefix) { whereInputConfig.fields[`${field.name}_`] = getWhereType( nonNullFieldType, - level + 1 + nestedType.name ).where; } @@ -395,8 +393,6 @@ export class GqlEntityController { orderByValues }; - cache.set(name, result); - return result; };