In GraphQL, how do I handle `resolveType` and` isTypeOf` when the `interfaces` function is used?

I read this great meaning - GraphQLInterfaceType

But still there are some bewilders:

  • Is it really necessary to define ES6 classes for all types of GraphQL schemas?
    • The main problem here: we will end up with a lot of empty ES6 classes and an equivalent amount of GraphQL types .
  • If this is not the case, then how to handle resolveType and isTypeOf when using the interfaces functions?
  • Even I defined all ES6 classes for all GraphQL types , but the source data was built in different places using different technologies, such as grpc+protobuf , which have nothing to do with these class definitions, therefore as isTypeOf: (value) => value instanceof Dog work here?
+5
source share
1 answer

The implementation of resolveType and isTypeOf very flexible for a reason: it is extremely application specific. It depends on the database, data models, similar types, etc. Some servers may have separate ES6 classes for all their models, especially if you use ORM, where it creates instances of these classes when querying the database. But ORM is not required. And you do not need to instantiate other classes to determine the type of GraphQL.

In some cases, you can determine the type solely from the properties of objects. If this does not apply to your application, there are things you can do to give clues. Here is an example of SQL.

 SELECT id, body, author_id, post_id, 'Comment' AS "$type" -- leave a hint to resolve the type FROM comments UNION SELECT id, body, author_id, NULL AS post_id, 'Post' AS "$type" -- leave a hint to resolve the type FROM posts 

This query provides a "type hint", an optional computed column, so the resolveType implementation is a simple property search. Other DBMSs may use a similar strategy.

+3
source

Source: https://habr.com/ru/post/1266845/


All Articles