首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将阿波罗客户端连接到aws appsync

无法将阿波罗客户端连接到aws appsync
EN

Stack Overflow用户
提问于 2020-08-16 14:53:19
回答 1查看 1.8K关注 0票数 0

我有一个web应用程序,使用appsync作为后端和react +阿波罗客户端(v3)作为前端。但是,当我尝试将阿波罗客户端连接到appsync时,我会从库中得到一条错误消息:

./node_modules/aws-appsync-react/lib/offline-helpers.js

模块未找到:无法解决'/Users/mypath/web/node_modules/aws-appsync-react/lib‘中的“反应-阿波罗”

下面是客户机的配置:

代码语言:javascript
复制
import AWSAppSyncClient from "aws-appsync";
import AppSyncConfig from "./aws-exports";

export const apolloClient = new AWSAppSyncClient({
  url: AppSyncConfig.aws_appsync_graphqlEndpoint,
  region: AppSyncConfig.aws_appsync_region,
  auth: {
    type: AppSyncConfig.aws_appsync_authenticationType,
    apiKey: AppSyncConfig.aws_appsync_apiKey,
  },
});

在我的App.ts

代码语言:javascript
复制
import { ApolloProvider } from "@apollo/client";
import { Rehydrated } from "aws-appsync-react";
import { apolloClient } from "./apollo";

...
<ApolloProvider client={apolloClient}>
  <Rehydrated>
      <MyApp />
  </Rehydrated>
</ApolloProvider>

看起来是兼容性问题吗?

我在用"@apollo/client": "^3.1.3", "aws-appsync": "^4.0.0","aws-appsync-react": "^4.0.0",

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-17 13:51:47

这是一个兼容性问题。当前版本的aws-appsync不支持apollo-client v3,请参阅此线程了解进度:https://github.com/awslabs/aws-mobile-appsync-sdk-js/issues/448

最好的解决办法是:Proper way to setup AWSAppSyncClient, Apollo & React

注意,解决方案确实使用了两个不推荐的库,但可以稍微改进如下:

代码语言:javascript
复制
import { ApolloClient, ApolloLink, InMemoryCache } from "@apollo/client";
import { createAuthLink } from "aws-appsync-auth-link";
import { createHttpLink } from "apollo-link-http";
import AppSyncConfig from "./aws-exports";

const url = AppSyncConfig.aws_appsync_graphqlEndpoint;
const region = AppSyncConfig.aws_project_region;
const auth = {
  type: AppSyncConfig.aws_appsync_authenticationType,
  apiKey: AppSyncConfig.aws_appsync_apiKey,
};
const link = ApolloLink.from([
  // @ts-ignore
  createAuthLink({ url, region, auth }),
  // @ts-ignore
  createHttpLink({ uri: url }),
]);
const client = new ApolloClient({
  link,
  cache: new InMemoryCache(),
});

export default client;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63438293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档