且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

GraphQL - 从嵌套的 JSON 对象中获取所有字段

更新时间:2023-01-16 21:29:46

我可以做第二个版本,但它需要很多额外的代码,包括创建一个 NestedObjectType 和指定所有嵌套的属性.

I can do the second version, but it requires lots of extra code, including creating a NestedObjectType and specifying all the nested properties.

去做吧!这将会非常棒.为了充分发挥 GraphQL 的潜力,这就是要走的路.

Do it! It will be great. That's the way to go in order to use GraphQL to its full potential.

除了防止过度获取之外,它还为您提供了许多其他好处,例如类型验证,以及更具可读性和可维护性的代码,因为您的架构提供了更完整的数据描述.稍后您会感谢自己预先做了额外的工作.

Aside from preventing over-fetching, it also gives you a lot of other benefits like type validation, and more readable and maintainable code since your schema gives a fuller description of your data. You'll thank yourself later for doing the extra work up front.

如果由于某种原因你真的不想走那条路并完全理解后果,你可以使用 JSON.stringify 将嵌套对象编码为字符串.

If for some reason you really don't want to go that route though and fully understand the consequences, you could encode the nested objects as strings using JSON.stringify.

但就像我说的,我建议你不要!

But like I said, I recommend you don't!