How to access the value of parent resolver inside a field resolver while using buildSchema?

限于喜欢 提交于 2019-11-28 14:35:38

Short answer: don't use buildSchema.

Passing resolve functions through the root value only works because it relies on the default resolver behavior, and it only works for root-level fields. Unfortunately, if you use buildSchema, the only way to provide resolvers is through the root value.

Your options are:

  1. Build your schema programatically instead of using Schema Definition Language (SDL). This will allow you to specify a resolve function for any field in your schema, and that resolve function will get all four parameters (parent value, arguments, context and info). You can check out the docs for some examples.

  2. Write your schema in SDL but use makeExecutableSchema from graphql-tools to generate your GraphQLSchema instance. makeExecutableSchema allows you to painlessly inject resolvers for any fields and provides a number of other features. More information about how to generate a schema this way can be found here.

  3. Dump express-graphql for apollo-server, which uses makeExecutableSchema under the hood and provides a number of additional features that express-graphql does not. Check the docs for how to get started.

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