-
-
Notifications
You must be signed in to change notification settings - Fork 439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change the default field resolver #2191
Comments
I think this could be a great idea and something that would improve performance greatly. I have however tried to also submit a pr to Laravel with the issue itself as webonyx/graphql is prob. not the only package relying on ArrayAccess being implemented correctly. laravel/framework#45976 |
I am using for while now the following "DefaultResolver": <?php
namespace App\GraphQL\Resolvers;
use ArrayAccess;
use Closure;
use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Support\Str;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
class DefaultResolver
{
public function __invoke(mixed $parent, $args, GraphQLContext $context, ResolveInfo $resolveInfo): mixed
{
$fieldName = $resolveInfo->fieldName;
$snakeFieldName = Str::snake($fieldName);
$value = $this->getFieldValue($parent, $fieldName);
if ($value === null && $snakeFieldName !== $fieldName) {
// Give a chance to resolve field using the snake case
$value = $this->getFieldValue($parent, $snakeFieldName);
}
return $value instanceof Closure ? $value($parent, $args, $context, $resolveInfo) : $value;
}
protected function getFieldValue(mixed $parent, string $fieldName): mixed
{
if (is_array($parent) || $parent instanceof ArrayAccess) {
return $parent[$fieldName] ?? null;
}
if (is_object($parent) && isset($parent->{$fieldName})) {
return $parent->{$fieldName};
}
return null;
}
} It is plugged into lighthouse with It mainly address the If this get you some idea for a new default resolver on Lighthouse 6 😉 |
What problem does this feature proposal attempt to solve?
Community feedback has shown that the default resolver from
webonyx/graphql-php
may not be optimally suited for a Laravel application and there is potential for improvement.Because the default resolver is called very often, we have to consider performance.
Which possible solutions should be considered?
Given Eloquent models are probably the most common returned data type, we could optimize for them.
Changing the default resolver is documented here, but could perhaps be simplified through the config.
The text was updated successfully, but these errors were encountered: