You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Introduce resolveValue method to Interface and Union types (#1776)
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
0 commit comments