【问题标题】:AWS AppSync + React-Apollo Query/useQuery raising exception this.currentObservable.query.getCurrentResult is not a functionAWS AppSync + React-Apollo Query/useQuery 引发异常 this.currentObservable.query.getCurrentResult 不是函数
【发布时间】:2020-01-06 18:38:10
【问题描述】:

我是 GraphQL/Apollo 的新手,很难用 React 应用程序设置它。

我有一个 React 组件,它从使用 Amplify/AppSync 构建的 GraphQL API 加载列表。

如果我手动调用来获取项目,即:

    const videosData = await client.query({
      query: gql(queries.listVideos)
    });
    const videosItems = videosData.data.listVideos.items;
    setVideosData(videosItems);

像魅力一样工作。 但是,如果我尝试使用 Apollo Query 组件或 useQuery 挂钩,则会引发以下错误:

TypeError: this.currentObservable.query.getCurrentResult 不是 功能

如果我只是添加行以使用钩子获取查询,它已经给了我这个错误

钩子调用:

const {loading, error, data, refetch} = useQuery(gql(queries.listVideos));

引发问题的被调用函数:

QueryData.getQueryResult
node_modules/@apollo/react-hooks/lib/react-hooks.esm.js:325
  322 |     called: true
  323 |   });
  324 | } else {
> 325 |   var currentResult = this.currentObservable.query.getCurrentResult();
      | ^  326 |   var loading = currentResult.loading,
  327 |       partial = currentResult.partial,
  328 |       networkStatus = currentResult.networkStatus,

如果我使用<Query> 组件,也会出现完全相同的问题

软件包版本:

"aws-amplify": "^1.1.30",
"aws-amplify-react": "^2.3.10",
"aws-appsync": "^1.8.1",
"graphql-tag": "^2.10.1",
"react-apollo": "^3.0.1",

知道我可能做错了什么以及如何解决吗?

【问题讨论】:

  • 我遇到了同样的问题。你找到解决办法了吗?
  • @BrianMcDonough 还没有... tbh 我在设置这个无服务器的 graphql api 时遇到了很多问题,而且几乎没有支持,我正在考虑回到旧的 REST 服务器

标签: reactjs graphql react-apollo aws-amplify aws-appsync


【解决方案1】:

如果你添加:

"resolutions": {
  "apollo-client": "2.6.3"
}

在您的package.json 中并重新安装它应该可以工作。

您可能会看到以下警告:

Resolution field "apollo-client@2.6.3" is incompatible with requested version "apollo-client@2.4.6"

这是因为 Appsync 依赖于旧版本的 react-apollo,但我发现它运行良好。

您可以关注this issue,希望很快会得到解决,我们不再需要这样做了。

【讨论】:

  • 这不起作用。答案中提到的问题仍然是开放的,并且争论不休。
【解决方案2】:

正如其他答案中提到的,问题是因为aws-appsync 依赖于以前的版本apollo-client。 使用分辨率不是解决此问题的“更干净”的方法,因为您正在修复与此库不完全兼容的依赖版本。

我强烈建议您以这种方式为 AWS AppSync 创建自定义 apollo 客户端:

import { ApolloProvider } from '@apollo/react-hooks';
import { ApolloLink } from 'apollo-link';
import { createAuthLink } from 'aws-appsync-auth-link';
import { createHttpLink } from 'apollo-link-http';
import { AppSyncConfig } from '.aws-exports';
import ApolloClient from 'apollo-client';
import { InMemoryCache } from "apollo-cache-inmemory";

const url = AppSyncConfig.graphqlEndpoint;
const region = AppSyncConfig.region;
const auth = {
  type: AppSyncConfig.authenticationType,
  apiKey: AppSyncConfig.apiKey
};
const link = ApolloLink.from([
   createAuthLink({ url, region, auth }), 
   createHttpLink({ uri: url })
]);
const client = new ApolloClient({
  link,
  cache: new InMemoryCache()
});

const WithProvider = () => (
  <ApolloProvider client={client}>
    <App />
  </ApolloProvider>
)

export default WithProvider

我还在medium上创建了一个更详细的帖子

【讨论】:

  • 这行得通,谢谢!!!我与jwtToken auth (Cognito) 一起使用。也许在你的答案中添加缺少的import { InMemoryCache } from "apollo-cache-inmemory";
  • 很高兴它为你工作@sigmus。我注意到并在我的帖子上修复了它,但我忘了在这里做同样的事情,谢谢!
  • @GuilleAcosta 很好的解决方案,谢谢。只是一个简单的问题,如何使它与订阅一起使用?
  • @SatvikDaga 您有订阅问题吗?你得到哪个错误?您是否按照这些步骤进行操作? apollographql.com/docs/react/data/subscriptions
  • @GuilleAcosta 我按照步骤操作,但出现错误。使用 createSubscriptionHandshakeLink 解决了它
猜你喜欢
  • 1970-01-01
  • 2021-06-08
  • 2021-01-20
  • 2020-09-02
  • 2019-11-19
  • 2019-10-06
  • 2021-04-04
  • 2021-12-08
  • 2019-03-11
相关资源
最近更新 更多