Skip to content
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

Resolve alias field #211

Closed
zabaala opened this issue Feb 22, 2019 · 12 comments
Closed

Resolve alias field #211

zabaala opened this issue Feb 22, 2019 · 12 comments

Comments

@zabaala
Copy link

zabaala commented Feb 22, 2019

Hi

I'm trying to create somes types and a case sound strange for me. When I try to create a field with a custom name, and I set the correct field name in alias, the aliased value field value is not returned.

The value is returned only if implement resolveCustomFieldResolve() method. It's correct?

Thanks.

@albertcito
Copy link
Contributor

This is my UserType.php with a custom field called images:

return [
    'idUser' => [
        'type' => Type::nonNull(Type::int()),
        'description' => 'User identification, primary key'
    ],
    'images' => [
        'type' => GraphQL::paginate('ImageType'),
        'description' => 'Images create by the user',
        'args' => $pagination
    ],
]
protected function resolveImagesField($root, $args)
{
    $images = $root->images(); 
    return $images->paginate($args['limit'], ['*'], 'page', $args['page']);
}

@zabaala
Copy link
Author

zabaala commented Feb 23, 2019

Thanks, @albertcito.

In my case is different. I want understand because the alias is not resolved automatically.

BTW you are passing $pagination to args, to receive him in resolveImagesField($root, $args) method. What does $pagination do? And where i can find all attributes that can be passed to a field (like as: type, description, args, alias, ...others)?

@rebing
Copy link
Owner

rebing commented Feb 24, 2019

Please elaborate your issue or show an example

@zabaala
Copy link
Author

zabaala commented Feb 25, 2019

@rebing,

Let's say i creating a Customer Type from a model that has a attribute called address_number. Then we would have something similar to this:

<?php

class Subscriber extends Type
{
  // ...
  public function fields()
  {
    'addressNumber' => [
      'type' => Type::int(),
      'alias' => 'address_number'
     ]
  }
}

In that case, if I do not implement the resolveAddressNumberResolve() method, the address_number value will not be returned, although the field is being queried in the database. My question is: should not this / could happen automatically, since I am informing the alias?

This ends up generating unnecessary code. Implementing this method would be interesting in cases such as the email field mentioned in the example of the repository itself, where the field needs to be returned in the low case.

@anolek
Copy link
Contributor

anolek commented Feb 27, 2019

Same issue as #130 if i understand correctly your problem

<?php

class Subscriber extends Type
{
  // ...
  public function fields()
  {
    'addressNumber' => [
      'type' => Type::int(),
      'alias' => 'address_number',
      'resolve' => function ($root) { // As a workaround
               return $root->address_number;
      }
     ]
  }
}

@zabaala
Copy link
Author

zabaala commented Feb 27, 2019

@anolek exactly!

My question is: because alias is not resolved automatically as mentioned by @rebing here?

Follow a piece of his 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.

@rebing
Copy link
Owner

rebing commented Mar 3, 2019

@zabaala As I mentioned in the other thread, you can look into it and find the problem in SelectFields class.

@mfn
Copy link
Collaborator

mfn commented May 31, 2019

@zabaala I believe this was fixed with #283 , can you try the v1.22.0 release?

@anolek
Copy link
Contributor

anolek commented Jun 17, 2019

After upgrading to v1.22.0, it is, indeed fixed @mfn

@zabaala
Copy link
Author

zabaala commented Jun 17, 2019

I'll try it soon, @mfn.

Thanks.

@dharkness
Copy link

Aliases work for me as well with v1.22.0.

@mfn
Copy link
Collaborator

mfn commented Jul 1, 2019

There has been now feedback from OP but given the other feedback and the PR I merged, I'm pretty sure this is fixed.

If not, please simply create a new issue => thanks!

@mfn mfn closed this as completed Jul 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants