【问题标题】:ReactJS apolloClient Could not find "client" in the context or passed in as an optionReactJS apolloClient 在上下文中找不到“客户端”或作为选项传入
【发布时间】:2021-04-14 10:59:41
【问题描述】:

我不明白为什么它会触发我这个问题,并且它之前在我的另一个应用程序中以相同的方式工作。前三天刚试了下,还没解决这个问题。

我在 stackoverflow 上找到了这个解决方案:React Apollo Error: Invariant Violation: Could not find "client" in the context or passed in as an option

但这并没有解决我的问题

在这种情况下有人可以帮助我吗?

这是我的 App.js

import EmpTable from './components/empTable';
import { ApolloProvider } from '@apollo/react-hooks';


import { ApolloClient, InMemoryCache } from '@apollo/client';

const client = new ApolloClient({
  uri: 'http://localhost:8000/graphql/',
  cache: new InMemoryCache(),
});




function App() {
  return (
    <ApolloProvider client={client}>
        <EmpTable />
    </ApolloProvider>
  );
}

export default App;

这是我的 EmplyeeTable

import { gql, useQuery } from "@apollo/client";


function EmpTable() {

  const GET_EMPLOYEE = gql`
    query getEmp($id: String) {
      employeeById(id: $id) {
        id
        name
        role
      }
    }
  `;


  const {refetch} = useQuery(GET_EMPLOYEE)

  return (
    <div className="row">
      {/* some div  */}
    </div>
  );
}

export default EmpTable;

我收到以下代码错误:

Could not find "client" in the context or passed in as an option. Wrap the root component in an <ApolloProvider>, or pass an ApolloClient instance in via options.
new InvariantError
src/invariant.ts:12
   9 | export class InvariantError extends Error {
  10 |   framesToPop = 1;
  11 |   name = genericMessage;
> 12 |   constructor(message: string | number = genericMessage) {
  13 |     super(
  14 |       typeof message === "number"
  15 |         ? `${genericMessage}: ${message} (see https://github.com/apollographql/invariant-packages)`
View compiled
invariant
src/invariant.ts:27
  24 |   message?: string | number,
  25 | ): asserts condition {
  26 |   if (!condition) {
> 27 |     throw new InvariantError(message);
  28 |   }
  29 | }
  30 | 

这个错误太长了,我只是在这里放了几个。谁能告诉我到底是什么问题?

【问题讨论】:

    标签: reactjs react-apollo apollo-client


    【解决方案1】:

    尝试从@apollo/client 导入ApolloProvider

    import { ApolloProvider } from '@apollo/client';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-18
      • 2020-03-13
      • 2021-06-25
      • 2022-06-29
      • 2020-02-16
      • 2021-04-19
      • 2021-07-17
      • 2018-09-25
      相关资源
      最近更新 更多