Add types parameter to BuildSchema - #1872
Conversation
…nstances from SDL Enables users to supply custom type instances (e.g., scalars with serialize/parseValue, enums with PHP values) when building a schema from SDL, without having to use the more awkward typeConfigDecorator callback pattern. Types whose names match SDL definitions replace the SDL-built instances. Types whose names are absent from the SDL are registered as extra types. Closes #681
There was a problem hiding this comment.
Pull request overview
Adds a types parameter to the SDL schema build path (BuildSchema) so callers can inject pre-built type instances (e.g., custom scalars/enums) in the same way Schema/SchemaConfig already support via types.
Changes:
- Extend
BuildSchema::build()/BuildSchema::buildAST()(and constructor) with an optionaliterable $types = []parameter. - Add override support in
ASTDefinitionBuilderso provided named types can replace SDL-defined types by name (without mutating user instances). - Ensure “extra” provided types (not present in SDL) are still discoverable via
Schema::getType()/getTypeMap(), and add coverage + docs/changelog updates.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/Utils/BuildSchema.php |
Plumbs a new types parameter through SDL build, wires override/extra type behavior into SchemaConfig’s type loader and types list. |
src/Utils/ASTDefinitionBuilder.php |
Adds a typeOverrides map and short-circuits type building to return provided instances for matching SDL names. |
tests/Utils/BuildSchemaTest.php |
Adds a test validating SDL-defined type overrides (scalar/enum) and registration of extra types, including end-to-end scalar serialization. |
docs/class-reference.md |
Documents the new types parameter for BuildSchema::build() and BuildSchema::buildAST(). |
CHANGELOG.md |
Notes the new capability in the Unreleased “Added” section. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
# Conflicts: # CHANGELOG.md
types parameter to BuildSchema| /** @var array<string, Type&NamedType> $typeOverrides */ | ||
| $typeOverrides = []; | ||
| /** @var array<string, Type&NamedType> $extraTypesMap */ | ||
| $extraTypesMap = []; | ||
| foreach ($this->types as $type) { | ||
| if (isset($typeDefinitionsMap[$type->name])) { | ||
| $typeOverrides[$type->name] = $type; | ||
| } else { | ||
| $extraTypesMap[$type->name] = $type; | ||
| } | ||
| } |
| foreach ($this->types as $type) { | ||
| if (isset($typeDefinitionsMap[$type->name])) { | ||
| $typeOverrides[$type->name] = $type; | ||
| } else { | ||
| $extraTypesMap[$type->name] = $type; | ||
| } | ||
| } |
| if (isset($this->typeOverrides[$typeName])) { | ||
| return $this->cache[$typeName] = $this->typeOverrides[$typeName]; | ||
| } |
| $schema = BuildSchema::build( | ||
| file_get_contents('schema.graphql'), | ||
| types: [$dateType], | ||
| ); |
# Conflicts: # CHANGELOG.md
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/Utils/BuildSchema.php:223
- The new
$typesparameter is consumed without any validation. If a caller passes a non-Type/non-NamedTypevalue, this will fatally error at$type->name. Also, allowing built-in scalar or introspection type names here is misleading:Schema::loadType()explicitly skips the typeLoader for built-in scalar names (Schema.php:351-353), andSchema::getType()resolves introspection types before the loader, so injected instances for those reserved names will never be used. Finally, duplicate names in$typesare silently overwritten by the maps here, which can hide mistakes.
Consider validating entries (instance/type name), rejecting reserved names, and throwing on duplicates to make the API safer and the behavior predictable.
foreach ($this->types as $type) {
if (isset($typeDefinitionsMap[$type->name])) {
$typeOverrides[$type->name] = $type;
} else {
$extraTypesMap[$type->name] = $type;
# Conflicts: # CHANGELOG.md
# Conflicts: # CHANGELOG.md
Allow injecting pre-built type instances when building a schema from SDL.
Enables users to supply custom type instances (e.g., scalars with serialize/parseValue, enums with PHP values) when building a schema from SDL, without having to use the more awkward typeConfigDecorator callback pattern.
Types whose names match SDL definitions replace the SDL-built instances.
Types whose names are absent from the SDL are registered as extra types.
Closes #681