我在我的 React 应用程序中使用 @apollo/client 和 @absinthe/socket-apollo-link NPM 包,但是在解析和钩子onError
的实现中收到的查询和突变错误时遇到了一些麻烦。useQuery
useMutation
例如,这是我在组件中设置查询的方式:
useQuery(OperationLib.agendaQuery, {
fetchPolicy: "network-only",
onCompleted: ({ myData }) => {
setData(myData)
setLoading(false)
},
onError: (error) => {
console.log(error)
}
})
调用该onError
处理程序时,返回的错误对象记录为:
Error: request: [object Object]
at new ApolloError (app.js:36358)
at app.js:146876
at app.js:145790
at new Promise (<anonymous>)
at Object.error (app.js:145790)
at notifySubscription (app.js:145130)
at onNotify (app.js:145169)
at SubscriptionObserver.error (app.js:145230)
at app.js:58209
at Array.forEach (<anonymous>)
我可以将此响应分解为各个部分"graphQLErrors"
, "networkError"
, "message"
, "extraInfo"
,但我发现很难在那里获得任何有用的信息。特别是,我希望能够从中得到一些东西message
- 但在这种情况下,error.message
是字符串,
request: [object Object]
typeof error.message
记录string
所以是的,我对此无能为力。
也许我可以在其他属性之一下找到有用的东西?不,graphQLErrors
是一个空数组,networkError
产生的输出与我在上面记录初始错误时得到的输出相同,并且extraInfo
未定义。
我深入研究了源代码并找到了方法createRequestError
- 当我在此处添加调试日志以查看是什么message
时,我提供了一些好的数据 - 我可以看到我认为在错误响应中某处可用的消息:
var createRequestError = function createRequestError(message) {
return new Error("request: ".concat(message));
}.bind(undefined);
什么可能导致此问题?我需要在 Apollo/Absinthe 初始化中配置什么吗?我已经这样设置了:
apollo-client.js
import { ApolloClient, InMemoryCache } from "@apollo/client"
import absintheSocketLink from "./absinthe-socket-apollo-link"
export default new ApolloClient({
link: absintheSocketLink,
cache: new InMemoryCache()
})
absinthe-socket-apollo-link.js
import * as AbsintheSocket from "@absinthe/socket"
import { createAbsintheSocketLink } from "@absinthe/socket-apollo-link"
import { Socket as PhoenixSocket } from "phoenix"
const protocol = window.location.protocol === "https:" ? "wss" : "ws";
const getToken = () => JSON.parse(window.localStorage.getItem("token"))
let token = getToken();
const params = {
get jwt() {
if (!token) {
token = getToken();
}
return token;
},
};
export default createAbsintheSocketLink(
AbsintheSocket.create(
new PhoenixSocket(`${protocol}://${WS_API_URL}/graphql`, {
reconnect: true,
params: params
})
)
);
非常感谢您的任何见解!