Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complex filters handling using generated code #404

Open
DanielAndrysek opened this issue Nov 29, 2022 · 0 comments
Open

Complex filters handling using generated code #404

DanielAndrysek opened this issue Nov 29, 2022 · 0 comments
Assignees
Labels
Discussion question Further information is requested

Comments

@DanielAndrysek
Copy link

DanielAndrysek commented Nov 29, 2022

Hi

I've found the issue with query generating by generated code. We are using Symfony 6 + Api Platform 2.6 for backend and angular with mst-gql for frontend. We're using soft delete functionality so in order to filter out results from query we have implemented "Exists" filter.

Generated selector:

export class ProductSupplierModelSelector extends QueryBuilder {
  get id() { return this.__attr(`id`) }
  get name() { return this.__attr(`name`) }
  get deletedAt() { return this.__attr(`deletedAt`) }
  get created() { return this.__attr(`created`) }
  get modified() { return this.__attr(`modified`) }
  get deleted() { return this.__attr(`deleted`) }
  products(builder: string | ProductModelSelector | ((selector: ProductModelSelector) => ProductModelSelector) | undefined, args?: { name?: (string | null), supplier?: (string | null), supplierList?: (string | null)[], family?: (string | null), familyList?: (string | null)[], exists?: (ProductFilterExists | null)[] }) { return this.__child(`products`+ (args ? '('+['name', 'supplier', 'supplierList', 'family', 'familyList', 'exists'].map((argName) => ((args as any)[argName] ? `${argName}: ${JSON.stringify((args as any)[argName])}` : null) ).filter((v) => v!=null).join(', ') + ')': ''), ProductModelSelector, builder) }
}

The generated code produces query like:

products(exists: [{"deletedAt":false}])

Instead of:

products(exists: [{deletedAt:false}])

The reason of this is the part with JSON.stringify:

${JSON.stringify((args as any)[argName])}`

My solution is to substitute it with a method wich converts complex objects into queryable string without escaping object keys:

const filterToString = (filter: any): string => {
    if (Array.isArray(filter)){
        return `[${filter.map((tmp) => filterToString(tmp)).join(", ")}]`;
    }else if (typeof filter === "object"){
        const objectKeys = Object.keys(filter);
        return `{${objectKeys.map((key) => `${key}:${filterToString(filter[key])}`).join(", ")}}`;
    }
    return JSON.stringify(filter);
};

Is there any other way to avoid this issue?

@Benz19 Benz19 added question Further information is requested Discussion labels Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Discussion question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants