nuwave / nuwave/lighthouse

Respect query constraints within relations when batchloading relationships

Open
#815 12 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

Describe the bug

I have a @hasOne relationship on one of my models, and I want to return this relationship in GraphQL, but the result is always null.

Expected behavior
I expect the selected_guide_booking field to return a model according to the @hasOne relationship.

In my other Laravel code (the blade files for example) this works perfectly. $booking->selected_guide_booking->price for example will return the right price. However when I do the same in a GraphQL query, it always returns null.

Schema

This is the relationship in my model:

public function selected_guide_booking()
    {
        return $this->hasOne('App\GuideBooking','booking_id')->where('guide_id', $this->guide_id);
    }

This is the field on my Booking Type:

selected_guide_booking: GuideBooking @hasOne

Each Booking can have multiple GuideBookings, but only has one GuideBooking where the guide_id is the same as the guide_id in the Booking.

Output/Logs

Example GraphQL response, click to expand
{
  "data": {
    "me": {
      "email": "test@test.com",
      "price": "35",
      "completed_incomingbookings": [
        {
          "selected_guide_booking": null,
          "review": {
            "id": "1",
            "text": "Bla bla bla bla",
            "ranking": "5",
            "tourist": {
              "realname": "Madeline Hayes MD",
              "photo": null,
              "about_tourist": "Adipisci magnam corrupti officia nulla. Sunt cupiditate nam consequatur sed omnis. Sit non voluptatem voluptatem autem. Repudiandae eum vero quia reiciendis qui nam. Quia ut ut quam. Alias reiciendis est vitae sint sint et."
            }
          }
        }
      ]
    }
  }
}

The review field on this query also uses a @hasOne relationship, but does work correctly. As far as I can tell the only difference is that the selected_guide_booking in theory could return multiple models, if it wouldn't have the 'where' clause on the relationship.


Environment

Lighthouse Version: 2.6.4
Laravel Version: 5.7.26
PHP Version: 7.2.11

Additional context

I've tried changing the @hasOne relationship on the selected_guide_booking field to a @hasMany relationship. This correctly returns the model, but it is inside an array. This breaks the other Laravel code, because it's expecting a hasOne relationship. A temporary fix could be to use a hasMany relationship and then putting [0] behind every returned model to retrieve the first model in the array, but this would make for some ugly code.

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 with the selected_guide_booking relationship on the Booking model and the selected_guide_booking field on the Booking Type. Reproduce the query using the hasOne relationship with its guide_id constraint, then compare it with the working review field and the hasMany workaround. Done means GraphQL returns the constrained GuideBooking model while the Laravel relationship remains hasOne.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, laravel, php
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.