1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Softonic\GraphQL\Console\Mutation; |
4
|
|
|
|
5
|
|
|
use Softonic\GraphQL\Config\MutationTypeConfig; |
6
|
|
|
use Softonic\GraphQL\DataObjects\Mutation\Collection; |
7
|
|
|
use Softonic\GraphQL\DataObjects\Mutation\Item; |
8
|
|
|
use stdClass; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
class GenerateConfig extends Command |
15
|
|
|
{ |
16
|
|
|
const SCALAR = 'SCALAR'; |
17
|
|
|
|
18
|
|
|
const INPUT_OBJECT = 'INPUT_OBJECT'; |
19
|
|
|
|
20
|
|
|
const LIST = 'LIST'; |
21
|
|
|
|
22
|
|
|
protected static $defaultName = 'mutation:generate-config'; |
23
|
|
|
|
24
|
|
|
protected $generatedFieldTypes = []; |
25
|
|
|
|
26
|
|
|
protected function configure() |
27
|
|
|
{ |
28
|
|
|
$this->setDescription('Creates a mutation config.') |
29
|
|
|
->setHelp('This command allows you to create a mutation config based in the result of a ' . |
30
|
|
|
'introspection query for a specific mutation.'); |
31
|
|
|
|
32
|
|
|
$this |
33
|
|
|
->addArgument( |
34
|
|
|
'instrospection-result', |
35
|
|
|
InputArgument::REQUIRED, |
36
|
|
|
'Json file with the introspection query result' |
37
|
|
|
) |
38
|
|
|
->addArgument( |
39
|
|
|
'mutation', |
40
|
|
|
InputArgument::REQUIRED, |
41
|
|
|
'Mutation name to extract to the configuration' |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
46
|
|
|
{ |
47
|
|
|
if (!$this->checkArguments($input, $output)) { |
48
|
|
|
return 1; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$jsonSchema = file_get_contents($input->getArgument('instrospection-result')); |
52
|
|
|
$mutation = $input->getArgument('mutation'); |
53
|
|
|
|
54
|
|
|
$mutationConfig = $this->generateConfig(json_decode($jsonSchema), $mutation); |
|
|
|
|
55
|
|
|
|
56
|
|
|
$output->writeln(var_export($mutationConfig, true)); |
57
|
|
|
|
58
|
|
|
return 0; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function checkArguments(InputInterface $input, OutputInterface $output) |
62
|
|
|
{ |
63
|
|
|
$jsonPath = $input->getArgument('instrospection-result'); |
64
|
|
|
|
65
|
|
|
if (!file_exists($jsonPath)) { |
66
|
|
|
$output->writeln("The file '{$jsonPath}' does not exist"); |
67
|
|
|
|
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function generateConfig(StdClass $jsonSchema, string $mutation) |
75
|
|
|
{ |
76
|
|
|
foreach ($jsonSchema->data->__schema->types as $type) { |
77
|
|
|
if ($type->name === 'Mutation' && $type->fields[0]->name === $mutation) { |
78
|
|
|
$initialMutationField = $type->fields[0]->args[0]->name; |
79
|
|
|
$inputType = $type->fields[0]->args[0]->type->ofType->name; |
80
|
|
|
break; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return [ |
85
|
|
|
$mutation => [ |
86
|
|
|
$initialMutationField => [ |
|
|
|
|
87
|
|
|
'linksTo' => '.', |
88
|
|
|
'type' => Item::class, |
89
|
|
|
'children' => $this->getMutationConfig($jsonSchema, $inputType), |
|
|
|
|
90
|
|
|
], |
91
|
|
|
], |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function getTypeFromField($field) |
96
|
|
|
{ |
97
|
|
|
$isCollection = false; |
98
|
|
|
$type = $field->type; |
99
|
|
|
|
100
|
|
|
while ($type->kind !== self::SCALAR && $type->kind !== self::INPUT_OBJECT) { |
101
|
|
|
if ($type->kind === self::LIST) { |
102
|
|
|
$isCollection = true; |
103
|
|
|
} |
104
|
|
|
$type = $type->ofType; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($type->kind === self::SCALAR) { |
108
|
|
|
return [ |
109
|
|
|
'type' => $type->kind, |
110
|
|
|
'isCollection' => $isCollection, |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return [ |
115
|
|
|
'type' => $type->name, |
116
|
|
|
'isCollection' => $isCollection, |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function getMutationConfig(stdClass $jsonSchema, $inputType, $parentLinksTo = ''): array |
121
|
|
|
{ |
122
|
|
|
$children = []; |
123
|
|
|
foreach ($jsonSchema->data->__schema->types as $type) { |
124
|
|
|
if ($type->name === $inputType) { |
125
|
|
|
foreach ($type->inputFields as $inputField) { |
126
|
|
|
[ |
127
|
|
|
'type' => $type, |
128
|
|
|
'isCollection' => $isCollection, |
|
|
|
|
129
|
|
|
] = $this->getTypeFromField($inputField); |
130
|
|
|
|
131
|
|
|
if ($type === self::SCALAR) { |
132
|
|
|
$children[$inputField->name] = [ |
133
|
|
|
'type' => MutationTypeConfig::SCALAR_DATA_TYPE, |
134
|
|
|
]; |
135
|
|
|
continue; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (!in_array($inputType, $this->generatedFieldTypes[$inputField->name] ?? [])) { |
139
|
|
|
$this->generatedFieldTypes[$inputField->name][] = $inputType; |
140
|
|
|
$linksTo = "{$parentLinksTo}.{$inputField->name}"; |
141
|
|
|
$children[$inputField->name] = [ |
142
|
|
|
'linksTo' => $linksTo, |
143
|
|
|
'type' => $isCollection ? Collection::class : Item::class, |
144
|
|
|
'children' => $this->getMutationConfig($jsonSchema, $type, $linksTo), |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
break; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $children; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.