lighthouse
lighthouse copied to clipboard
@whereAuth directive doesn't return pivot of linked relationship
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
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
Closing due to lack of reproducibility.