Graphql-laravel: All of fields return null if not define resolve the field

Created on 26 Aug 2019  路  3Comments  路  Source: rebing/graphql-laravel

Versions:

  • graphql-laravel Version: ^2.0
  • Lumen Version: "5.8.*"
  • PHP Version: >=7.1.3

Description:

All of data return null if I don't describle the resolve the field yourself

Steps To Reproduce:

UserType.php

<?php

declare(strict_types=1);

namespace App\GraphQL\Types;

use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Type as GraphQLType;

class UserType extends GraphQLType
{
    protected $attributes = [
        'name'          => 'User',
        'description'   => 'Th么ng tin th脿nh vi锚n',
        'model'         => \App\User::class,
    ];

    public function fields(): array
    {
        return [
            'id' => [
                'type' => Type::int(),
                'description' => 'ID',                
            ],
            'name' => [
                'type' => Type::string(),
                'description' => 'T锚n',
            ],
            'email' => [
                'type' => Type::string(),
                'description' => 'Email',
            ],
        ];
    }
}

UsersQuery.php

<?php

declare(strict_types=1);

namespace App\GraphQL\Queries;

use Closure;
use App\User;

use \GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ResolveInfo;
use Rebing\GraphQL\Support\SelectFields;
use Rebing\GraphQL\Support\Query;
use Rebing\GraphQL\Support\Facades\GraphQL;

class UsersQuery extends Query
{
    protected $attributes = [
        'name' => 'Users query',
        'description' => 'L岷 danh s谩ch th脿nh vi锚n'
    ];

    public function type(): Type
    {
        return GraphQL::paginate('user');
    }

    public function args(): array
    {
        return [
            'id' => ['name' => 'id', 'type' => Type::int()],
            'name' => ['name' => 'name', 'type' => Type::string()],
            'email' => ['name' => 'email', 'type' => Type::string()]
        ];
    }

    public function resolve($root, $args, $context, ResolveInfo $info, Closure $getSelectFields)
    {
        $users = \App\User::paginate(5);
        return $users;
    }
}

config\graphql.php

<?php

declare(strict_types=1);

return [

    'prefix' => 'graphql',
    'routes' => '{graphql_schema?}',
    'controllers' => \Rebing\GraphQL\GraphQLController::class . '@query',
    'middleware' => [],
    'route_group_attributes' => [],
    'default_schema' => 'default',
    'schemas' => [
        'default' => [
            'query' => [
                'users' => \App\GraphQL\Queries\UsersQuery::class
            ],
            'mutation' => [
            ],
            'middleware' => [],
            'method'     => ['get', 'post'],
        ],
    ],
    'types' => [
        'user' => \App\GraphQL\Types\UserType::class
    ],
    'lazyload_types' => false,
    'error_formatter' => ['\Rebing\GraphQL\GraphQL', 'formatError'],
    'errors_handler' => ['\Rebing\GraphQL\GraphQL', 'handleErrors'],
    'params_key'    => 'variables',
    'security' => [
        'query_max_complexity'  => null,
        'query_max_depth'       => null,
        'disable_introspection' => false,
    ],
    'pagination_type' => \Rebing\GraphQL\Support\PaginationType::class,
    'graphiql' => [
        'prefix'     => '/graphiql',
        'controller' => \Rebing\GraphQL\GraphQLController::class . '@graphiql',
        'middleware' => [],
        'view'       => 'graphql::graphiql',
        'display'    => env('ENABLE_GRAPHIQL', true),
    ],
    'defaultFieldResolver' => function ($root, $args, $context, $info) { },
    'headers' => [],
    'json_encoding_options' => 0,
];

GraphiQL Request

{
  users {
    data {
      id
      name
      email
    }
    total
    per_page
  }
}

GraphiQL Result

{
  "data": {
    "users": {
      "data": [
        {
          "id": null,
          "name": null,
          "email": null
        },
        {
          "id": null,
          "name": null,
          "email": null
        },
        {
          "id": null,
          "name": null,
          "email": null
        },
        {
          "id": null,
          "name": null,
          "email": null
        },
        {
          "id": null,
          "name": null,
          "email": null
        }
      ],
      "total": 25,
      "per_page": 5
    }
  }
}

But if I add resolve functions in UserType.php -> It's display the exactly data
```
protected function resolveIdField($root, $args)
{
return $root->id;
}
protected function resolveNameField($root, $args)
{
return strtolower($root->name);
}
protected function resolveEmailField($root, $args)
{
return strtolower($root->email);
}

**GraphiQL Result**

{
"data": {
"users": {
"data": [
{
"id": 1,
"name": "reina kiehn",
"email": "giovanni.[email protected]"
},
{
"id": 2,
"name": "chloe waters",
"email": "[email protected]"
},
{
"id": 3,
"name": "estrella barrows",
"email": "[email protected]"
},
{
"id": 4,
"name": "ms. kayli kling",
"email": "[email protected]"
},
{
"id": 5,
"name": "morton stracke",
"email": "blanche.[email protected]"
}
],
"total": 25,
"per_page": 5
}
}
}
```

bug

Most helpful comment

'defaultFieldResolver' => function ($root, $args, $context, $info) { },

This is the problem: you provided a "default field resolver" which returned nothing.

Remove that line and it will work.

All 3 comments

IMHO the test \Rebing\GraphQL\Tests\Database\SelectFields\PrimaryKeyTests\PaginationTest::testPagination exactly covers your scenario.

The resolver \Rebing\GraphQL\Tests\Database\SelectFields\PrimaryKeyTests\PrimaryKeyPaginationQuery::resolve looks a bit different:

    public function resolve($root, $args, $ctx, ResolveInfo $info, Closure $getSelectFields)
    {
        /** @var SelectFields $selectFields */
        $selectFields = $getSelectFields();

        return Post
            ::with($selectFields->getRelations())
            ->select($selectFields->getSelect())
            ->paginate(1);
    }

But the details don't matter, return Post::paginate(1); is supposed to work as well (and also does in my tests).

I'm not sure what's up with the resolving, can you debug?

If you've no progress, please publish a test project on github reproducing the issue.

This is my demo project.
https://github.com/canhkieu/lumen-graphql-blog

After download, please edit .env file and run
php artisan migrate:refresh --seed

After that go to: http://{{uri}}/graphiql to test.

'defaultFieldResolver' => function ($root, $args, $context, $info) { },

This is the problem: you provided a "default field resolver" which returned nothing.

Remove that line and it will work.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

drmax24 picture drmax24  路  6Comments

edgarsn picture edgarsn  路  4Comments

benjamin-ndc picture benjamin-ndc  路  3Comments

timothyvictor picture timothyvictor  路  3Comments

jglover picture jglover  路  6Comments