TriPSs / nestjs-query

Easy CRUD for GraphQL.
https://tripss.github.io/nestjs-query/
MIT License
157 stars 43 forks source link

Alias And Field Names In Generated SQL For Aggregate GroupBy Date Are Not Properly Escaped With Quotes #238

Open dvins opened 7 months ago

dvins commented 7 months ago

Describe the bug When using groupBy date the alias and field names in the generated SQL are not being properly escaped, resulting in an invalid query and failure.

Have you read the Contributing Guidelines?

Yes.

To Reproduce Steps to reproduce the behavior:

  1. With a backing Postgres data structure wherein the schema concerns a TypeORM ViewEntity class named UserRecord targeting the database schema tenant and view user.
  2. Issue an aggregate query, such as:
{
  userAggregate {
    groupBy {
      createdAt(by: MONTH )
    }
    count {
      userId
    }
  }
}

After which an error is thrown by the database and introspection of the generated SQL for the query shows that the alias and field name in the TO_CHAR() function for the group by aggregate is not properly escaped in quotes, whereas the count aggregate is.

SELECT
  DATE(TO_CHAR(UserRecord.createdAt, 'YYYY-mm-01')) AS "GROUP_BY_createdAt",
  COUNT("UserRecord"."userId") AS "COUNT_userId"
FROM
  "tenant"."user" "UserRecord"
GROUP BY
  "GROUP_BY_createdAt"
ORDER BY
  "GROUP_BY_createdAt" ASC

Expected behavior The generated SQL does not error and results are subsequently returned.

Desktop (please complete the following information):

TriPSs commented 7 months ago

Interesting, we do not do anything special or more for the COUNT. What driver are you using as the tests for MySQL and Postgres both pass.