Skip to content

Recommend using assumeValidSDL when caching a parsed schema? #1708

@zimzat

Description

@zimzat

Should the documentation for caching the parsed schema also recommend passing assumeValidSDL to avoid re-validating the schema on every request?

We do some manual operations to merge multiple .graphql files and modifications based on schema-level directives (authorization control based on session type) so we wouldn't want to opt out entirely. Instead we moved to manually call DocumentValidator::assertValidSDL as part of the build process and then pass assumeValidSDL to avoid it on every request. We saw a hefty reduction in resource usage after doing so.

$cacheFilename = 'cached_schema.php';

if (!file_exists($cacheFilename)) {
    $document = Parser::parse(file_get_contents('./schema.graphql'));
    DocumentValidator::assertValidSDL($document);
    file_put_contents($cacheFilename, "<?php\nreturn " . var_export(AST::toArray($document), true) . ";\n");
} else {
    $document = AST::fromArray(require $cacheFilename); // fromArray() is a lazy operation as well
}

$typeConfigDecorator = function () {};
$schema = BuildSchema::build($document, $typeConfigDecorator, ['assumeValidSDL' => true]);

Do you see any reason why this could be a problem, or could this be made the default recommendation in the documentation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions