我的阿波罗客户端不能得到任何网络错误,只有GraphQL错误。
我将客户端设置如下:
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.map(({ message, locations, path }) =>
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)
);
if (networkError) console.log(`[Network error]: ${networkError}`);
});
const client = new ApolloClient({
link: ApolloLink.from([
errorLink,
new HttpLink({
uri: 'http://localhost:4000/graphql'
}),
]),
});在服务器上,我发出了一个请求,我这样解析它:
.then(res => {
if (res.ok) {
return { blah: res.json(), count };
}
throw new Error();
})
.catch(error => {
throw new AuthenticationError('Must Authenticate');
});AuthenicationError只是作为一个GraphQL错误出现在客户端([GraphQL error]: Message: Must Authenticate,,我基本上只是希望能够在客户端从服务器请求中获得HTTP Status Code。
谢谢
发布于 2019-02-28 14:54:33
您的客户端实现看起来是正确的。
我猜您正在使用带有一些graphl中间件的express来处理请求。主要的事情是,在graphql中间件投入使用之前,您需要处理身份验证过程。
因此,身份验证是一种graphql变体,您可以直接处理它。在我的例子中,它看起来有点像这样:
...
const app = express();
app.use(
"/graphql",
bodyParser.json(),
(request, response, next) => authenticationMiddleware(request) // authentication is handled before graphql
.then(() => next())
.catch(error => next(error)),
graphqlExpress(request => {
return {
schema: executable.schema,
context: {
viewer: request.headers.viewer
},
};
}),
);
...
app.listen(...)
另一种可能是将状态代码添加到graphql错误中的错误响应中。但这取决于您使用的npm包。例如,apollo-服务器-graphql https://www.apollographql.com/docs/apollo-server/api/apollo-server.html#constructor-options-lt-ApolloServer-gt中的formatError
https://stackoverflow.com/questions/54868706
复制相似问题