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

前端 未结 1 1257
天涯浪人
天涯浪人 2020-12-06 15:51

When we are using graphqlHTTP, the first argument passed to the resolve method is actually the parameters passed by the client query not root this is fine for a query resolv

相关标签:
1条回答
  • 2020-12-06 16:08

    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.

    0 讨论(0)
提交回复
热议问题