Ryn*_*yne 5 graphql react-apollo apollo-client aws-appsync
我在开始使用 React、Apollo 和 AWS-AppSync 时遇到了问题。我无法解决此错误消息:
TypeError: this.currentObservable.query.getCurrentResult is not a function
Run Code Online (Sandbox Code Playgroud)
我正在使用@apollo/react-hooks 和 aws-appsync 的更新包。
我目前的设置看起来像这样。
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import config from './aws-exports';
import AWSAppSyncClient from 'aws-appsync';
import { ApolloProvider } from '@apollo/react-hooks';
const client = new AWSAppSyncClient({
url: config.aws_appsync_graphqlEndpoint,
region: config.aws_appsync_region,
auth: {
type: config.aws_appsync_authenticationType,
apiKey: config.aws_appsync_apiKey
}
});
ReactDOM.render(
<ApolloProvider client={client}>
<React.StrictMode>
<App />
</React.StrictMode>
</ApolloProvider>,
document.getElementById('root')
);
Run Code Online (Sandbox Code Playgroud)
我有一个函数可以进行如下查询:
import React from 'react';
import { useQuery } from '@apollo/react-hooks';
import gql from 'graphql-tag';
const Flavors = () => {
const GET_FLAVORS = gql`
query listAll {
items {
name,
image
}
}
`;
const { loading, error, data } = useQuery(GET_FLAVORS);
if(loading) return <p>loading...</p>
if(error) return <p>Something went wrong...</p>
return (
<div>
{
data.listIceCreamTypes.items.map(type => {
return <div key={type.name}>
<img src={type.image} alt={type.name} />
<h1>{type.name}</h1>
</div>
})
}
</div>
)
}
export default Flavors;
Run Code Online (Sandbox Code Playgroud)
我已经经历了https://github.com/apollographql/react-apollo/issues/3148 中描述的各种解决方案,例如添加:
"resolutions": {
"apollo-client": "2.6.3"
}
Run Code Online (Sandbox Code Playgroud)
到 package.json。然后重新运行 npm install 并重新启动服务器。
似乎没有什么能解决我的问题。
编辑**这是一个重现问题的仓库:https : //github.com/Rynebenson/IceCreamQL
我已经在 stackoverflow 上的其他相关问题上回答了这个问题。
正如其他答案中提到的,问题是因为 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';
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
Run Code Online (Sandbox Code Playgroud)
我还在媒体上创建了一个更详细的帖子
| 归档时间: |
|
| 查看次数: |
2613 次 |
| 最近记录: |