Skip to content

Commit ad32c7b

Browse files
committed
add cache to GraphQLObjectType and introspection's schema part builder
1 parent fa9faab commit ad32c7b

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/Introspection/Introspection.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@
2222
*/
2323
class Introspection
2424
{
25+
static $introspectionSchemaPartsCache;
26+
2527
/**
2628
* @return array
2729
* @throws BadImplementationError
2830
*/
2931
static public function buildIntrospectionSchemaParts(): array
3032
{
33+
if (self::$introspectionSchemaPartsCache !== null)
34+
return self::$introspectionSchemaPartsCache;
35+
3136
$__Type = null;
3237
$__Field = null;
3338
$__TypeKind = null;
@@ -523,12 +528,14 @@ function ($_, $__, $___, $info) {
523528
&$__TypeKind
524529
];
525530

526-
return [
531+
self::$introspectionSchemaPartsCache = [
527532
"__Schema" => $__Schema,
528533
"SchemaMetaFieldDef" => $SchemaMetaFieldDef,
529534
"TypeMetaFieldDef" => $TypeMetaFieldDef,
530535
"TypeNameMetaFieldDef" => $TypeNameMetaFieldDef,
531536
];
537+
538+
return self::$introspectionSchemaPartsCache;
532539
}
533540

534541
/**
@@ -641,11 +648,3 @@ enumValues(includeDeprecated: true) {
641648
';
642649
}
643650
}
644-
645-
646-
/*function isIntrospectionType(string $name){
647-
global $introspectionTypes;
648-
return array_reduce($introspectionTypes, function($carry, GraphQLObjectType $type) use($name){
649-
return $carry || $type->getName()===$name;
650-
}, false);
651-
}*/

src/Types/GraphQLObjectType.php

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class GraphQLObjectType extends GraphQLType
1515
protected $description;
1616

1717
private $fields;
18+
private $fieldsCache;
1819
private $interfaces;
1920
private $isTypeOfFn;
2021

@@ -78,11 +79,17 @@ public function getInterfaces(): array
7879
*/
7980
public function getFields(): array
8081
{
82+
if ($this->fieldsCache !== null)
83+
return $this->fieldsCache;
84+
8185
$allFields = call_user_func($this->fields);
8286
$fields = [];
8387
foreach ($allFields as $field) {
8488
$fields[$field->getName()] = $field;
8589
}
90+
91+
$this->fieldsCache = $fields;
92+
8693
return $fields;
8794
}
8895

0 commit comments

Comments
 (0)