| 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 function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->setDescription('Creates a mutation config.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             ->setHelp('This command allows you to create a mutation config based in the result of a ' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 'introspection query for a specific mutation.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             ->addArgument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 'instrospection-result', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 InputArgument::REQUIRED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 'Json file with the introspection query result' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             ->addArgument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 'mutation', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 InputArgument::REQUIRED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 'Mutation name to extract to the configuration' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if (!$this->checkArguments($input, $output)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $jsonSchema = file_get_contents($input->getArgument('instrospection-result')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $mutation   = $input->getArgument('mutation'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $mutationConfig = $this->generateConfig(json_decode($jsonSchema), $mutation); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $output->writeln(var_export($mutationConfig, true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     private function checkArguments(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $jsonPath = $input->getArgument('instrospection-result'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if (!file_exists($jsonPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $output->writeln("The file '{$jsonPath}' does not exist"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     private function generateConfig(StdClass $jsonSchema, string $mutation) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         foreach ($jsonSchema->data->__schema->types as $type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             if ($type->name === 'Mutation' && $type->fields[0]->name === $mutation) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $initialMutationField = $type->fields[0]->args[0]->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 $inputType            = $type->fields[0]->args[0]->type->ofType->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $mutation => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 $initialMutationField => [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     'linksTo'  => '.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                     'type'     => Item::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                     'children' => $this->getMutationConfig($jsonSchema, $inputType), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     private function getTypeFromField($field) | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         $isCollection = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         $type         = $field->type; | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         while ($type->kind !== self::SCALAR && $type->kind !== self::INPUT_OBJECT) { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             if ($type->kind === self::LIST) { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |                 $isCollection = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             $type = $type->ofType; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         if ($type->kind === self::SCALAR) { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |                 'type'         => $type->kind, | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |                 'isCollection' => $isCollection, | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             'type'         => $type->name, | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |             'isCollection' => $isCollection, | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     private function getMutationConfig(stdClass $jsonSchema, $inputType, $parentLinksTo = ''): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $children = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         foreach ($jsonSchema->data->__schema->types as $type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             if ($type->name === $inputType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 foreach ($type->inputFields as $inputField) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                         'type'         => $type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                         'isCollection' => $isCollection, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                     ] = $this->getTypeFromField($inputField); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     if ($type === self::SCALAR) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                         $children[$inputField->name] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                             'type' => MutationTypeConfig::SCALAR_DATA_TYPE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                         continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                     $linksTo                     = "{$parentLinksTo}.{$inputField->name}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                     $children[$inputField->name] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                         'linksTo'  => $linksTo, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                         'type'     => $isCollection ? Collection::class : Item::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                         'children' => $this->getMutationConfig($jsonSchema, $type, $linksTo), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         return $children; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |  | 
            
                        
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.