Lighthouse: Missing documentation on querying pivot data.

Created on 9 Jan 2020  路  7Comments  路  Source: nuwave/lighthouse

(I'll try to write a pull-request with updated documentation soon, if you wish)

Describe the bug

Lighthouse has the possibility to query data from a pivot-table.
This option is not documented.

You need to use a key named "pivot" in your schema to be able to retrieve pivot data.

type Role {
    id: ID!
    pivot: UserRolePivot #this key MUST be called pivot
}
type User {
    id: ID!
    roles: [Role!] @belongsToMany
}
type UserRolePivot {
    meta: String # pivot data
}

then you can query like this:

{
  users {
    id
    roles {
      id
      pivot {
        meta
      }
    }
  }
}

If you call it anything other than "pivot" it will not work.

Expected behavior/Solution

I would expect the documentation to give an example on querying pivot data.

Environment

Lighthouse Version: 4.7

question

All 7 comments

I think you're accessing to the Pivot via pivot key just because it's name. I mean, it's not a specific Lighthouse thing.
In fact, as your pivot: UserRolePivot doesn't have any directive, Lightouse tries to get it from the $root (that is an Eloquent model that implements the magic __get function), so it just does $root->pivot and now it has the Pivot. Then for the meta it just does $root->meta ($root is now the Pivot), and that's all.

So, sure, we can document it, but I think it would looks like a guide to how to write queries and understand the logic behind, not specific to Pivot, but to anything.

I also think this is more laravel specific, as you actually can define the name instead of just using pivot. As DB grows sometimes one model can have multiple pivots, and they needs different names.

For example in my App\Project I have:

    public function candidates(): BelongsToMany
    {
        return $this->belongsToMany('App\Candidate')
                    ->withTrashed()
                    ->withTimestamps()
                    ->as('candidate_project_pivot')
                    ->withPivot('status', 'is_shortlisted');
    }

So I am able to query candidates and access the pivot of project-candidate relation via candidate_project_pivot attribute.

But you can still write docs somewhere, maybe in tutorial? I think it is a good help for beginners

Interesting! I didn't know this was possible in Laravel, I suppose I'm a beginner :-)

I'll see if I can find a logical place for this information.
The tutorial might become a bit long if this was added.

@enzonotario and @lorado,
I can see now that this is not a Lighthouse specific thing, but more Laravel related.

Thank you both for your feedback.

Not my use case, but I was thinking, what if it would have multiple pivots (i.e. from another model as well)?

It doesn't make much sense to me to do it like this, especially if it cannot have a different name.

Addendum: I hope the solution would not be creating a custom resolver...

@pedzed Sorry, I don't get it, why can't you have different name? Using ->as('candidate_project_pivot') you can call pivot array how ever you want. And again - this is laravels feature, not lighthouse

In my project I am using different pivots. E.g.:

type Candidate {
    id: ID
    candidate_project_pivot: CandidateProjectPivot
    bucket_candidate_pivot: BucketMorphItemPivot
    candidate_ch_category_pivot: CandidateChCategoryPivot
}

And so I am able to query everything out of pivot tables (don't forget to use withPivot() to let laravel fetch data for you)

@lorado That makes sense. Thanks for clearing that up. I thought @HendrikJan had already tested that, because he explicitly stated:

pivot: UserRolePivot #this key MUST be called pivot
Was this page helpful?
0 / 5 - 0 ratings

Related issues

souljacker picture souljacker  路  3Comments

let-aurn picture let-aurn  路  3Comments

mikeerickson picture mikeerickson  路  3Comments

spawnia picture spawnia  路  4Comments

eriktisme picture eriktisme  路  4Comments