laravel-graphql icon indicating copy to clipboard operation
laravel-graphql copied to clipboard

what is the difference between return type listOf() and GraphQL?

Open pramonoagung opened this issue 6 years ago • 3 comments

public function type() { return Type::listOf(GraphQL::type('User')); } public function type() { return GraphQL::type('User')); }

pramonoagung avatar Jul 18 '18 14:07 pramonoagung

  • With the former you can return User:where(…)->get() (i.e. a collection)
  • with the latter only User::where()->firstOrFail() (i.e.. only a single model)

Makes sense?

mfn avatar Jul 18 '18 16:07 mfn

I got an error when using listOf() with User::findOrFail($args['id']). The error message was

User Error: expected iterable, but did not find one for field Query

So, we have to use User::where('id',$args['id'])->get() instead of User::findOrFail($args['id']) when using listOf() method

Thanks @mfn

pramonoagung avatar Jul 20 '18 02:07 pramonoagung

@pramonoagung Note that there is no reason to use listOf() when only ever returning at most one model.

ThisIsJustARandomGuy avatar Jul 26 '18 13:07 ThisIsJustARandomGuy