folkloreinc / laravel-graphql

Facebook GraphQL for Laravel 5. It supports Relay, eloquent models, validation and GraphiQL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What does the parameters of resolve($root, $args, SelectFields $fields, ResolveInfo $info) method mean?

zwl1619 opened this issue · comments

What does the parameters of resolve($root, $args, SelectFields $fields, ResolveInfo $info) method mean?

    public function resolve($root, $args, SelectFields $fields, ResolveInfo $info)
    {
        return [];
    }

as follow:
1、$root
2、$args
3、SelectFields $fields
4、ResolveInfo $info

Resolving is a GraphQL context to retrieve data; see also the docs for the underlying library: http://webonyx.github.io/graphql-php/data-fetching/

You're question is a bit light on the surrounding context, but I'll give it a shot:

  • $root refers to the "parent" type; it's empty if there's none. For example you query { user { orders { … you can access user from your orders resolve()
  • $args are the arguments passed to the node, e.g. user(arg1: value1)
  • $fields is ... I don't know where you got this from exactly, it's supposed to be the $context. The default Folklore context is the authenticated user but you can override it to be anything
  • $info is directly from http://webonyx.github.io/ to access the current node (AST?) tree to extract (look ahead) further information

I rarely do need $root, $args is a no-brainer, $context is very important (authentication) and for complex optimizations I need to access $info (via \GraphQL\Type\Definition\ResolveInfo::getFieldSelection)

If you're new to the whole GraphQL world I recommend getting acquainted with the architecture in general => http://facebook.github.io/graphql/

HTH