11<?php
22namespace GraphQL \Type \Definition ;
33
4+ use GraphQL \Utils ;
5+
46class InputObjectType extends Type implements InputType
57{
68 /**
79 * @var array<InputObjectField>
810 */
9- private $ _fields = [];
11+ private $ _fields ;
12+
13+ public $ config ;
1014
1115 public function __construct (array $ config )
1216 {
@@ -21,12 +25,7 @@ public function __construct(array $config)
2125 'description ' => Config::STRING
2226 ]);
2327
24- if (!empty ($ config ['fields ' ])) {
25- foreach ($ config ['fields ' ] as $ name => $ field ) {
26- $ this ->_fields [$ name ] = new InputObjectField ($ field + ['name ' => $ name ]);
27- }
28- }
29-
28+ $ this ->config = $ config ;
3029 $ this ->name = $ config ['name ' ];
3130 $ this ->description = isset ($ config ['description ' ]) ? $ config ['description ' ] : null ;
3231 }
@@ -36,6 +35,29 @@ public function __construct(array $config)
3635 */
3736 public function getFields ()
3837 {
38+ if (null === $ this ->_fields ) {
39+ $ this ->_fields = [];
40+ $ fields = isset ($ this ->config ['fields ' ]) ? $ this ->config ['fields ' ] : [];
41+ $ fields = is_callable ($ fields ) ? call_user_func ($ fields ) : $ fields ;
42+ foreach ($ fields as $ name => $ field ) {
43+ $ this ->_fields [$ name ] = new InputObjectField ($ field + ['name ' => $ name ]);
44+ }
45+ }
46+
3947 return $ this ->_fields ;
4048 }
49+
50+ /**
51+ * @param string $name
52+ * @return InputObjectField
53+ * @throws \Exception
54+ */
55+ public function getField ($ name )
56+ {
57+ if (null === $ this ->_fields ) {
58+ $ this ->getFields ();
59+ }
60+ Utils::invariant (isset ($ this ->_fields [$ name ]), "Field '%s' is not defined for type '%s' " , $ name , $ this ->name );
61+ return $ this ->_fields [$ name ];
62+ }
4163}
0 commit comments