@@ -64,8 +64,6 @@ class ObjectType extends Type implements OutputType, CompositeType
6464 */
6565 private $ _config ;
6666
67- private $ _initialized = false ;
68-
6967 /**
7068 * @var callable
7169 */
@@ -75,36 +73,9 @@ public function __construct(array $config)
7573 {
7674 Utils::invariant (!empty ($ config ['name ' ]), 'Every type is expected to have name ' );
7775
78- $ this ->name = $ config ['name ' ];
79- $ this ->description = isset ($ config ['description ' ]) ? $ config ['description ' ] : null ;
80- $ this ->resolveFieldFn = isset ($ config ['resolveField ' ]) ? $ config ['resolveField ' ] : null ;
81- $ this ->_config = $ config ;
82-
83- if (isset ($ config ['interfaces ' ])) {
84- InterfaceType::addImplementationToInterfaces ($ this );
85- }
86- }
87-
88- /**
89- * Late instance initialization
90- */
91- private function initialize ()
92- {
93- if ($ this ->_initialized ) {
94- return ;
95- }
96- $ config = $ this ->_config ;
97-
98- if (isset ($ config ['fields ' ]) && is_callable ($ config ['fields ' ])) {
99- $ config ['fields ' ] = call_user_func ($ config ['fields ' ]);
100- }
101- if (isset ($ config ['interfaces ' ]) && is_callable ($ config ['interfaces ' ])) {
102- $ config ['interfaces ' ] = call_user_func ($ config ['interfaces ' ]);
103- }
104-
10576 // Note: this validation is disabled by default, because it is resource-consuming
10677 // TODO: add bin/validate script to check if schema is valid during development
107- Config::validate ($ this -> _config , [
78+ Config::validate ($ config , [
10879 'name ' => Config::STRING | Config::REQUIRED ,
10980 'fields ' => Config::arrayOf (
11081 FieldDefinition::getDefinition (),
@@ -118,19 +89,26 @@ private function initialize()
11889 'resolveField ' => Config::CALLBACK
11990 ]);
12091
121- $ this ->_fields = FieldDefinition::createMap ($ config ['fields ' ]);
122- $ this ->_interfaces = isset ($ config ['interfaces ' ]) ? $ config ['interfaces ' ] : [];
92+ $ this ->name = $ config ['name ' ];
93+ $ this ->description = isset ($ config ['description ' ]) ? $ config ['description ' ] : null ;
94+ $ this ->resolveFieldFn = isset ($ config ['resolveField ' ]) ? $ config ['resolveField ' ] : null ;
12395 $ this ->_isTypeOf = isset ($ config ['isTypeOf ' ]) ? $ config ['isTypeOf ' ] : null ;
124- $ this ->_initialized = true ;
96+ $ this ->_config = $ config ;
97+
98+ if (isset ($ config ['interfaces ' ])) {
99+ InterfaceType::addImplementationToInterfaces ($ this );
100+ }
125101 }
126102
127103 /**
128104 * @return FieldDefinition[]
129105 */
130106 public function getFields ()
131107 {
132- if (false === $ this ->_initialized ) {
133- $ this ->initialize ();
108+ if (null === $ this ->_fields ) {
109+ $ fields = isset ($ this ->_config ['fields ' ]) ? $ this ->_config ['fields ' ] : [];
110+ $ fields = is_callable ($ fields ) ? call_user_func ($ fields ) : $ fields ;
111+ $ this ->_fields = FieldDefinition::createMap ($ fields );
134112 }
135113 return $ this ->_fields ;
136114 }
@@ -142,8 +120,8 @@ public function getFields()
142120 */
143121 public function getField ($ name )
144122 {
145- if (false === $ this ->_initialized ) {
146- $ this ->initialize ();
123+ if (null === $ this ->_fields ) {
124+ $ this ->getFields ();
147125 }
148126 Utils::invariant (isset ($ this ->_fields [$ name ]), "Field '%s' is not defined for type '%s' " , $ name , $ this ->name );
149127 return $ this ->_fields [$ name ];
@@ -154,8 +132,10 @@ public function getField($name)
154132 */
155133 public function getInterfaces ()
156134 {
157- if (false === $ this ->_initialized ) {
158- $ this ->initialize ();
135+ if (null === $ this ->_interfaces ) {
136+ $ interfaces = isset ($ this ->_config ['interfaces ' ]) ? $ this ->_config ['interfaces ' ] : [];
137+ $ interfaces = is_callable ($ interfaces ) ? call_user_func ($ interfaces ) : $ interfaces ;
138+ $ this ->_interfaces = $ interfaces ;
159139 }
160140 return $ this ->_interfaces ;
161141 }
0 commit comments