微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从@apollo/client v 3.3.11

如何解决从@apollo/client v 3.3.11

问题是:

无法在上下文中找到“客户端”或作为选项传入。将根组件包裹在一个 中,或者通过选项传递一个 ApolloClient 实例。

当我运行 react server 并使用从 useMutation 导入的钩子 @apollo/client 时,我尝试从 @apollo/react-hook 导入它,但仍然是同样的问题。

请问有什么建议吗?

import React,{ useState } from 'react';
import logo from './logo.svg';
import './App.css';
import {ApolloClient,ApolloProvider,gql,InMemoryCache,useMutation} from '@apollo/client'
const CREATE_USER = gql `
    mutation createuser($name:String!  $username:String! $password:String!){
        createuser(name:$name username:$username password:$password){
            id,name,username
        }

    }
`

function App() {
  const [name,setname] = useState("")
  const [username,setusername] = useState("")
  const [passowrd,setpassowrd] = useState("")
  const client = new ApolloClient({
    uri: 'http://localhost:3001/graphql',cache: new InMemoryCache()
  });
  const [createU] = useMutation(CREATE_USER);
 
  return (
    <ApolloProvider client={client}>
      <div className="createuser">
        <h2>Create User</h2>
        <input type="text" placeholder="your name"/>
        <input type="text" placeholder="your username"/>
        <input type="password" placeholder="your password"/>
        <button> Create User</button>
      </div>
    </ApolloProvider>
  );
}

export default App;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。