0

I read Laravel Lighthouse documentation and searched around the web I did not find how to query a trait in a third party package in Laravel. I'm using the qirolab/laravel-reactions package, it has the reactionSummary() trait. I was asking how can I add this relation in the lighthouse query?

type Post {
    id: ID!
    title: String!
    excerpt: String!
    image_url: String!
    slug: String!
    source: Source! @belongsTo
    reactionSummary: ???????
    created_at: DateTime!
    updated_at: DateTime!
}

I have a purpose with my question beside solving my issue, understanding how lighthouse work with packages or how to integrate third party packages with lighthouse?

user2682025
  • 656
  • 2
  • 13
  • 39

1 Answers1

0

You need to define the schema for the reaction model in graphql and in the post schema you need to define an array of the reaction type.

According to the model (https://github.com/qirolab/laravel-reactions/blob/master/src/Models/Reaction.php) the reaction graphql schema would look something like this:

type Reaction {
   reactBy: User! @belongsTo
   type: String
   reactable: Reactable! @morphTo
}

Your post schema would change to

type Post {
    id: ID!
    title: String!
    excerpt: String!
    image_url: String!
    slug: String!
    source: Source! @belongsTo
    reactionSummary: [Reaction]
    created_at: DateTime!
    updated_at: DateTime!
}

As I can see from the migration file https://github.com/qirolab/laravel-reactions/blob/master/migrations/2018_07_10_000000_create_reactions_table.php. The reaction has a polymorphic relation.

That means the returning type can vary depending on which kind of model is set as the reactable_type in the field. Therefore you need to define your own Union type.

A Union is an abstract type that simply enumerates other Object Types. They are similar to interfaces in that they can return different types, but they can not have fields defined.

Source: https://lighthouse-php.com/5/the-basics/types.html#union

See also polymorphic relations and the union section: https://lighthouse-php.com/5/eloquent/polymorphic-relationships.html#one-to-one

I hope that gives you the direction on how you can proceed.

jobnomade
  • 180
  • 1
  • 4
  • Hi @jobnomade Thank you very much for the great explanation it helped me a lot to understand lighthouse more, But unfortunately it's not answering my question because this way it only returns the model, I was asking about the trait in the package since it's making calculations and returns it back. otherwise this way we could make our own models without the need of any packages. and my question had two purpose, first understanding how lighthouse work with packages or how to integrate packages with lighthouse, and second fixing my issue. thanks a lot again it was very helpful. – user2682025 Nov 16 '21 at 15:54
  • The trait extends your model with new relations, so there is no "lighthouse" way to integrate a package in my honest opinion. If you have the package in question installed and the migration run, you can create your mutations or queries to access the data from the package provided models (included by the trait). For that reason you need to define the proper schema in graphql. The reactable package and lighthouse won't do this for you. That's the reason why I tried to explain you that you need to define the reactable schema yourself with some examples. – jobnomade Nov 17 '21 at 08:27