nuwave / lighthouse

A framework for serving GraphQL from Laravel
https://lighthouse-php.com
MIT License
3.34k stars 439 forks source link

Change the default field resolver #2191

Open spawnia opened 2 years ago

spawnia commented 2 years ago

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.

olivernybroe commented 1 year ago

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

pyrou commented 1 year ago

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 😉