|
20 | 20 | use GraphQL\Type\Definition\NonNull; |
21 | 21 | use GraphQL\Type\Definition\ObjectType; |
22 | 22 | use GraphQL\Type\Definition\Type; |
| 23 | +use GraphQL\Type\Definition\UnionType; |
| 24 | +use GraphQL\Type\Definition\WrappingType; |
23 | 25 | use GraphQL\Type\Introspection; |
24 | 26 | use GraphQL\Utils; |
25 | 27 |
|
@@ -162,6 +164,66 @@ public static function typeFromAST(Schema $schema, $inputTypeNode) |
162 | 164 | return $schema->getType($inputTypeNode->name->value); |
163 | 165 | } |
164 | 166 |
|
| 167 | + /** |
| 168 | + * Given root type scans through all fields to find nested types. Returns array where keys are for type name |
| 169 | + * and value contains corresponding type instance. |
| 170 | + * |
| 171 | + * Example output: |
| 172 | + * [ |
| 173 | + * 'String' => $instanceOfStringType, |
| 174 | + * 'MyType' => $instanceOfMyType, |
| 175 | + * ... |
| 176 | + * ] |
| 177 | + * |
| 178 | + * @param Type $type |
| 179 | + * @param array|null $typeMap |
| 180 | + * @return array |
| 181 | + */ |
| 182 | + public static function extractTypes($type, array $typeMap = null) |
| 183 | + { |
| 184 | + if (!$typeMap) { |
| 185 | + $typeMap = []; |
| 186 | + } |
| 187 | + if (!$type) { |
| 188 | + return $typeMap; |
| 189 | + } |
| 190 | + |
| 191 | + if ($type instanceof WrappingType) { |
| 192 | + return self::extractTypes($type->getWrappedType(true), $typeMap); |
| 193 | + } |
| 194 | + |
| 195 | + if (!empty($typeMap[$type->name])) { |
| 196 | + Utils::invariant( |
| 197 | + $typeMap[$type->name] === $type, |
| 198 | + "Schema must contain unique named types but contains multiple types named \"$type\"." |
| 199 | + ); |
| 200 | + return $typeMap; |
| 201 | + } |
| 202 | + $typeMap[$type->name] = $type; |
| 203 | + |
| 204 | + $nestedTypes = []; |
| 205 | + |
| 206 | + if ($type instanceof UnionType) { |
| 207 | + $nestedTypes = $type->getTypes(); |
| 208 | + } |
| 209 | + if ($type instanceof ObjectType) { |
| 210 | + $nestedTypes = array_merge($nestedTypes, $type->getInterfaces()); |
| 211 | + } |
| 212 | + if ($type instanceof ObjectType || $type instanceof InterfaceType || $type instanceof InputObjectType) { |
| 213 | + foreach ((array) $type->getFields() as $fieldName => $field) { |
| 214 | + if (isset($field->args)) { |
| 215 | + $fieldArgTypes = array_map(function(FieldArgument $arg) { return $arg->getType(); }, $field->args); |
| 216 | + $nestedTypes = array_merge($nestedTypes, $fieldArgTypes); |
| 217 | + } |
| 218 | + $nestedTypes[] = $field->getType(); |
| 219 | + } |
| 220 | + } |
| 221 | + foreach ($nestedTypes as $type) { |
| 222 | + $typeMap = self::extractTypes($type, $typeMap); |
| 223 | + } |
| 224 | + return $typeMap; |
| 225 | + } |
| 226 | + |
165 | 227 | /** |
166 | 228 | * Not exactly the same as the executor's definition of getFieldDef, in this |
167 | 229 | * statically evaluated environment we do not always have an Object type, |
|
0 commit comments