RelationArgumentGenerator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 1
b 0
f 0
dl 0
loc 26
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 17 5
1
<?php
2
3
namespace DeInternetJongens\LighthouseUtils\Generators\Arguments;
4
5
use GraphQL\Type\Definition\ObjectType;
6
use GraphQL\Type\Definition\Type;
7
8
class RelationArgumentGenerator
9
{
10
    /**
11
     * Generates a GraphQL mutation argument for a relation field
12
     *
13
     * @param Type[] $typeFields
14
     * @param bool $required Should the relationship fields be required?
15
     * @return array
16
     */
17
    public static function generate(array $typeFields): array
18
    {
19
        $arguments = [];
20
21
        foreach ($typeFields as $fieldName => $field) {
22
            $config = $field->config;
23
24
            $required =  isset($config['generator-required']) ? ($config['generator-required'] ? '!' : '') : '';
25
            $className = get_class($field);
26
            if ($className !== ObjectType::class) {
27
                continue;
28
            }
29
30
            $arguments[] = sprintf('%s_id: ID%s', $fieldName, $required);
31
        }
32
33
        return $arguments;
34
    }
35
}
36