我在我的应用程序中使用 graphql,它错误地发送以下响应。
您可能已经注意到 graphql 将所有错误作为状态代码 200 发送。我从 API 返回的错误位于data.errors
.
{
"data": {
"errors": [
{
"message": "Error: Token has expired",
"locations": [
{
"line": 3,
"column": 7
}
],
"path": [
"createGame"
],
"extensions": {
"code": "403",
"exception": {
"stacktrace": [
"Error: Error: Token has expired"
]
}
}
}
],
"data": {
"createGame": null
}
},
"status": 200,
"statusText": "OK",
"headers": {
"content-type": "application/json; charset=utf-8"
},
"config": {
"url": "http://localhost:4200/graphql",
"method": "post",
"data": "{\"query\":\"\\n mutation {\\n createGame (name: \\\"SA\\\", shortName: \\\"s\\\") {\\n id,\\n name,\\n shortName\\n }\\n }\\n \"}",
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoxLCJlbXBsb3llZUlkIjowLCJ1c2VyUm9sZUlkIjozLCJwYXNzd29yZCI6IiQyYiQxMCRwNTE2V1hnSGZXdWx6czVmY2o2ZGp1Q09lUzF0UUw3U2MySEIuMDRPUmpWekh6VnduSTNwNiIsImlzQWN0aXZlIjp0cnVlLCJjcmVhdGVkQXQiOiIyMDE5LTA4LTE5VDA5OjMxOjA5LjAzOVoiLCJ1cGRhdGVkQXQiOiIyMDE5LTA4LTE5VDA5OjMxOjA5LjA0MVoifSwiaWF0IjoxNTY3ODcyNTk2LCJleHAiOjE1Njc4NzI2MDZ9.HpNE9m5YEUv0qgBHxsEoQMd1p29TkOqvQzYF7ptljJ0"
},
"baseURL": "http://localhost:4200/graphql",
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1
},
"request": {}
}
我遇到的问题是我正在前端集成 axios 响应拦截器
http.interceptors.response.use(
/**
* Leave response as it is.
*/
(response: any) => response,
/**
* This interceptor checks if the response had a 401 status code, which means
* that the access token used for the request has expired. It then refreshes
* the access token and resends the original request.
*/
unauthorizedResponseHandlerInterceptor
);
And the unauthorizedResponseHandlerInterceptor
该函数仅在出现错误时由 Axios 调用,但我的 graphql 服务器返回状态为 200,因此该函数不会被调用。
我有什么方法可以修改 axios 响应拦截器,以便它检查中的值数据错误代替status