이 로직을 사용하면 Query 및 Mutation은 HTTP를 사용하고 Subscriptions은 WebSocket을 사용한다.
이를 지원하기 위해 @apollo/client 라이브러리는 boolean 검사 결과에 따라 두 개의 다른 링크 중 하나를 사용할 수 있는 split함수를 제공합니다.
split함수는 3가지 인자를 갖는다.
1. Boolean type function
2. wsLink : 함수의 return값이 true면 wsLink가 선택된다.
3. httpLink: 함수의 return값이 true면 httpLink가 선택된다.
const splitLink = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
);
},
wsLink,
httpLink,
);