Skip to content

Schema::getType() shoud always return null for non-existent type #997

@vhenzl

Description

@vhenzl

There is an inconsistency in how Schema::getType() behaves depending on how the schema is created.

If the schema is created using type definitions, it correctly returns null for a type that doesn't exist in the schema:

$schema = new Schema([
    'types' => [
        new ObjectType([
            'name' => 'Foo',
            'fields' => ['id' => Type::id()],
        ]),
    ],
]);

$foo = $schema->getType('Foo');
$bar = $schema->getType('Bar'); // ✅ returns null as expected

If the schema is built from SDL, it throws Unknown type: "Bar". error instead or returning null:

$schema = BuildSchema::build('type Foo { id: ID }');

$foo = $schema->getType('Foo');
$bar = $schema->getType('Bar'); // 💥 throws error

This has a negative impact for example on SDL validation rules.

For reference, graphql-js always returns undefined for a non-existent type, regardless of how the schema was created.

I'm trying to wrap my head around how the lazy type loading works (it seems related, at least partially). Any suggestions on how to fix this without a significant rewrite of BuildSchema, SchemaExtender and ASTDefinitionBuilder are welcome. (However, I'm hitting various problems with schema building & extending from different directions, so I'm more and more convinced that rewriting them in accordance with the reference implementation is actually the only reasonable solution.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions