gpt4 book ai didi

websocket - 如何将 Apollo 客户端与 AppSync 结合使用?

转载 作者:行者123 更新时间:2023-12-03 06:56:21 26 4
gpt4 key购买 nike

AppSync 使用基于 WebSocket 的 MQTT 进行订阅,而 Apollo 使用 WebSocket。将 apollo 与 AppSync 结合使用时,Subscription 组件或 Query 组件中的 subscribeForMore 都不适合我。

One AppSync feature that generated a lot of buzz is its emphasis on real-time data. Under the hood, AppSync’s real-time feature is powered by GraphQL subscriptions. While Apollo bases its subscriptions on WebSockets via subscriptions-transport-ws, subscriptions in GraphQL are actually flexible enough for you to base them on another messaging technology. Instead of WebSockets, AppSync’s subscriptions use MQTT as the transport layer.

有没有办法在使用 Apollo 的同时使用 AppSync?

最佳答案

好的,这就是它对我的作用。您需要使用 aws-appsync SDK ( https://github.com/awslabs/aws-mobile-appsync-sdk-js ) 才能将 Apollo 与 AppSync 结合使用。无需进行任何其他更改即可使订阅与 AppSync 配合使用。

配置 ApolloProvider 和客户端:

    // App.js
import React from 'react';
import { Platform, StatusBar, StyleSheet, View } from 'react-native';
import { AppLoading, Asset, Font, Icon } from 'expo';
import AWSAppSyncClient from 'aws-appsync' // <--------- use this instead of Apollo Client
import {ApolloProvider} from 'react-apollo'
import { Rehydrated } from 'aws-appsync-react' // <--------- Rehydrated is required to work with Apollo

import config from './aws-exports'
import { SERVER_ENDPOINT, CHAIN_ID } from 'react-native-dotenv'
import AppNavigator from './navigation/AppNavigator';

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,
// jwtToken: async () => token, // Required when you use Cognito UserPools OR OpenID Connect. token object is obtained previously
}
})


export default class App extends React.Component {
render() {
return <ApolloProvider client={client}>
<Rehydrated>
<View style={styles.container}>
<AppNavigator />
</View>
</Rehydrated>
</ApolloProvider>
}

组件中的订阅如下所示:

    <Subscription subscription={gql(onCreateBlog)}>
{({data, loading})=>{
return <Text>New Item: {JSON.stringify(data)}</Text>
}}
</Subscription>

关于websocket - 如何将 Apollo 客户端与 AppSync 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52960709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com