Skip to content

type alias not working as expected #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
timothyvictor opened this issue Sep 1, 2018 · 3 comments
Closed

type alias not working as expected #130

timothyvictor opened this issue Sep 1, 2018 · 3 comments

Comments

@timothyvictor
Copy link

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.

@timothyvictor timothyvictor changed the title type alias not working type alias not working as expected Sep 1, 2018
@anolek
Copy link
Contributor

anolek commented Sep 1, 2018

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;
                }
            ],

@timothyvictor
Copy link
Author

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.

@rebing
Copy link
Owner

rebing commented Sep 3, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants