|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DeInternetJongens\LighthouseUtils\Generators\Arguments; |
|
4
|
|
|
|
|
5
|
|
|
use DeInternetJongens\LighthouseUtils\Schema\Scalars\Date; |
|
6
|
|
|
use DeInternetJongens\LighthouseUtils\Schema\Scalars\DateTimeTz; |
|
7
|
|
|
use DeInternetJongens\LighthouseUtils\Schema\Scalars\Email; |
|
8
|
|
|
use DeInternetJongens\LighthouseUtils\Schema\Scalars\PostalCodeNl; |
|
9
|
|
|
use GraphQL\Type\Definition\BooleanType; |
|
10
|
|
|
use GraphQL\Type\Definition\EnumType; |
|
11
|
|
|
use GraphQL\Type\Definition\FloatType; |
|
12
|
|
|
use GraphQL\Type\Definition\IntType; |
|
13
|
|
|
use GraphQL\Type\Definition\StringType; |
|
14
|
|
|
use GraphQL\Type\Definition\Type; |
|
15
|
|
|
use Nuwave\Lighthouse\Schema\Types\Scalars\DateTime; |
|
16
|
|
|
|
|
17
|
|
|
class InputFieldsArgumentGenerator |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var array */ |
|
20
|
|
|
private static $supportedGraphQLTypes = [ |
|
21
|
|
|
StringType::class, |
|
22
|
|
|
IntType::class, |
|
23
|
|
|
FloatType::class, |
|
24
|
|
|
Date::class, |
|
25
|
|
|
DateTime::class, |
|
26
|
|
|
DateTimeTz::class, |
|
27
|
|
|
PostalCodeNl::class, |
|
28
|
|
|
EnumType::class, |
|
29
|
|
|
Email::class, |
|
30
|
|
|
BooleanType::class, |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
/** @var array */ |
|
34
|
|
|
private static $ignoredColumns = [ |
|
35
|
|
|
'created_at', |
|
36
|
|
|
'updated_at', |
|
37
|
|
|
'deleted_at', |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Generates a GraphQL Arguments for a mutation |
|
42
|
|
|
* More information: |
|
43
|
|
|
* https://lighthouse-php.netlify.com/docs/schema.html#input-types |
|
44
|
|
|
* |
|
45
|
|
|
* @param Type[] $typeFields |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function generate(array $typeFields): array |
|
49
|
|
|
{ |
|
50
|
|
|
$arguments = []; |
|
51
|
|
|
foreach ($typeFields as $fieldName => $field) { |
|
52
|
|
|
$className = get_class($field); |
|
53
|
|
|
if (! in_array($className, self::$supportedGraphQLTypes) || in_array($fieldName, self::$ignoredColumns)) { |
|
54
|
|
|
continue; |
|
55
|
|
|
}; |
|
56
|
|
|
|
|
57
|
|
|
$required = isset($field->config['generator-required']) && $field->config['generator-required'] === true ? '!' : ''; |
|
58
|
|
|
$arguments[] = sprintf('%s: %s%s', $fieldName, $field->name, $required); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $arguments; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|