diff --git a/CHANGELOG.md b/CHANGELOG.md index 46b8560e..1c6b3ef4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG Next release ------------ - This project has a changelog `\o/` +- Auto-resolve aliased fields [\#283](https://github.com/rebing/graphql-laravel/pull/283) 2019-03-07, v1.21.2 ------------------- diff --git a/src/Rebing/GraphQL/Support/Type.php b/src/Rebing/GraphQL/Support/Type.php index 0cc17d45..e68454f6 100644 --- a/src/Rebing/GraphQL/Support/Type.php +++ b/src/Rebing/GraphQL/Support/Type.php @@ -49,6 +49,14 @@ protected function getFieldResolver($name, $field) return call_user_func_array($resolver, $args); }; } + + if (isset($field['alias'])) { + $alias = $field['alias']; + + return function ($type) use ($alias) { + return $type->{$alias}; + }; + } } public function getFields() diff --git a/tests/Objects/ExampleType.php b/tests/Objects/ExampleType.php index 6a379aec..9f7a3b9f 100644 --- a/tests/Objects/ExampleType.php +++ b/tests/Objects/ExampleType.php @@ -18,6 +18,10 @@ public function fields() 'description' => 'A test field', ], 'test_validation' => ExampleValidationField::class, + 'test_with_alias' => [ + 'type' => Type::string(), + 'alias' => 'testWithAlias', + ], ]; } }