Skip to content

Commit

Permalink
fix: ensure subentities are defined in where type (#268)
Browse files Browse the repository at this point in the history
* fix: accept Int IDs when building subentity where object

* fix: use unique names for nested where type
  • Loading branch information
Sekhmet authored Oct 7, 2023
1 parent 5a3f39c commit ac79462
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/graphql/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ type WhereResult = {
orderByValues: Record<string, { value: string }>;
};

const cache = new Map<string, WhereResult>();

/**
* Controller for performing actions based on the graphql schema provided to its
* constructor. It exposes public functions to generate graphql or database
Expand Down Expand Up @@ -306,37 +304,37 @@ export class GqlEntityController {
type: GraphQLObjectType,
resolver: GraphQLFieldResolver<Parent, Context>
): GraphQLFieldConfig<Parent, Context> {
const getWhereType = (type: GraphQLObjectType<any, any>, level = 0): WhereResult => {
const name = `${type.name}_filter`;

const cachedValue = cache.get(name);
if (cachedValue) return cachedValue;
const getWhereType = (
nestedType: GraphQLObjectType<any, any>,
prefix?: string
): WhereResult => {
const name = prefix ? `${prefix}_${nestedType.name}_filter` : `${nestedType.name}_filter`;

const orderByValues = {};
const whereInputConfig: GraphQLInputObjectTypeConfig = {
name,
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;
}

Expand Down Expand Up @@ -395,8 +393,6 @@ export class GqlEntityController {
orderByValues
};

cache.set(name, result);

return result;
};

Expand Down

0 comments on commit ac79462

Please sign in to comment.