1
<?php
2

3
namespace Nuwave\Lighthouse\Schema\Factories;
4

5
use GraphQL\Language\AST\InputValueDefinitionNode;
6
use Nuwave\Lighthouse\Schema\AST\ASTHelper;
7
use Nuwave\Lighthouse\Schema\ExecutableTypeNodeConverter;
8

9
class ArgumentFactory
10
{
11
    /**
12
     * Convert input value definitions to a executable types.
13
     *
14
     * @param  iterable<\GraphQL\Language\AST\InputValueDefinitionNode>  $definitionNodes
15
     * @return array<string, array<string, mixed>>
16
     */
17 1
    public function toTypeMap($definitionNodes): array
18
    {
19 1
        $arguments = [];
20

21 1
        foreach ($definitionNodes as $inputDefinition) {
22 1
            $arguments[$inputDefinition->name->value] = $this->convert($inputDefinition);
23
        }
24

25 1
        return $arguments;
26
    }
27

28
    /**
29
     * Convert an argument definition to an executable type.
30
     *
31
     * The returned array will be used to construct one of:
32
     * @see \GraphQL\Type\Definition\FieldArgument
33
     * @see \GraphQL\Type\Definition\InputObjectField
34
     *
35
     * @return array<string, mixed> The configuration to construct an \GraphQL\Type\Definition\InputObjectField|\GraphQL\Type\Definition\FieldArgument
36
     */
37 1
    public function convert(InputValueDefinitionNode $definitionNode): array
38
    {
39
        /** @var \Nuwave\Lighthouse\Schema\ExecutableTypeNodeConverter $definitionNodeConverter */
40 1
        $definitionNodeConverter = app(ExecutableTypeNodeConverter::class);
41
        /** @var \GraphQL\Type\Definition\Type&\GraphQL\Type\Definition\InputType $type */
42 1
        $type = $definitionNodeConverter->convert($definitionNode->type);
43

44
        $config = [
45 1
            'name' => $definitionNode->name->value,
46 1
            'description' => data_get($definitionNode->description, 'value'),
47 1
            'type' => $type,
48 1
            'astNode' => $definitionNode,
49
        ];
50

51 1
        if ($defaultValue = $definitionNode->defaultValue) {
52
            $config += [
53 1
                'defaultValue' => ASTHelper::defaultValueForArgument($defaultValue, $type),
54
            ];
55
        }
56

57 1
        return $config;
58
    }
59
}

Read our documentation on viewing source code .

Loading