Graphql-laravel: type alias not working as expected

Created on 1 Sep 2018  路  3Comments  路  Source: rebing/graphql-laravel

Hello. Really enjoying using this package, but I'm struggling to get the alias key in the type fields method working.

I have the following:

<?php

namespace App\GraphQL\Admin\Type;

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

class ClosureType extends GraphQLType {

    protected $attributes = [
        'name'          => 'closure',
        'description'   => 'A period when the school is closed',
        'model'         => Closure::class,
    ];

    public function fields()
    {
        return [
            'id' => [
                'type' => Type::nonNull(Type::string()),
                'description' => 'The id of the closure',
                // 'alias' => 'user_id', // Use 'alias', if the database column is different from the type name
            ],
            'start_date' => [
                'type' => Type::string(),
                'description' => 'The start date of the closure',
            ],
            'end_date' => [
                'type' => Type::string(),
                'description' => 'The date of the closure',
            ],
            'start_period' => [
                'type' => Type::int(),
                'description' => 'The first period of the closure',
                'alias' => 'start_period_number', // this is the db column
            ],
            'end_period' => [
                'type' => Type::int(),
                'description' => 'The last period of the closure',
                'alias' => 'end_period_number',
            ],
        ];
    }
}

but both start_period and end_period return null:
screen shot 2018-09-01 at 13 28 37

However if I use the column name as the field key:

public function fields()
    {
        return [
           //...
            'start_period_number' => [
                'type' => Type::int(),
                'description' => 'The first period of the closure',
            ],
            'end_period_number' => [
                'type' => Type::int(),
                'description' => 'The last period of the closure',
            ],
        ];
    }

then the queries work as expected:

screen shot 2018-09-01 at 13 31 49

Have I misunderstood how the alias works?

Any pointers would be greatly appreciated.

Thanks in advance,

Tim.

Most helpful comment

To be honest, I don't think I've ever used the alias field myself, but that doesn't look like the correct behavior. It should be as you said that it automatically resolves to the correct field. So if the alias is end_period_number, then it should resolve to $root->end_period_number automatically.

Briefly looking at the code it should work as intended, so I'm not sure where the problem is. You can dig deeper by starting from Rebing\GraphQL\Support\SelectFields::219

All 3 comments

You also need to add "resolve".
I don't know if it's the expected behavior of "alias", but without it won't work.

Example :

            'start_period' => [
                'type' => Type::int(),
                'description' => 'The first period of the closure',
                'alias' => 'start_period_number', // this is the db column
                'resolve' => function ($root) {
                                return $root->start_period_number;
                }
            ],
            'end_period' => [
                'type' => Type::int(),
                'description' => 'The last period of the closure',
                'alias' => 'end_period_number',
                'resolve' => function ($root) {
                                return $root->end_period_number;
                }
            ],

Ah, thank you, @anolek. I think I was expecting, based on the following comment in the docs, that adding the column name to the alias would handle the resolve:

// 'alias' => 'user_id', // Use 'alias', if the database column is different from the type name

I wonder in what instance one might want the alias value to be different form the name of property with which to resolve the field?

If this is the intended behaviour, then I'll submit a PR to update the docs. If not, then I'll attempt to PR the change.

Thanks again.

To be honest, I don't think I've ever used the alias field myself, but that doesn't look like the correct behavior. It should be as you said that it automatically resolves to the correct field. So if the alias is end_period_number, then it should resolve to $root->end_period_number automatically.

Briefly looking at the code it should work as intended, so I'm not sure where the problem is. You can dig deeper by starting from Rebing\GraphQL\Support\SelectFields::219

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stevelacey picture stevelacey  路  8Comments

kirgy picture kirgy  路  8Comments

nozols picture nozols  路  8Comments

canhkieu picture canhkieu  路  3Comments

drmax24 picture drmax24  路  6Comments