Graphql-laravel: Can i use this with polymorphic relationship?

Created on 8 May 2017  路  7Comments  路  Source: rebing/graphql-laravel

How can we use this with laravel eloquent polymorphic relationship?

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?

All 7 comments

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.

10 I added MorphMany and MorphTo support, although I'm not sure how to create the MorphTo graphql Type (https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-relations 'commentable' in this example).

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

canhkieu picture canhkieu  路  3Comments

constantinosergiou picture constantinosergiou  路  3Comments

jglover picture jglover  路  6Comments

Artem-Schander picture Artem-Schander  路  7Comments

dongkaipo picture dongkaipo  路  4Comments