GraphQL - Get all fields from nested JSON object

后端 未结 2 1712
囚心锁ツ
囚心锁ツ 2021-02-05 13:47

I\'m putting a GraphQL wrapper over an exiting REST API as described in Zero to GraphQL in 30 minutes. I\'ve got an API endpoint for a product with one property that points to a

2条回答
  •  面向向阳花
    2021-02-05 14:20

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

    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.

    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!

提交回复
热议问题