nuwave / nuwave/lighthouse

Define input enums in WHERE HAS conditions

Open
#2,723 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
PHP
Stars
3.5k
Forks
468
Avg merge
3h 9m
Merged PRs (30d)
2

Description

When defining a WHERE HAS query with a condition in complex queries, Lighthouse will fetch the column enums from the parent type, not the relation, if the where definition uses any sort of column enumeration.

{
  drivers(
    where: {
      OR: [
        {HAS: {relation: "employee", condition: {column: NAME, value: "Peter"}}}, // `column: DriverColumnEnum` instead of `column: EmployeeColumnEnum`
        {column: ID, operator: EQ, value: "6217"} // `column: DriverColumnEnum`
      ]
    }
  ) {
    data {
      id
      name
      employee {
        name
      }
    }
  }
}

I think it would make sense if you could define the relations in a where, the same way we do in orderBy:

extend type Query {
    "List multiple drivers."
    drivers(
        orderBy: _ @orderBy(
            columnsEnum: "DriverColumn",
            relations: [{relation: "employee", columns: ["name"]}]
        )
        where: _ @whereConditions(
            columnsEnum: "DriverColumn",
            relations: [{relation: "employee", columns: ["name"]}] // or columnsEnum.
        )
    ): [Driver!]! @paginate(defaultCount: 10)
}

This would both enable stronger schema generation by having enums for the relation name (right now it's currently just a string), make orderBy and where more similar in how they are written in the schema definitions, and of course fix the issue described in the discussion link below.
One of the huge benefits of using enum types in the schema, is that the schema documentation can explain exactly what a API consumer can use, so I think this would be the absolute best solution for usability. Otherwise a API consumer will need intricate knowledge of the database columns available.
I think it would also make sense that the relations.column type will default to String if column enumerations are missing, instead of trying to resovle the column enum type of the parent.

Thanks for taking your time to consider this. :)

Original Q&A discussion this is based on: https://github.com/nuwave/lighthouse/discussions/2720#discussioncomment-14779266

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the whereConditions and orderBy directive definitions, then read the linked discussion for the expected behavior. The issue proposes relation-aware column enums and a String fallback when relation column enumerations are absent; done should include schema generation and query handling that use the relation's columns rather than the parent type's enum.

Written by the indexing model from the issue text.

Assessment

Tech stack
laravel, php
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.