Fix schema built from SDL to return null for unknown types #1068
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #997 (and unblocks #998 and #999).
History background
The implementation of
BuildSchema
,SchemaExtender
andASTDefinitionBuilder
roughly matches their counterparts fromgraphql-js
v14.0.However, during the lifetime of v14, the JS code underwent significant changes. In the final version 14.7,
buildASTSchema()
,extendSchema()
andASTDefinitionBuilder
looks much different then they did in v14.0. For the context of this PR, the most important changes (graphql/graphql-js@afc6b7c) were thatASTDefinitionBuilder
's constructor didn't accepttypeDefinitionsMap
param any more, and that the original single methodbuildType()
was replaced with a newbuildType()
andgetNamedType()
. Each was used in a different context, and only the latter could throw the"Unknown type"
error.These changes to the reference implementation have never been reflected in
graphql-php
.Implementation notes
The context is everything. Sometimes it's okay for the builder to return
null
instead of throwing"Unknown type"
error – like in this case when it's requested to build a type that isn't defined in the SDL schema.To handle this explicitly, I added the new
maybeBuildType(string $name): ?Type
method to the builder.graphql-js
(v14.2+) buildstypeMap
with all types from SDL ahead. Then callingtypeMap[typeName]
returns either a type orundefined
.The new
maybeBuildType()
provides similar functionality for the lazy environment where we don't have such a map and where the individual types are built from the SDL schema on demand.Additional notes
In v15,
graphql-js
has undergone even more significant changes andASTDefinitionBuilder
has been removed completely (graphql/graphql-js@3948aa2).To ensure future compatibility with the reference implementation, it would be worth considering reimplementing schema builder and extender completely.