-
-
Notifications
You must be signed in to change notification settings - Fork 571
Introduce resolveValue
method to Interface and Union types
#1776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+363
−97
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Instead of calling it |
26e8d73
to
80ba591
Compare
spawnia
reviewed
Oct 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name resolveValue
makes sense to me, especially considering the symmetry with resolveType
.
This allows transforming the object value after type resolution. This is useful when you have a single entity that needs different representations. For example, when your data layer returns a generic `PetEntity` with a type discriminator, but your GraphQL schema has separate `Dog` and `Cat` types. Example: ```php $PetType = new InterfaceType([ 'name' => 'Pet', 'resolveType' => static function (PetEntity $objectValue): string { if ($objectValue->type === 'dog') { return 'Dog'; } return 'Cat'; }, 'resolveValue' => static function (PetEntity $objectValue) { if ($objectValue->type === 'dog') { return new Dog($objectValue->name, $objectValue->woofs); } return new Cat($objectValue->name, $objectValue->meows); }, 'fields' => ['name' => ['type' => Type::string()]], ]); Now field resolvers receive the properly typed Dog or Cat object instead of the generic PetEntity, allowing for type-safe resolution without needing to transform objects before they reach the type resolver. Common use cases: - Database polymorphism (single table with type column) - External APIs returning generic objects with type discriminators
80ba591
to
a753f1c
Compare
spawnia
added a commit
that referenced
this pull request
Oct 8, 2025
ruudk
added a commit
to ruudk/graphql-php
that referenced
this pull request
Oct 8, 2025
In hindsight it makes more sense to first resolve the value to the desired type before calling `resolveType`. Related to webonyx#1776
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Note
This is an alternative (and I think better) approach to:
$objectValue
by reference when callingresolveType
#1774This allows transforming the object value after type resolution.
This is useful when you have a single entity that needs different representations. For example, when your data layer returns a generic
PetEntity
with a type discriminator, but your GraphQL schema has separateDog
andCat
types.Example:
Now field resolvers receive the properly typed Dog or Cat object instead of the generic PetEntity,
allowing for type-safe resolution without needing to transform objects before they reach the type
resolver.
Common use cases: