0

I'm using Lighthouse package to implement GraphQL, the users in my app belong to an entity and I need to get models that belong to the entity of each user, my schema is like this for the moment

"A date string with format `Y-m-d`, e.g. `2011-05-23`."
scalar Date @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\Date")

"A datetime string with format `Y-m-d H:i:s`, e.g. `2018-05-23 13:43:32`."
scalar DateTime @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\DateTime")

type Query {
    me: User @auth
    execution: [RobotTime!]! @all
}

type RobotTime {
    id: ID!
    instance: Instance!
    robot: Robot!
    start: DateTime!
    end: DateTime!
}

type Instance {
    id: ID!
    name: String!
    token: String!
}

type Robot {
    id: ID!
    start_bot: String!
    process: Process!
}

type Process {
    id: ID!
    name: String!
    token: String!
}
type User {
    id: ID!
    name: String!
    email: String!
    created_at: String!
    updated_at: String
}

I have searched the documentation, but I can't find anything that helps me to do what I need to do.

Currently I have it done in a controller and I return it as a json with a not so complex logic, there are about 5 models that I use.

Sorry for my bad English

LePepe
  • 53
  • 6

1 Answers1

0

You need to declare the appropriate relation in your User model in laravel, sth like this:

public function entity(): BelongsTo
{
    return $this->belongsTo(Entity::class);
}

Then add the relation in User type in graphql:

type User {
    id: ID!
    name: String!
    email: String!
    created_at: DateTime!
    updated_at: DateTime

    entity: Entity @belongsTo
}

Of course, you'll need to declare an Entity model in laravel and type in graphql.

Alireza A2F
  • 519
  • 4
  • 26