opened
dharkness
username_0
### Versions:

- graphql-laravel Version: 1.22.0
- Laravel Version: 5.5.45
- PHP Version: 7.2.19

### Description:

The `$args` passed to an Eloquent relation closure defined in a type's `fields()` function are those for the top-level query instead of the args on the relation itself in the GraphQL query—the ones passed to the field resolver function.

### Steps To Reproduce:

1. Create types and Eloquent models to support this query of a simple parent-child model with a one-to-many relation:

```graphql
query {
    parents(name: "bob") {
        name
        children(minAge: 25) {
            name
        }
    }
}
```

2. Define a `query` for the `children` field on the `Parent` type to get eager loading.

```php
class ParentType extends Type {
    protected $attributes = [
        'name'          => 'Parent',
        'model'         => Parent::class,
    ];
    public function fields() {
        return [
            ...
            'children' => [
                'type' => Type::listOf(GraphQL::type('Child')),
                'args' => [
                    'id' => ['type' => Type::int()],
                    'model' => ['type' => Type::string()],
                ],
                'query' => function (array $args, $query) {    // WRONG: $args = ['name' => 'bob']
                    if (isset($args['minAge'])) {
                        return $query->where('age', '>=', $args['minAge']);
                    }
                    else {
                        return $query;    // and so the subquery is not limited, loading too many rows
                    }
                },
            ],
        ];
    }
}
```

### Workaround:

Add a custom field resolver for the relation. This may be the intended solution, but it means we end up loading much more data than necessary.

```php
class ParentType extends Type {
    ...
    public function resolveChildrenField($root, $args) {    // RIGHT: $args = ['minAge' => 25]
        if (isset($args['minAge'])) {
            return $root->children->where('age', '>=', $args['minAge']);
        }
        else {
            return $root->children;
        }
    }
}
```
Eloquent relation closure in type's fields config gets wrong $args
issue
created
mfn
username_1
This was fixed with https://github.com/rebing/graphql-laravel/pull/327 recently but only in master which will eventually become v2 of this library (no ETA at this point).
comment
closed
mfn
username_1
issue
created
dharkness
username_0
Sweet, it works. Thank you so much!
comment