“Struggling to solve ‘ApolloProvider was not passed a client instance.'”

huangapple go评论103阅读模式
英文:

Struggling to solve "ApolloProvider was not pased a client instance."

问题

这是您提供的内容的中文翻译:

我已经阅读了多篇文章和项目,试图解决这个问题。我认为问题出在我格式化代码的方式上,但无法确定错误在哪里。我目前只是尝试将任何内容传递到页面上,以便我可以开始格式化网站。

这是我为页面编写的代码,请告诉我是否需要添加另一个文件的代码:

App.js

import './App.css';
import { ApolloProvider } from "@apollo/client";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

import Home from './pages/Home';
import Footer from './components/Footer';
import Header from './components/Header';

function App() {
  return (
    <ApolloProvider>
      <Router>
        <Header />
        <Routes>
          <Route
            path="/"
            element={<Home />}
          />
        </Routes>
        <Footer />
      </Router>
    </ApolloProvider>
  );
}

export default App;

index.js

import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

const rootElement = document.getElementById('root');
const root = createRoot(rootElement);

root.render(
  <StrictMode>
    <App />
  </StrictMode>
);

package.json

"dependencies": {
  "@apollo/client": "^3.5.8",
  "@testing-library/jest-dom": "^5.11.6",
  "@testing-library/react": "^11.2.2",
  "@testing-library/user-event": "^12.5.0",
  "react": "^18.1.0",
  "react-dom": "^18.1.0",
  "react-router-dom": "^6.2.1",
  "react-scripts": "5.0.1",
  "web-vitals": "^0.2.4",
  "graphql": "^15.4.0",
  "apollo-link": "^1.2.14",
  "apollo-link-error": "^1.1.13"
}

Home.js

import React from 'react';

const Home = () => {
    return (
        <main> 
            <p>This is the home page! Hello </p>
        </main>
    )
}

export default Home;

这是我收到的错误:

“Struggling to solve ‘ApolloProvider was not passed a client instance.'”

英文:

I have read multiple articles and items trying to solve this problem. I think I have a problem with the way that I have formatted my code but cannot figure out where the error is located. I am currently just trying to get anything to pass onto the page so I can start formatting the site.

This is the code that I have for the page, please let me know if I need to add another files code:

App.js

import &#39;./App.css&#39;;
import { ApolloProvider } from &quot;@apollo/client&quot;;
import { BrowserRouter as Router, Routes, Route } from &quot;react-router-dom&quot;;

import Home from &#39;./pages/Home&#39;
import Footer from &#39;./components/Footer&#39;
import Header from &#39;./components/Header&#39;


function App() {
  return (
    &lt;ApolloProvider&gt;
      &lt;Router&gt;
        &lt;Header /&gt;
        &lt;Routes&gt;
          &lt;Route
            path=&quot;/&quot;
            element={&lt;Home /&gt;}
            /&gt;
        &lt;/Routes&gt;
        &lt;Footer /&gt;
      &lt;/Router&gt;
    &lt;/ApolloProvider&gt;
  );
}

export default App;

index.js

import { StrictMode } from &#39;react&#39;;
import {createRoot} from &#39;react-dom/client&#39;;
import &#39;./index.css&#39;;
import App from &#39;./App&#39;;
import reportWebVitals from &#39;./reportWebVitals&#39;;

const rootElement = document.getElementById(&#39;root&#39;)
const root = createRoot(rootElement)

root.render(
  &lt;StrictMode&gt;
    &lt;App /&gt;
  &lt;/StrictMode&gt;
);

package.json

  &quot;dependencies&quot;: {
    &quot;@apollo/client&quot;: &quot;^3.5.8&quot;,
    &quot;@testing-library/jest-dom&quot;: &quot;^5.11.6&quot;,
    &quot;@testing-library/react&quot;: &quot;^11.2.2&quot;,
    &quot;@testing-library/user-event&quot;: &quot;^12.5.0&quot;,
    &quot;react&quot;: &quot;^18.1.0&quot;,
    &quot;react-dom&quot;: &quot;^18.1.0&quot;,
    &quot;react-router-dom&quot;: &quot;^6.2.1&quot;,
    &quot;react-scripts&quot;: &quot;5.0.1&quot;,
    &quot;web-vitals&quot;: &quot;^0.2.4&quot;,
    &quot;graphql&quot;: &quot;^15.4.0&quot;,
    &quot;apollo-link&quot;: &quot;^1.2.14&quot;,
    &quot;apollo-link-error&quot;: &quot;^1.1.13&quot;

Home.js

import React from &#39;react&#39;

const Home = () =&gt; {
    return (
        &lt;main&gt; 
            &lt;p&gt;This is the home page! Hello &lt;/p&gt;
        &lt;/main&gt;
    )
}

export default Home

Here is the error that I am receiving:

“Struggling to solve ‘ApolloProvider was not passed a client instance.'”

答案1

得分: 2

根据Apollo文档,您需要创建一个指向GraphQL服务器的客户端:

const client = new ApolloClient({
  uri: 'https://flyby-router-demo.herokuapp.com/',
  cache: new InMemoryCache(),
});

function App() {
  return (
    <ApolloProvider client={client}>
      <Router>
        <Header />
        <Routes>
          <Route
            path="/"
            element={<Home />}
          />
        </Routes>
        <Footer />
      </Router>
    </ApolloProvider>
  );
}
英文:

According to the Apollo documentation you need to create a client that will point to the GraphQL server:

const client = new ApolloClient({
  uri: &#39;https://flyby-router-demo.herokuapp.com/&#39;,
  cache: new InMemoryCache(),
});

function App() {
  return (
    &lt;ApolloProvider client={client}&gt;
      &lt;Router&gt;
        &lt;Header /&gt;
        &lt;Routes&gt;
          &lt;Route
            path=&quot;/&quot;
            element={&lt;Home /&gt;}
            /&gt;
        &lt;/Routes&gt;
        &lt;Footer /&gt;
      &lt;/Router&gt;
    &lt;/ApolloProvider&gt;
  );
}

huangapple
  • 本文由 发表于 2023年8月10日 20:45:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定