1
<?php
2

3
namespace Nuwave\Lighthouse\GlobalId;
4

5
use Closure;
6
use Nuwave\Lighthouse\Exceptions\DefinitionException;
7
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
8
use Nuwave\Lighthouse\Schema\Values\FieldValue;
9
use Nuwave\Lighthouse\Support\Contracts\ArgDirective;
10
use Nuwave\Lighthouse\Support\Contracts\ArgSanitizerDirective;
11
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
12
use Nuwave\Lighthouse\Support\Contracts\GlobalId;
13

14
class GlobalIdDirective extends BaseDirective implements FieldMiddleware, ArgSanitizerDirective, ArgDirective
15
{
16
    /**
17
     * @var \Nuwave\Lighthouse\Support\Contracts\GlobalId
18
     */
19
    protected $globalId;
20

21 1
    public function __construct(GlobalId $globalId)
22
    {
23 1
        $this->globalId = $globalId;
24
    }
25

26 1
    public static function definition(): string
27
    {
28
        return /** @lang GraphQL */ <<<'GRAPHQL'
29 1
"""
30
Converts between IDs/types and global IDs.
31

32
When used upon a field, it encodes;
33
when used upon an argument, it decodes.
34
"""
35
directive @globalId(
36
  """
37
  Decoding a global id produces a tuple of `$type` and `$id`.
38
  This setting controls which of those is passed along.
39
  """
40
  decode: GlobalIdDecode = ARRAY
41
) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
42

43
"""
44
Options for the `decode` argument of `@globalId`.
45
"""
46
enum GlobalIdDecode {
47
    """
48
    Return an array of `[$type, $id]`.
49
    """
50
    ARRAY
51

52
    """
53
    Return just `$type`.
54
    """
55
    TYPE
56

57
    """
58
    Return just `$id`.
59
    """
60
    ID
61
}
62
GRAPHQL;
63
    }
64

65 1
    public function handleField(FieldValue $fieldValue, Closure $next): FieldValue
66
    {
67 1
        $type = $fieldValue->getParentName();
68

69
        $fieldValue->resultHandler(function ($result) use ($type) {
70 1
            return $this->globalId->encode($type, $result);
71 1
        });
72

73 1
        return $next($fieldValue);
74
    }
75

76
    /**
77
     * Decodes a global id given as an argument.
78
     *
79
     * @param  string  $argumentValue
80
     * @return string|array<string>
81
     */
82 1
    public function sanitize($argumentValue)
83
    {
84 1
        if ($decode = $this->directiveArgValue('decode')) {
85
            switch ($decode) {
86 1
                case 'TYPE':
87 1
                    return $this->globalId->decodeType($argumentValue);
88 1
                case 'ID':
89 1
                    return $this->globalId->decodeID($argumentValue);
90 0
                case 'ARRAY':
91 0
                    return $this->globalId->decode($argumentValue);
92
                default:
93 0
                    throw new DefinitionException(
94 0
                        "The decode argument of the @globalId directive can only be TYPE, ARRAY or ID, got {$decode}"
95
                    );
96
            }
97
        }
98

99 1
        return $this->globalId->decode($argumentValue);
100
    }
101
}

Read our documentation on viewing source code .

Loading