在查询上下文或传递的道具中找不到“客户端”

2024-04-19

这就是我尝试在我的 React Native 应用程序中实现 apollo graphQL 的方式。

但我确实得到了错误

Could not find "client" in the context of Query or as passed props. Wrap the root component in an <ApolloProvider>

所以我以为我已经做到了这一点,但显然我误解了一些东西。

import React, { Component } from 'react'
import { ApolloProvider, graphql } from 'react-apollo'
import { ApolloClient } from 'apollo-client'
import { createHttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import gql from 'graphql-tag'

import Routes from './config/routes'

// Initialize the Apollo Client
const client = new ApolloClient({
  link: createHttpLink({ uri: 'http://localhost:3000' }),
  cache: new InMemoryCache()
})

// Define query types
const DATA_QUERY = gql`
  query {
    getVersion {
      version
    }
  }
`

export class App extends Component {
  render () {
    console.log(this.props)
    // ApolloProvider lets us use Apollo Client in descendant components
    return (
      <ApolloProvider client={client}>
        <Routes />
      </ApolloProvider>
    )
  }
}

// Inject query response as `this.props.data`
export default graphql(DATA_QUERY)(App)

我在开始使用后遇到了这个问题,试图找到同一问题的解决方案@apollo/react-hooks。由于错误的表述是相同的,我仍然会提供我的答案,以防它对某人有帮助。

错误是缺少ApolloHooksProvider围绕内容。我通过改变这个来修复它

<ApolloProvider client={client}>
    <Content />
</ApolloProvider>

to this

import { ApolloProvider as ApolloHooksProvider } from '@apollo/react-hooks'

<ApolloProvider client={client}>
    <ApolloHooksProvider client={client}>
        <Content />
    </ApolloHooksProvider>
</ApolloProvider>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在查询上下文或传递的道具中找不到“客户端” 的相关文章

随机推荐