With GraphQL, what is the benefit or necessity of declaring a backing model “class” that does not vary from the GraphQL “Type” in any obvious way?

穿精又带淫゛_ 提交于 2019-12-06 16:03:17

They are not the same thing. Just the same name:

  • One is the object instance
  • the other is the structure of the objects (types use to document the API)

It could simply be that the boilerplate doesn't demonstrate the benefits of this approach. I think you already have some ideas where it could be beneficial, for example when your backend data model isn't exactly mapped to your GraphQL API.

A lot of organizations adopting GraphQL today have some existing backends and data models that they are used to working with. In this case, you can think of GraphQL as just another API layer, that sits next to your REST API or anything else. GraphQL is not your actual backend - you shouldn't be handling your business logic directly in the resolvers, because then your whole app will become coupled to GraphQL. This isn't as clear when you are building a new app from scratch with GraphQL, but it's a good pattern to follow because you will probably want to be flexible with your API in the future.

In the diagram above, GraphQL would be the "external interface." Note that it sits on top of the business logic/model layer, but doesn't replace it.

The types in your GraphQL API are the data you want to display in the UI. This could contain computed fields that aren't in the database, some fields could be pulling from multiple backends, etc.

Read about these concepts as presented by Dan Schafer in my summary of his React Europe talk here: https://medium.com/apollo-stack/graphql-at-facebook-by-dan-schafer-38d65ef075af#.awusd3ibv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!