订阅在React Native中不起作用?

英式

我需要集成subscription到我的ReactNative应用程序中。subscription上工作正常localhostgraphiql我已经在上部署了后端Heroku我正在使用apollo-server而不是hasura我的订阅不适用于Heroku给出的url,但在localhost上可以正常使用。查询和突变对于localhost和Heroku url都可以正常工作。因此,我正在尝试从ReactNative客户端访问我的订阅我将基本URL保留为本地主机。查询和突变部分适用于我的ReactNative客户端,但我的订阅部分不起作用。

通过添加以下内容,我已为订阅配置了Apollo客户端

const httpLink = createHttpLink({
  uri: 'http://localhost:5000',
});

const wsLink = new WebSocketLink({
  uri: `ws://localhost:5000`,
  options: {
    reconnect: true,
    connectionParams: {
      // headers: {
      //   Authorization: `Bearer ${token}`,
      // },
    },
  },
});

const authLink = setContext(async (req, {headers}) => {
  try {
    const token = await AsyncStorage.getItem('token');
    return {
      headers: {
        ...headers,
        authorization: token ? `Bearer ${token}` : '',
      },
    };
  } catch (e) {
    console.log(e);
  }
});

const link = split(
  ({query}) => {
    const {kind, operation} = getMainDefinition(query);
    return kind === 'OperationDefinition' && operation === 'subscription';
  },
  wsLink,
  authLink.concat(httpLink),
);

const client = new ApolloClient({
  link: link,
  cache: new InMemoryCache(),
});

这是我的useSubscription钩子

const {data, error, loading} = useSubscription(
    HEALTH_CONSULTATION_SUBSCRIPTION,
  );

我既没有错误也没有数据。我触发subscriptionGraphiql

英式

因此,我的React Native代码没有问题问题出在我使用的Heroku免费层。我尝试用替换subscriptionHasura's Subscription https://hasura.io/learn/graphql/graphiql?tutorial=react-native并且它起作用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

PushNotificationIOS.presentLocalNotification()在react-native中不起作用

开关盒在React Native中不起作用

React Native在NPM 5.3.0中不起作用

React Native Http请求在Android中不起作用

图像resizeMode ='contain'在React Native中不起作用

为什么我的裁判在React Native中不起作用?

react-native-streetview在IOS中不起作用

shadowOffset在React Native Android中不起作用

React Native:iOS中的透明Stack Navigator不起作用

onNavigationStateChange在Android React Native中不起作用

连接分派在React Native中不起作用

navigator.geolocation.getCurrentPosition在React Native中不起作用

clearTimeout在React Native中不起作用

React-Native-Document-Picker在Android中不起作用

Axios发布请求在React Native中不起作用

posesEnabled:false在React Native中不起作用

onPress在React Native Flatlist中不起作用

React Native:NavigationOptions中的标题样式不起作用

flex在react-native中的ScrollView中不起作用

获取在 React Native ios 中不起作用

Tab 中的 react-native-maps 不起作用

React Native 中的垂直居中不起作用

在 React Native fetch 方法中解析 JSON 不起作用

视图样式在 React Native 中不起作用

clearInterval() 在 React Native 功能组件中不起作用

React-native-reanimated 在网络中不起作用

REACT NATIVE - this.setState 在 onChangeText 中不起作用

useScrollToTop 在 useEffect 中不起作用?(React Native Expo)

React Native:在TestFlight中显示图像不起作用