Object Identification Type Resolver ambiguous
tpetry opened this issue · 3 comments
The StarWars example uses a simple object property check to decide whether an object is a faction or a ship. For such a trivial example this easy, but with 50+ object types this is really really complex and error prone.
Is there a better way to implement the resolver? I was digging through the source code but did not find anything more usable. Deciding on the output is a really wacky solution. Having the requested global id would be a dream...
This depends on your implementation. If you store the typeName or the globalId in your PHP entities, this could be used.
I am using something similar to the TypeRegistry.
Inside of the TypeRegistry I also store a map of my PHP entities and the corresponding types. That way the type resolver looks something like:
$type = $typeRegistry->getTypeForNode($myEntity);
I hope that helps?
Another way is to assign GraphQL type to value when creating the value. Then your resolveType
becomes trivial. But it has it's own disadvantages and only works well for GraphQL-first projects.
In general GraphQL leaves design of "values" for types to you. There are no standards or recommendations here (yet).
Yeah, i began refactoring all code to include the GraphQL type into the PHP entities yesterday. Not a nice design but it works.
It would be nice if there would be a general solution. Like the ability to return the type with the globalidResolver
without changing any data entities because this code part already has to do the graphql type guessing but with the base64 globalid has the needed information to make the correct decision.