How can we use this with laravel eloquent polymorphic relationship?
I haven't tried polymorphic relationships, so I assume they don't work? The Support/SelectFields.php class probably has to be modified, but I currently don't have time for it. You can submit a PR, if you want.
The relation post -> comments should work now though
Do you have an example, please : )
@rebing I also want to use this package with polymorphic relations. Do I need to use Union or are regular type and query enough?
The relation post -> comments should work now though
That works as expected.
But I can't make it work the other way round? Was that resolved?
@Traxo7 depends on when you last checked. Since March/April this year a lot has improved, lots of contributions especially to the SelectFields class. Not sure what exact nature you have, but please try it out, also look at the tests as a guide, and report any issues you find!
@mfn
I checked tests and found for example a Like model https://github.com/rebing/graphql-laravel/blob/master/tests/Support/Models/Like.php
So the case I was talking about would be how to query for likes, and then get likeable models along with those likes. In your example, Post and Comment are likeable.
So I presume query would look something like the following
likes() {
id
createdAt
likeable {
__typename
...on Post {
// ...
}
...on Comment {
// ...
}
}
}
But I'm not sure how to approach this, specifically - how to create LikeType, and perhaps even relevant Union (Post | Comment)? Can you point me to a relevant example if it exists?
One of the uses cases for this would be for example get 10 latest likes by user.
EDIT:
Ok it seems it works, I used union. I'll test more then perhaps post example here. If polymorhpism shuold not be resolved with union let me know.
EDIT 2:
So the partial relevant code would be LikeableUnion and LikeType:
// LikeableUnion.php
public function resolveType($value)
{
if ($value instanceof Comment) {
return GraphQL::type('comment');
} elseif ($value instanceof Post) {
return GraphQL::type('post');
}
}
// LikeType.php
public function fields(): array
{
$fields = [
//...
'likeable' => ['type' => GraphQL::type('likeable')]
];
return $fields
}
So it seems to be straightforward, just refer to docs how to include it and make queries.
Most helpful comment
@rebing I also want to use this package with polymorphic relations. Do I need to use Union or are regular type and query enough?