prismaNode implementing an interface #529
-
I have a couple of Prisma objects implementing the same interface:
However, this way I can't have a |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
This is a complicated problem to solve. isTypeOf or resolveType are needed only for fields that return an abstract type (interface or union). If you are only using an interface to share fields, then those methods are not required. In all other cases, there are multiple potential types an object could be. Prisma unfortunately does not provide any identifying information on which table a record is from (there is an open issue requesting this as a feature), but even if it did, Pothos allows multiple graphql object types to be created for the same table. This means we need some hints from the user/app to figure out those types. We can do this 3 different ways: An isTypeOf check as you mentioned. This will run whenever this object type is returned, not just when used for an interface or union A resolveType check on the union or interface, this will only run as needed. The final option isn't really documented, but the ref returned when creating a prismaObject has an addTypeBrand method on it that can be used to brand an object or list of objects as a specific type in a resolver. The default resolveType method knows how to detect these brands and doesn't require any additional setup. You only need to use thos in resolvers that return an abstract type |
Beta Was this translation helpful? Give feedback.
-
Thanks for your help! I do have a follow up question on the same topic. I'm using a class to define my interface, now I have multiple prismaNodes implementing this interface. Ideally I would want to override some of the methods defined on my interface class for each prismaNode. But as I don't manually define the class for each prismaNode, I can't think of a way to do this. Am I missing something? |
Beta Was this translation helpful? Give feedback.
This is a complicated problem to solve.
isTypeOf or resolveType are needed only for fields that return an abstract type (interface or union). If you are only using an interface to share fields, then those methods are not required.
In all other cases, there are multiple potential types an object could be. Prisma unfortunately does not provide any identifying information on which table a record is from (there is an open issue requesting this as a feature), but even if it did, Pothos allows multiple graphql object types to be created for the same table. This means we need some hints from the user/app to figure out those types.
We can do this 3 different ways:
An isTypeOf check as you mention…