TypeError:无法读取未定义的属性'useGETForQueries'-GraphQL挂钩

问题描述

我正在为我的项目使用graphQL挂钩,我在测试中遇到问题,它会显示错误

TypeError: Cannot read property 'useGETForQueries' of undefined

在属于搜索组件的这一行代码中失败:

const [searchManual,{ loading: searchLoading,data,error }] = useManualQuery(SEARCH_QUERY);

认情况下,useGETForQuereiesfalse,但不确定为什么运行测试时会出现此错误。项目运行正常,这只是测试用例。我已在GraphQlClient

中为该变量明确添加了布尔值

graphql拦截器JS文件

const gqlAxios = axios.create()
gqlAxios.interceptors.response.use(
  function (response) {
    if(response.status === 401 || response.status === 403 )
        window.location.href = config.api.LOGIN_URL;
    
    return response;
  },function (error) {
    console.log("responseError",error);
    return Promise.reject(error);
  }
)
gqlAxios.interceptors.request.use(
  function (config) {
    config = {
      ...config,headers: {
          ...config.headers,'session': getCookieValue()
      }
    }
    return config;
  }
)


const fixed = config.api.APP_GQL_HOST;
const url = fixed + 'graphql';

export const client = new GraphQLClient({ url,fetch: buildAxiosFetch(gqlAxios),useGETForQueries: false });

client常量将导出到index.js,并用作ClientContext.Provider中的值。

const Wrapped = (
  <ClientContext.Provider value={client}>
    <App />
  </ClientContext.Provider>
)

ReactDOM.render(
  Wrapped,document.getElementById('root')
);

这可能是jsdom的错误,这是运行测试的实际控制台错误

console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
      Error: Uncaught [TypeError: Cannot read property 'useGETForQueries' of undefined]

如果是这样,如何解决?我只是想解决通过一个简单的测试用例:

it('Expect component in the document',() => {

        const { getByTestId } = render(<Search />);
        const component = getByTestId("test");
        expect(component).toBeInTheDocument();

    });

是否需要添加其他配置?我想使用msw(模拟服务工作程序),但是我陷入了这个错误,无法前进。有没有人遇到graphql hooks错误?另外,您对该图书馆有什么经验?优点是重量轻,但在测试时似乎并不友好。与阿波罗的任何比较。感谢您对这个问题的见识。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)