0

I'm using lighthouse for graphql on top of my laravel application. I know how to define types. But how can I use the model accessor in the type definition of schema? For example, i have type use like this

type User {
   id: ID!
   name: String
}

But I don't have a name column in the database table but I have a name accessor in my eloquent model which I want to use in this case instead of weird column name. Is there any way I can use the model accessor?

Adil Amanat
  • 70
  • 1
  • 5

2 Answers2

2

Do you mean methods like getFirstNameAttribute? Simply define a field in snake case in your scheme, for my example it would be first_name: String

type User {
   #...
   first_name: String
}
lorado
  • 336
  • 1
  • 7
1

Your code should work fine if you are using laravel accessor get{Attribute}Attribute, you can also use @method directive:

type User {
   #...
   name: String @method(name: "getName")
}

Checkout: https://lighthouse-php.com/master/api-reference/directives.html#method & https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor

isma0611
  • 63
  • 1
  • 4