1414use GraphQL \Language \AST \ScalarTypeDefinitionNode ;
1515use GraphQL \Language \AST \UnionTypeDefinitionNode ;
1616use GraphQL \Language \Parser ;
17+ use Overblog \GraphQLBundle \Config \Parser \GraphQL \ASTConverter \CustomScalarNode ;
18+ use Overblog \GraphQLBundle \Config \Parser \GraphQL \ASTConverter \EnumNode ;
19+ use Overblog \GraphQLBundle \Config \Parser \GraphQL \ASTConverter \InputObjectNode ;
20+ use Overblog \GraphQLBundle \Config \Parser \GraphQL \ASTConverter \InterfaceNode ;
1721use Overblog \GraphQLBundle \Config \Parser \GraphQL \ASTConverter \NodeInterface ;
22+ use Overblog \GraphQLBundle \Config \Parser \GraphQL \ASTConverter \ObjectNode ;
23+ use Overblog \GraphQLBundle \Config \Parser \GraphQL \ASTConverter \UnionNode ;
1824use SplFileInfo ;
1925use Symfony \Component \Config \Resource \FileResource ;
2026use Symfony \Component \DependencyInjection \ContainerBuilder ;
2127use Symfony \Component \DependencyInjection \Exception \InvalidArgumentException ;
28+
2229use function array_pop ;
23- use function call_user_func ;
2430use function explode ;
2531use function file_get_contents ;
26- use function get_class ;
2732use function preg_replace ;
2833use function sprintf ;
2934use function trim ;
30- use function ucfirst ;
3135
3236class GraphQLParser implements ParserInterface
3337{
34- private const DEFINITION_TYPE_MAPPING = [
35- NodeKind::OBJECT_TYPE_DEFINITION => ' object ' ,
36- NodeKind::INTERFACE_TYPE_DEFINITION => ' interface ' ,
37- NodeKind::ENUM_TYPE_DEFINITION => ' enum ' ,
38- NodeKind::UNION_TYPE_DEFINITION => ' union ' ,
39- NodeKind::INPUT_OBJECT_TYPE_DEFINITION => ' inputObject ' ,
40- NodeKind::SCALAR_TYPE_DEFINITION => ' customScalar ' ,
38+ protected const DEFINITION_TYPE_MAPPING = [
39+ NodeKind::OBJECT_TYPE_DEFINITION => ObjectNode::class ,
40+ NodeKind::INTERFACE_TYPE_DEFINITION => InterfaceNode::class ,
41+ NodeKind::ENUM_TYPE_DEFINITION => EnumNode::class ,
42+ NodeKind::UNION_TYPE_DEFINITION => UnionNode::class ,
43+ NodeKind::INPUT_OBJECT_TYPE_DEFINITION => InputObjectNode::class ,
44+ NodeKind::SCALAR_TYPE_DEFINITION => CustomScalarNode::class ,
4145 ];
4246
4347 public static function parse (SplFileInfo $ file , ContainerBuilder $ container , array $ configs = []): array
@@ -67,19 +71,28 @@ public static function parse(SplFileInfo $file, ContainerBuilder $container, arr
6771 return $ typesConfig ;
6872 }
6973
74+ protected static function createUnsupportedDefinitionNodeException (DefinitionNode $ typeDef ): InvalidArgumentException
75+ {
76+ $ path = explode ('\\' , \get_class ($ typeDef ));
77+
78+ return new InvalidArgumentException (
79+ sprintf (
80+ '%s definition is not supported right now. ' ,
81+ preg_replace ('@DefinitionNode$@ ' , '' , array_pop ($ path ))
82+ )
83+ );
84+ }
85+
7086 /**
7187 * @return class-string<NodeInterface>
7288 */
7389 protected static function getNodeClass (DefinitionNode $ typeDef ): string
7490 {
75- if (isset ($ typeDef ->kind ) && array_key_exists ($ typeDef ->kind , self ::DEFINITION_TYPE_MAPPING )) {
76- /**
77- * @var class-string<NodeInterface> $class
78- */
79- return sprintf ('\\%s \\GraphQL \\ASTConverter \\%sNode ' , __NAMESPACE__ , ucfirst (self ::DEFINITION_TYPE_MAPPING [$ typeDef ->kind ]));
91+ if (isset ($ typeDef ->kind ) && \array_key_exists ($ typeDef ->kind , static ::DEFINITION_TYPE_MAPPING )) {
92+ return static ::DEFINITION_TYPE_MAPPING [$ typeDef ->kind ];
8093 }
8194
82- self :: throwUnsupportedDefinitionNode ($ typeDef );
95+ throw static :: createUnsupportedDefinitionNodeException ($ typeDef );
8396 }
8497
8598 /**
@@ -89,17 +102,6 @@ protected static function prepareConfig(DefinitionNode $typeDef): array
89102 {
90103 $ nodeClass = static ::getNodeClass ($ typeDef );
91104
92- return call_user_func ([$ nodeClass , 'toConfig ' ], $ typeDef );
93- }
94-
95- private static function throwUnsupportedDefinitionNode (DefinitionNode $ typeDef ): void
96- {
97- $ path = explode ('\\' , get_class ($ typeDef ));
98- throw new InvalidArgumentException (
99- sprintf (
100- '%s definition is not supported right now. ' ,
101- preg_replace ('@DefinitionNode$@ ' , '' , array_pop ($ path ))
102- )
103- );
105+ return \call_user_func ([$ nodeClass , 'toConfig ' ], $ typeDef );
104106 }
105107}
0 commit comments