Skip to content

CTE in WHERE clause is empty #212

@DoLevi

Description

@DoLevi

I'm running into a SQL syntax error using your (amazing) project:
Given a graphql schema like this:

type User @model(pk: "id", readOnly: true, cte: """SELECT * FROM users""") {
    username: String!
    hobbies: [Hobby!]! @relate(on: { from: "id", to: "user_id" })
}

type Hobby @model(pk: "id", readOnly: true, table: "hobbies") {
    title: String!
    user: User! @relate(on: { from: "user_id", to: "id" })
}

type Query @sqlmancer(dialect: POSTGRES) {
    hobbies: [Hobby!]!
}

and a resolver like this:

const resolvers = {
    Query: {
        hobbies: () => Hobby
            .findMany()
            .where({ user: { username: { equal: "levi" } } })
            .resolveInfo(resolveInfo)
            .execute();
    }
}

executing the hobbies query produces this SQL query:

select "h1"."title" as "title",
    (
        select json_agg("u1"."o")->0
        from (
                with "u2" as (
                    SELECT *
                    FROM users
                )
                select json_build_object('username', "u2"."username") as "o"
                from "u2"
                where "h1"."user_id" = "u2"."id"
                limit $1
            ) as "u1"
    ) as "user"
from "hobbies " as " h1 "
where (
        exists (
            with
            select null
            from "u3"
            where "h1"."user_id" = "u3 "."id"
                and "u3"."username" = $2
        )
    )

throwing a syntax error due to the empty with statement within the where clause. This happens whenever I use where from the QueryBuilders to filter on a type modelled by a common table expression.

EDIT: Here a repository reproducing the issue in express.
EDIT 2: Oh I forgot, this occurs using the pg client.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions