从 ApolloServer GraphQL 向前端发送错误状态代码
我在 Node 上运行这个简单的 Apollo Server GraphQL,我希望每次收到 401 之类的错误或 GQL 从 formatError
获得的任何其他错误时,将该错误状态发送到前端,因为现在 FE 总是即使有错误,
const gateway = new ApolloGateway({
serviceList,
})
const server = new ApolloServer({
gateway,
formatError: (err: GraphQLError) => {
return new ApolloError(err.message, err.extensions.code, err.extensions)
},
})
我现在也会收到 200,每次抛出错误时 formatError
都会将该错误对象返回给 FE,但我还需要更改响应状态代码,而不是一直是 200。
I have this simple Apollo Server GraphQL running on Node, i want that every time i get something like 401 or any other error that GQL gets from formatError
, send that error status to Front End, because now FE always receives 200 even if there is an error
const gateway = new ApolloGateway({
serviceList,
})
const server = new ApolloServer({
gateway,
formatError: (err: GraphQLError) => {
return new ApolloError(err.message, err.extensions.code, err.extensions)
},
})
I have now that every time error is thrown formatError
will return that error object to FE but i need to change response status code also instead of that being 200 all the time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 GraphQL 文档中:
说了这么多,你可以按照既定的流程创建一个插件 链接:
从上面来看,添加
@ts-ignore
语句很重要,因为它是GraphQLRequestContext 接口。In GraphQL documentation:
Said that then, You can create a plugin according to the established flow Link:
From the above, it's important to add the
@ts-ignore
statement because it's an optional response statement in theGraphQLRequestContext
interface.