Skip to content

[Support] How to get AST from extended Schema #1465

@Kingdutch

Description

@Kingdutch

The library recommends storing the parsed AST on disk as a loadable PHP file that can be loaded and passed to BuildSchema::build which generates a lazy-loaded schema. However this causes challenges when combined with a set-up that makes use of extend type.

Below are the two non-ideal methods that I know of, one doesn't work and another has a transformation that feels unnecessary. What is the correct way to do AST::toArray($schema) to allow restoring a merged schema later with AST::fromArray?

Storing the concatenaed parsed AST
One option which seems sensible is to parse all individual documents and use AST::concatAST over the array. This results in a merged AST that could be passed to BuildSchema::buildAst. The problem is that BuildSchema::buildSchema does not handle extend type nodes, so this results in a schema that only has the base fields.

Using SchemaExtender::extend
This is the preferred method to apply these kinds of extensions. Fetching the AST that contains the extension separately is easy enough and it's even possible to cache this to disk with AST::toArray. However the method itself will load all types in the schema that's being extended which is a very slow operation, so this makes it unusable in production.

The goal is then to get a resulting AST from the merged schema. The only way that I'm aware of to get an AST from a schema and write it to disk is to run AST::toArray(Parser::parse(SchemaPrinter::doPrint($schema))). However it feels weird to go through text this way and not being able to get the AST from the schema directly without going through SDL.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions