-
I'm trying to get my // UserType.php
public function fields(): array
{
return [
'role' => [
'type' => GraphQL::type('Role!'),
'description' => 'The role of the user for the current business.',
'resolve' => function (User $user) {
return $user->businesses->where('id', currentBusiness()->id)->first()->pivot->role;
},
'selectable' => false,
],
];
} Currently the What I essentially want to do is define a // UserType.php
public function fields(): array
{
return [
'role' => [
'type' => GraphQL::type('Role!'),
'description' => 'The role of the user for the current business.',
'resolve' => function (User $user) {
return $user->businesses->where('id', currentBusiness()->id)->first()->pivot->role;
},
'selectable' => false,
'with' => [
'businesss',
],
],
];
} Is there a way to eager-load the businesses? |
Beta Was this translation helpful? Give feedback.
Answered by
mfn
Aug 10, 2023
Replies: 1 comment 4 replies
-
How do your actual |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Basically the way the underlying graphql-php library suggests it, see https://webonyx.github.io/graphql-php/data-fetching/#solving-n1-problem
The realization to start returning
new Deferred()
with a closure from a resolver is key to achive the lazy-eager-loading correctly.A commonly used library is https://github.com/overblog/dataloader-php but I found it better in my project to use a custom implementation.