Skip to content

Add types parameter to BuildSchema - #1872

Open
spawnia wants to merge 9 commits into
masterfrom
681-build-schema-types-parameter
Open

Add types parameter to BuildSchema#1872
spawnia wants to merge 9 commits into
masterfrom
681-build-schema-types-parameter

Conversation

@spawnia

@spawnia spawnia commented Mar 14, 2026

Copy link
Copy Markdown
Collaborator

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

spawnia and others added 2 commits March 14, 2026 21:30
…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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 optional iterable $types = [] parameter.
  • Add override support in ASTDefinitionBuilder so 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.

@spawnia
spawnia marked this pull request as ready for review March 14, 2026 21:08
@spawnia
spawnia requested a review from ruudk March 29, 2026 18:33
@spawnia spawnia changed the title Add types parameter to BuildSchema Add types parameter to BuildSchema Jul 20, 2026
@spawnia
spawnia requested a review from Copilot July 21, 2026 13:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Comment thread src/Utils/BuildSchema.php
Comment on lines +215 to +225
/** @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;
}
}

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Comment thread src/Utils/BuildSchema.php
Comment on lines +219 to +225
foreach ($this->types as $type) {
if (isset($typeDefinitionsMap[$type->name])) {
$typeOverrides[$type->name] = $type;
} else {
$extraTypesMap[$type->name] = $type;
}
}
Comment on lines +273 to +275
if (isset($this->typeOverrides[$typeName])) {
return $this->cache[$typeName] = $this->typeOverrides[$typeName];
}
Comment on lines +73 to +76
$schema = BuildSchema::build(
file_get_contents('schema.graphql'),
types: [$dateType],
);

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 $types parameter is consumed without any validation. If a caller passes a non-Type/non-NamedType value, 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), and Schema::getType() resolves introspection types before the loader, so injected instances for those reserved names will never be used. Finally, duplicate names in $types are 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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to register ScalarTypes?

2 participants