GraphQLBundle
GraphQLBundle copied to clipboard
Is it possible to override serialization of EnumType?
| Q | A |
|---|---|
| Bug report? | no |
| Feature request? | no |
| BC Break report? | no |
| RFC? | no |
| Version/Branch | 0.15.0 |
Hi there,
I am trying to use an Enum which in my PHP code is a Collection with Value objects. When trying to parse the Collection as Enum I get the following error:
"debugMessage": "Expected a value of type \"ContentRating\" but received: COARSE_LANGUAGE",
"trace": [
{
"file": "\/var\/www\/vendor\/webonyx\/graphql-php\/src\/Executor\/ReferenceExecutor.php",
"line": 786,
"call": "GraphQL\\Executor\\ReferenceExecutor::completeLeafValue(GraphQLType: ContentRating, instance of App\\BoxedScalar\\ContentRating)"
},
I'm using the graphql config where my schema is as follows:
type MediaItem {
contentRatings: [ContentRating],
}
enum ContentRating {
DISCRIMINATION,
COARSE_LANGUAGE,
FEAR,
VIOLENCE,
SEX,
DRUGS_AND_ALCOHOL
}
It looks like it can't correctly find the correct value in the serialization process of the EnumType. I would like to change the way of serialization for the EnumType, since the following code works:
public function serialize($value)
{
$lookup = $this->getValueLookup();
// start added code
if($value instanceof ContentRating) {
$value = $value->getValue();
}
// end added code
if (isset($lookup[$value])) {
return $lookup[$value]->name;
}
throw new Error('Cannot serialize value as enum: ' . Utils::printSafe($value));
}
I'm using the ResolverMap for my resolvers.
Can you tell me if it's possible to override the serialization process of an EnumType somehow?
It looks like the problem is here because I use a Collection class for this Enum in my Entity that stores the ContentRating objects.
Hope you can help.