documentation告诉我们解析器有这种形式:
fieldName: (obj, args, context, info) => result
如果“无法找到该对象”,则该结果为null
或undefined
。
其他错误怎么办 - 例如,如果未授权访问?
您似乎无法直接“返回”错误,因为结果必须与架构匹配。
我已经尝试在解析器中抛出一个异常,这几乎是正确的:客户端获取一个带有异常消息的ApolloError:这很好。但是apollo-server会发出堆栈跟踪,就好像没有处理异常一样。这似乎不对。
因此问题:如何做到这一点?
apollo-server堆栈跟踪,如果它有帮助:
Error: test exception message
at /Users/mgregory/NRN/src/agreeonit/apollo_server/resolvers/mutationResolvers.js:29:17
at tryCatcher (/Users/mgregory/NRN/src/agreeonit/apollo_server/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/mgregory/NRN/src/agreeonit/apollo_server/node_modules/bluebird/js/release/promise.js:510:31)
at Promise._settlePromise (/Users/mgregory/NRN/src/agreeonit/apollo_server/node_modules/bluebird/js/release/promise.js:567:18)
at Promise._settlePromise0 (/Users/mgregory/NRN/src/agreeonit/apollo_server/node_modules/bluebird/js/release/promise.js:612:10)
at Promise._settlePromises (/Users/mgregory/NRN/src/agreeonit/apollo_server/node_modules/bluebird/js/release/promise.js:691:18)
at Async._drainQueue (/Users/mgregory/NRN/src/agreeonit/apollo_server/node_modules/bluebird/js/release/async.js:138:16)
at Async._drainQueues (/Users/mgregory/NRN/src/agreeonit/apollo_server/node_modules/bluebird/js/release/async.js:148:10)
at Immediate.Async.drainQueues (/Users/mgregory/NRN/src/agreeonit/apollo_server/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:574:20)
at tryOnImmediate (timers.js:554:5)
at processImmediate [as _immediateCallback] (timers.js:533:5)
答案 0 :(得分:1)
我发现正在发布堆栈跟踪,因为GraphQLOptions.debug
的默认设置为true
。
在初始化服务器时将其设置为false会清除所有内容。
结论:抛出异常是正确的方法。