lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

Change the default field resolver

Open spawnia opened this issue 3 years ago • 3 comments

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.

  • Model accessors are called twice
  • snake_case vs. camelCase
    • https://github.com/nuwave/lighthouse/issues/2188#issuecomment-1204080217
    • https://github.com/nuwave/lighthouse/issues/1561
  • https://github.com/nuwave/lighthouse/issues/2687

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.

spawnia avatar Aug 04 '22 07:08 spawnia

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. https://github.com/laravel/framework/pull/45976

olivernybroe avatar Feb 06 '23 09:02 olivernybroe

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 Executor::setDefaultFieldResolver($defaultResolver);

It mainly address the @rename overuse.

If this get you some idea for a new default resolver on Lighthouse 6 😉

pyrou avatar Mar 02 '23 17:03 pyrou