-
-
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
base: master
Are you sure you want to change the base?
Conversation
Instead of calling it |
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
26e8d73
to
80ba591
Compare
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
.
|
||
public string $name; | ||
|
||
public bool $woofs; |
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.
vocalizes
$contextValue | ||
) { | ||
$typeCandidate = $returnType->resolveType($result, $contextValue, $info); | ||
$result = $returnType->resolveValue($result, $contextValue, $info); |
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.
What's going on with the pass-by-reference of $result
? What happens if we remove &
?
Also, what is going on with its type - we seem to know it is array<mixed>
here, but is it really? What if me make it a hard type hint?
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: