nuwave / lighthouse

A framework for serving GraphQL from Laravel
https://lighthouse-php.com
MIT License
3.36k stars 437 forks source link

Implement supplementary directives for all Eloquent relationship types #332

Open spawnia opened 6 years ago

spawnia commented 6 years ago

Is your feature request related to a problem? Please describe.

Many users have come up with use cases where they require some of the other relationship types of Eloquent, such as Polymorphic relationships or Pivot Relations. In many cases, they work fine without any directives or do also work when using @belongsTo or @hasMany, although edge cases do come up from time to time.

To clarify: Other relationship types still work without those directives. They can simply be declared as fields on the type without using any directives.

Describe the solution you'd like

Implement additional directives for Eloquent's other relationship types. I am sure we can do some nice optimizations and make Lighthouse even more simple to use.

https://laravel.com/docs/eloquent-relationships#defining-relationships

Support

spawnia commented 5 years ago

@enzonotario have you started work on @morphOne and @morphMany? I would like to get those in before the next release.

enzonotario commented 5 years ago

not yet, but I can work on that this weekend!

sascha1337 commented 4 years ago

I would like to get the Roles of my UserModel, im using spatie/laravel-permissions. It's adding a roles() method with return MorphToMany. How would you guys put the reverse into the Schema - morphedByMany as inverse ? Documentation does not show this directive, but there is a Model in the Tests using morphedByMany here: https://github.com/nuwave/lighthouse/blob/36552f2886296d0e324c3482b5cef155a53b9501/tests/Utils/Models/Tag.php

Any clue ? When @morphedByMany implementation ?

lorado commented 4 years ago

Could you try @belongsToMany? It works somehow, however I didn't look into the code, so don't know how it exactly works.

You can currently fetch the data also without directive, but you will not have performance benefits from data loaders, as every field will be resolved separately, means every time laravel goes to DB.

sascha1337 commented 4 years ago

Sounds like a workaround, would be amazing to get this fixed / implemented :-)