Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple critical performance improvments. #1314

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions src/Plugin/GraphQL/Schema/SdlSchemaPluginBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use GraphQL\Language\Parser;
use GraphQL\Utils\BuildSchema;
use GraphQL\Utils\SchemaExtender;
use GraphQL\Utils\SchemaPrinter;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand Down Expand Up @@ -119,13 +120,14 @@ public function getSchema(ResolverRegistryInterface $registry) {
$extensions = $this->getExtensions();
$resolver = [$registry, 'resolveType'];
$document = $this->getSchemaDocument($extensions);
$options = ['assumeValid' => TRUE];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should only do this in prod, so we should check empty($this->inDevelopment)

$schema = BuildSchema::build($document, function ($config, TypeDefinitionNode $type) use ($resolver) {
if ($type instanceof InterfaceTypeDefinitionNode || $type instanceof UnionTypeDefinitionNode) {
$config['resolveType'] = $resolver;
}

return $config;
});
}, $options);

if (empty($extensions)) {
return $schema;
Expand All @@ -136,7 +138,20 @@ public function getSchema(ResolverRegistryInterface $registry) {
}

if ($extendSchema = $this->getExtensionDocument($extensions)) {
return SchemaExtender::extend($schema, $extendSchema);
// Generate the AST from the extended schema and
// save it to the cache. This is important, because the Drupal
// graphql module is not caching the extended schema.
// During schema extension, a very expensive function
// \GraphQL\Type\Schema::getTypeMap() is called.
$document = $this->getExtensionSchemaAst($schema, $extendSchema);
$options = ['assumeValid' => TRUE];
$extended_schema = BuildSchema::build($document, function ($config, TypeDefinitionNode $type) use ($resolver) {
if ($type instanceof InterfaceTypeDefinitionNode || $type instanceof UnionTypeDefinitionNode) {
$config['resolveType'] = $resolver;
}
return $config;
}, $options);
return $extended_schema;
}

return $schema;
Expand Down Expand Up @@ -242,4 +257,34 @@ protected function getSchemaDefinition() {
return file_get_contents($file) ?: NULL;
}

/**
* Retrieves the extension schema AST from cache if possible.
*
* @param mixed $schema
* The schema.
* @param mixed $extendSchema
* The extended schema.
*
* @return \GraphQL\Language\AST\DocumentNode
*
* @throws \GraphQL\Error\Error
* @throws \GraphQL\Error\SyntaxError
*/
public function getExtensionSchemaAst($schema, $extendSchema) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is confusing now, we already cache an AST in getExtensionDocument(). here we cache something again, why?

$cid = "schema_extension:{$this->getPluginId()}";
if (empty($this->inDevelopment) && $cache = $this->astCache->get($cid)) {
return $cache->data;
}

$schema = SchemaExtender::extend($schema, $extendSchema);
$schema_string = SchemaPrinter::doPrint($schema);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is clever, I like the idea! We construct a final AST with all extensions and then cache that 👍

$options = ['noLocation' => TRUE];
$ast = Parser::parse($schema_string, $options);
if (empty($this->inDevelopment)) {
$this->astCache->set($cid, $ast, CacheBackendInterface::CACHE_PERMANENT, ['graphql']);
}

return $ast;
}

}