lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

@whereAuth directive doesn't return pivot of linked relationship

Open JoaoHCopetti opened this issue 3 years ago • 1 comments

Describe the bug When using @whereAuth directive to filter models owned by the user, if the model has a pivot table the directive doesn't return it.

This is the query i'm using, every pivot is returning null.

{
  commissionsUser (first: 10) {
    data {
      id
      pivot {
        id
      }
    }
  }
}

Expected behavior/Solution

Lighthouse should return the pivot table of the relationship when using the @whereAuth directive if it has a pivot.

Steps to reproduce Here is the schema:

type CommissionUserPivot {
  id: ID!
  commission_value: Float
  confirmed_at: Boolean
  was_quantity_changed: Boolean
  user: User @belongsTo
  role: Role @belongsTo
}

type Commission {
  id: ID!
  pivot: CommissionUserPivot
}

The relationship method on User class:

public function commissions()
{
    return $this->belongsToMany(Commission::class)
        ->using(CommissionUser::class)
        ->withPivot([
            'id',
            'commission_value',
            'confirmed_at',
            'was_quantity_changed',
            'role_id'
        ]);
}

Also i made the same relationship on Commission class to be sure:

public function users()
{
    return $this->belongsToMany(User::class)
        ->using(CommissionUser::class)
        ->withPivot([
            'id',
            'commission_value',
            'confirmed_at',
            'was_quantity_changed',
            'role_id'
        ]);
}

If i use the directive every pivot is returned null, for example:

extend type Query {
  commissionsUser: [Commission] @paginate @whereAuth(relation: "users")
}
{
  "data": {
    "commissionsUser": {
      "data": [
        {
          "id": "33",
          "pivot": null
        },
        {
          "id": "34",
          "pivot": null
        }
      ]
    }
  }
}

But if i use a custom builder, every pivot is returned as intended:

extend type Query {
  commissionsUser: [Commission] @paginate(builder: "\\App\\GraphQL\\Builders\\CommissionsUserBuilder")
}
class CommissionsUserBuilder
{
    public function __invoke()
    {
        return Auth::user()->commissions();
    }
}
{
  "data": {
    "commissionsUser": {
      "data": [
        {
          "id": "33",
          "pivot": {
            "id": "1"
          }
        },
        {
          "id": "34",
          "pivot": {
            "id": "2"
          }
        }
      ]
    }
  }
}

Lighthouse Version 5.39

JoaoHCopetti avatar Feb 21 '22 15:02 JoaoHCopetti

The first step is the addition of a failing test case, probably to https://github.com/nuwave/lighthouse/blob/master/tests/Integration/Schema/Directives/BelongsToManyDirectiveTest.php

spawnia avatar Feb 21 '22 19:02 spawnia

Closing due to lack of reproducibility.

spawnia avatar Aug 09 '23 08:08 spawnia