Skip to content

Wrap Types aren’t working correctly in the query #1184

@ericsans

Description

@ericsans

Versions:

  • graphql-laravel Version: 9.8.0
  • Laravel Version: 10
  • PHP Version: 8.3

Description:

I followed the example, but it still isn’t going as expected. (here)

MessageWrapper.php

<?php
declare(strict_types = 1);

namespace App\GraphQL\Types;


use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;

class MessageWrapper
{
    /**
     * @param string $typeName type graphql
     */
    public static function type(string $typeName): Type
    {
        return GraphQL::wrapType(
            $typeName,
            $typeName . 'Messages',
            WrapperType::class
        );
    }
}

WrapperType.php

<?php

declare(strict_types = 1);
namespace App\GraphQL\Types;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type as GraphQLType;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Rebing\GraphQL\Support\Facades\GraphQL;

class WrapperType extends ObjectType
{
    public function __construct(string $typeName, ?string $customName = null)
    {
        $name = $customName ?: $typeName . 'Wrapper';

        $underlyingType = GraphQL::type($typeName);

        $config = [
            'name' => $name,
            'fields' => $this->getMessagesFields($underlyingType),
        ];

        if (isset($underlyingType->config['model'])) {
            $config['model'] = $underlyingType->config['model'];
        }

        parent::__construct($config);
    }

    protected function getMessagesFields(GraphQLType $underlyingType): array
    {
        return [
            'data' => [
                'type' => GraphQLType::nonNull(GraphQLType::listOf(GraphQLType::nonNull($underlyingType))),
                'description' => 'List of items on the current page',
                'resolve' => function (LengthAwarePaginator $data): Collection {
                    return $data->getCollection();
                },
            ],
        ];
    }
}

OwnerQuery.php

<?php

declare(strict_types=1);

namespace App\GraphQL\Queries;

use App\GraphQL\Types\MessageWrapper;
use App\GraphQL\Types\WrapperType;
use App\Models\Owner;
use Closure;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Query;
use Rebing\GraphQL\Support\SelectFields;

class OwnersQuery extends Query
{
    protected $attributes = [
        'name' => 'owners',
        'description' => 'Display a listing of the Owner resources',
    ];

    public function type(): Type
    {
        return MessageWrapper::type('Owner');
        // return GraphQL::paginate('Owner');
    }

    public function args(): array
    {
        return [
            
        ];
    }

    public function resolve($root, array $args, $context, ResolveInfo $resolveInfo, Closure $getSelectFields)
    {
        /** @var SelectFields $fields */
        $fields = $getSelectFields();
        $select = $fields->getSelect();
        $with = $fields->getRelations();

        $owners = Owner::query()
            ->with($with)
            ->select($select)
            ->paginate(2);

        return $owners;
    }
}

Query

query Owners {
    owners {
        data {
            id
            full_name
        }
    }
}

Problem

I always get this SQL error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from `owners` where `owners`.`deleted_at` is null limit 2 offset 0' at line 1 (Connection: mysql, SQL: select  from `owners` where `owners`.`deleted_at` is null limit 2 offset 0)

So the query ends up as select from owners... (with no fields selected).

But when I change the return type to use: GraphQL::paginate('Owner'); it works correctly and returns the result.

Best regards,
Eric Santoso

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions