我需要集成subscription
到我的ReactNative
应用程序中。在subscription
上工作正常localhost
在graphiql
。我已经在上部署了后端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,
);
我既没有错误也没有数据。我触发subscription
从Graphiql
因此,我的React Native
代码没有问题。问题出在我使用的Heroku免费层。我尝试用替换它subscription
,Hasura's Subscription
https://hasura.io/learn/graphql/graphiql?tutorial=react-native
并且它起作用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句