Allow setting per-schema scalar overrides explicitly via SchemaConfig#1927
Allow setting per-schema scalar overrides explicitly via SchemaConfig#1927tmoitie wants to merge 7 commits into
Conversation
When scalar overrides are not set explicitly, they are discovered by scanning the types config. Since the executor resolves built-in scalars through Schema::getType() on essentially every operation, the first such lookup fully resolves a lazily provided types callable - silently making the documented lazy schema pattern (typeLoader + lazy types) eager for every schema, including those that define no overrides at all. Passing scalar overrides explicitly (including an empty array for none) through the new scalarOverrides option skips the scan entirely, keeping lazy type loading lazy. The default behaviour of discovering overrides by scanning types is unchanged. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
8229f5a to
34ef7b5
Compare
|
Hey, tried to update this branch against master but ran into a GitHub quirk: deskpro/graphql-php is an organization-owned fork, and "Allow edits from maintainers" doesn't actually grant push access for org-owned forks (only personal-account ones), even though the API reports it as enabled. I already merged master in and validated locally (fix/stan/test all green), so it's ready - could you or someone with access to the org fork pull master in and push? Happy to share the exact diff if useful. |
|
Hi @spawnia, looks like your merge made it through. I checked on my end as well and I'm getting "Already up to date" |
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds an explicit scalarOverrides option on SchemaConfig/Schema to avoid resolving lazy types during hot-path built-in scalar lookups, addressing the lazy-loading regression introduced by per-schema scalar overrides.
Changes:
- Introduce
SchemaConfig::$scalarOverridesplusgetScalarOverrides()/setScalarOverrides()with validation for built-in scalar names. - Update
Schema::getScalarOverrides()to use explicit overrides when provided, skipping thetypesscan. - Add regression tests and update docs + changelog to document the new option and its lazy-loading implications.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/Type/ScalarOverridesTest.php | Adds coverage for explicit overrides preserving lazy types, precedence in getTypeMap(), and setter validation behavior. |
| src/Type/SchemaConfig.php | Adds scalarOverrides configuration, API accessors, and validation/normalization into a name-keyed map. |
| src/Type/Schema.php | Uses explicit scalarOverrides to bypass scanning types for scalar override discovery. |
| docs/type-definitions/scalars.md | Documents scalarOverrides as an alternative to types for overriding built-ins without resolving lazy types. |
| docs/schema-definition.md | Updates schema config options table to include scalarOverrides and clarify lazy-resolution behavior. |
| docs/class-reference.md | Adds scalarOverrides option and new getter/setter to the class reference. |
| CHANGELOG.md | Notes the new scalarOverrides option under Unreleased additions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $explicitScalarOverrides = $this->config->scalarOverrides; | ||
| if ($explicitScalarOverrides !== null) { | ||
| return $this->scalarOverrides = $explicitScalarOverrides; | ||
| } |
There was a problem hiding this comment.
Hi @spawnia, would you like me to action this one?
Feedback from my claude was that as written, it matches existing convention. Docblock documents name-keyed contract.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/Type/Schema.php:381
getScalarOverrides()returns$this->config->scalarOverridesverbatim when non-null, but the code later assumes it is keyed by scalar name (e.g.isset($scalarOverrides[$name])). BecauseSchemaConfig::$scalarOverridesis a public property, users can set it directly as a list (e.g.[$uppercaseString]), which would silently skip the discovery scan and fail to apply the override (numeric keys). Consider normalizing/validating the explicit overrides here (or viaSchemaConfig::setScalarOverrides()) before caching them.
$explicitScalarOverrides = $this->config->scalarOverrides;
if ($explicitScalarOverrides !== null) {
return $this->scalarOverrides = $explicitScalarOverrides;
}
Fixes the silent loss of lazy type loading introduced with per-schema scalar overrides (#1869, v15.31.0), by allowing overrides to be set explicitly on
SchemaConfigso the discovery scan oftypescan be skipped. Related to #1874.The problem
The executor and validator resolve built-in scalars (
Boolean,String,Int,Float,ID) viaSchema::getType()on essentially every operation — including a bare{ __typename }.Since #1869 (and after #1884 moved scalar overrides exclusively to
types), the first such lookup goes:getType()callsloadType(), which returnsnullfor built-in scalar names when atypeLoaderis set (theTODOreferencing After Upgrading from v15.30.2 => v15.31.0 Error: " {"exception":"[object] (GraphQL\\Error\\Error(code: 0): Config element \"String\" is not declared in GraphQL schema at " #1874).getType()falls through togetScalarOverrides(), which callsmaterializeTypes()— fully resolving thetypesconfig to scan it forScalarTypeinstances shadowing built-in names.When
typesis a lazy callable, that scan resolves it entirely. This silently turns the documented lazy schema pattern —typeLoaderfor per-name loading plus a lazytypescallable for types not reachable from the root types — eager on the first executed query, for every schema, including the overwhelming majority that define no scalar overrides at all. The cost lives inside the user's callable, so the library cannot be lazy after invoking it; the only fix is to not invoke it on the hot path.In PHP-FPM the
Schemais rebuilt per request, so this is a per-request cost. Downstream impact observed in a Lighthouse (nuwave/lighthouse) app: every GraphQL operation became 1.5–3.4× slower wall-clock between 15.30.2 and 15.32.3 (~6× more PHP work, xdebug cachegrind 9.1 MB → 57 MB for an authed{ __typename }request). Lighthouse'sSchemaBuilder::build()is exactly this pattern:Repro
getType('Boolean')scalarOverridesset (e.g.[])The fix
Overrides cannot be discovered from a lazy
typescallable without resolving it — that is fundamental. So this PR makes explicitness possible instead: a newSchemaConfigoptionscalarOverrides.null(default): discover by scanningtypes, exactly as before — zero behaviour change for existing users.[]for "none"): use exactly these overrides, never scan. Explicit overrides take precedence ingetTypeMap()the same way discovered ones do.The setter validates entries are
ScalarTypeinstances named after built-in scalars.This gives lazy-schema users a one-line opt-out while keeping the scalar-overrides feature fully intact. If this lands, Lighthouse's
SchemaBuilder::build()could pass its known overrides (usually[]) explicitly and restore its pre-15.31 per-request performance — happy to file the companion PR there.Downstream context: we currently pin 15.30.2 to avoid the regression, which re-exposes GHSA-r7cg-qjjm-xhqq, GHSA-fc86-6rv6-2jpm and the 15.31.5 quadratic-validation fix — this change is what would let us (and others in the same spot) unpin.
🤖 Generated with Claude Code