我的项目使用apollo useSubscription钩子和gqlgen golang服务器。
问题:当订阅来自UI(来自站点)时,在服务器端,我看到无限地订阅/取消订阅
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:41Z" level=info msg="subscribed: 684307d4-5f82-42bd-8d8b-3d8f5db915b5"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:44Z" level=info msg="message received: 684307d4-5f82-42bd-8d8b-3d8f5db915b5"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:44Z" level=info msg="message published: 684307d4-5f82-42bd-8d8b-3d8f5db915b5"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:44Z" level=info msg="channel close reason: context canceled"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:44Z" level=info msg="channel closed: 684307d4-5f82-42bd-8d8b-3d8f5db915b5"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:45Z" level=info msg="subscribed: 6c0741ce-e281-47d3-b333-6e341b82da7c"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:48Z" level=info msg="message received: 6c0741ce-e281-47d3-b333-6e341b82da7c"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:48Z" level=info msg="message published: 6c0741ce-e281-47d3-b333-6e341b82da7c"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:48Z" level=info msg="channel close reason: context canceled"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:48Z" level=info msg="channel closed: 6c0741ce-e281-47d3-b333-6e341b82da7c"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:49Z" level=info msg="subscribed: 9047a0da-5df0-48e0-80cf-86b584213b76"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:52Z" level=info msg="message received: 9047a0da-5df0-48e0-80cf-86b584213b76"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:52Z" level=info msg="message published: 9047a0da-5df0-48e0-80cf-86b584213b76"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:52Z" level=info msg="channel close reason: context canceled"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:52Z" level=info msg="channel closed: 9047a0da-5df0-48e0-80cf-86b584213b76"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:08:52Z" level=info msg="subscribed: ec636e7b-7d95-4e37-a3cd-a87089d5271b"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:09:52Z" level=info msg="channel close reason: context canceled"
[user-gateway-deployment-669bd99554-h8fbz user-gateway] time="2020-09-13T08:09:52Z" level=info msg="channel closed: ec636e7b-7d95-4e37-a3cd-a87089d5271b"
当我使用GraphQL Playground订阅时,一切都很好,我有1个订阅日志记录,许多收到发布,订阅完成后只有一个“通道关闭原因”。
const httpLink = new HttpLink({
uri: REACT_APP_GRAPHQL_URL,
});
const wsLink = new WebSocketLink({
uri: REACT_APP_GRAPHQL_SUBSCRIPTION_URL,
options: {
reconnect: true,
connectionParams: {
authToken: Cookies.get('token'),
},
},
});
const link = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
);
},
wsLink,
httpLink,
);
const client = new ApolloClient({
uri: REACT_APP_GRAPHQL_URL,
// eslint-disable-next-line
// @ts-ignore
link,
cache: new InMemoryCache(),
credentials: 'include',
clientState,
});
const UPDATE_BALANCE_SUBSCRIPTION = gql`
subscription balanceUpdate {
balanceUpdate {
walletId
amount
}
}
`;
export const useBalanceUpdateSubscription = () => {
return useSubscription<balanceUpdate>(UPDATE_BALANCE_SUBSCRIPTION);
};
挂钩的配置是标准配置,如阿波罗官方文档中所述。因此,为什么我们在行为上有所不同,这有点令人困惑。你能帮我吗?