Completed
Push — master ( 3f8b6c...ed0e69 )
by
unknown
06:31
created

ExtendSchemaWithLocaleFields   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extendSchemaWithLocaleFields() 0 12 3
A defineLocaleField() 0 10 1
A defineTranslationIdField() 0 20 1
1
<?php
2
3
namespace BBSLab\NovaTranslation\GraphQL\Directives\Traits;
4
5
use GraphQL\Language\AST\ArgumentNode;
6
use GraphQL\Language\AST\DirectiveNode;
7
use GraphQL\Language\AST\FieldDefinitionNode;
8
use GraphQL\Language\AST\NamedTypeNode;
9
use GraphQL\Language\AST\NameNode;
10
use GraphQL\Language\AST\NodeList;
11
use GraphQL\Language\AST\NonNullTypeNode;
12
use GraphQL\Language\AST\StringValueNode;
13
use Nuwave\Lighthouse\Schema\AST\ASTHelper;
14
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
15
16
trait ExtendSchemaWithLocaleFields
17
{
18
    /**
19
     * Extend GraphQL schema for given type with "locales" fields.
20
     *
21
     * @param  \Nuwave\Lighthouse\Schema\AST\DocumentAST  $documentAST
22
     * @param  string  $typeToExtend
23
     * @return void
24
     */
25
    protected function extendSchemaWithLocaleFields(DocumentAST &$documentAST, string $typeToExtend)
26
    {
27
        foreach ($documentAST->types as &$type) {
28
            if ($type->name->value === $typeToExtend) {
29
                /* @var \GraphQL\Language\AST\ObjectTypeDefinitionNode $type */
30
                $type->fields = ASTHelper::mergeNodeList($type->fields, [
31
                    $this->defineLocaleField(),
32
                    $this->defineTranslationIdField(),
33
                ]);
34
            }
35
        }
36
    }
37
38
    /**
39
     * Setup "locale" field definition.
40
     *
41
     * @return \GraphQL\Language\AST\FieldDefinitionNode
42
     */
43
    protected function defineLocaleField()
44
    {
45
        return new FieldDefinitionNode([
46
            'name' => new NameNode(['value' => 'locale']),
47
            'type' => new NonNullTypeNode(['type' => new NamedTypeNode(['name' => new NameNode(['value' => 'String'])])]),
48
            'arguments' => new NodeList([]),
49
            'directives' => new NodeList([]),
50
            'description' => new StringValueNode(['value' => 'Locale ISO', 'block' => false]),
51
        ]);
52
    }
53
54
    /**
55
     * Setup "translationId" field definition.
56
     *
57
     * @return \GraphQL\Language\AST\FieldDefinitionNode
58
     */
59
    protected function defineTranslationIdField()
60
    {
61
        return new FieldDefinitionNode([
62
            'name' => new NameNode(['value' => 'translationId']),
63
            'type' => new NonNullTypeNode(['type' => new NamedTypeNode(['name' => new NameNode(['value' => 'Int'])])]),
64
            'arguments' => new NodeList([]),
65
            'directives' => new NodeList([
66
                new DirectiveNode([
67
                    'name' => new NameNode(['value' => 'rename']),
68
                    'arguments' => new NodeList([
69
                        new ArgumentNode([
70
                            'name' => new NameNode(['value' => 'attribute']),
71
                            'value' => new StringValueNode(['value' => 'translation_id', 'block' => false]),
72
                        ]),
73
                    ]),
74
                ]),
75
            ]),
76
            'description' => new StringValueNode(['value' => 'Item translation ID', 'block' => false]),
77
        ]);
78
    }
79
}
80