1
<?php
2

3
namespace Nuwave\Lighthouse\Schema\Directives;
4

5
use Nuwave\Lighthouse\Exceptions\DefinitionException;
6
use Nuwave\Lighthouse\Schema\Values\FieldValue;
7
use Nuwave\Lighthouse\Support\Contracts\FieldResolver;
8

9
class RenameDirective extends BaseDirective implements FieldResolver
10
{
11 1
    public static function definition(): string
12
    {
13
        return /** @lang GraphQL */ <<<'GRAPHQL'
14 1
"""
15
Change the internally used name of a field or argument.
16

17
This does not change the schema from a client perspective.
18
"""
19
directive @rename(
20
  """
21
  The internal name of an attribute/property/key.
22
  """
23
  attribute: String!
24
) on FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
25
GRAPHQL;
26
    }
27

28 1
    public function resolveField(FieldValue $fieldValue): FieldValue
29
    {
30 1
        $attribute = $this->attributeArgValue();
31

32 1
        return $fieldValue->setResolver(
33
            function ($rootValue) use ($attribute) {
34 1
                return data_get($rootValue, $attribute);
35
            }
36
        );
37
    }
38

39
    /**
40
     * Retrieves the attribute argument for the directive.
41
     *
42
     * @throws \Nuwave\Lighthouse\Exceptions\DefinitionException
43
     */
44 1
    public function attributeArgValue(): string
45
    {
46 1
        $attribute = $this->directiveArgValue('attribute');
47

48 1
        if (! $attribute) {
49 1
            throw new DefinitionException(
50 1
                "The @{$this->name()} directive requires an `attribute` argument."
51
            );
52
        }
53

54 1
        return $attribute;
55
    }
56
}

Read our documentation on viewing source code .

Loading