glesys / butler-graphql

An opinionated GraphQL package for Laravel.
MIT License
34 stars 5 forks source link

Know what children fields are wanted #32

Closed mathieutu closed 3 years ago

mathieutu commented 3 years ago

Hi, I'm digging in your package and making some tests.

I would like to know in a resolver what fields are wanted in the resolved type, to be able to use them in a select.

example:

{
  users {
    id,
    firstName
    lastName
  }
}
class Users implements QueryInterface
{
    public function __invoke($root, array $args, array $context, ResolveInfo $info)
    {
        return User::all(/* [id, firstName, lastName] */);
    }
}

Could we do that?

Thanks.

wecc commented 3 years ago

Hi!

The simplest way would be to use $info->getFieldSelection() (you can specify the depth, 0 by default) to inspect what fields are present in the query.

You could also inspect something like $info->fieldNodes[0]->selectionSet->selections[*] if you really want to inspect field arguments, definitions and more.

Let me know if this helps!

mathieutu commented 3 years ago

Hi! Thanks for this really quick answer, I was just closing the issue saying "nvm, I found getFieldSelection method." 😅

I let you know if I need something else, thanks!