I haven’t seen any documented use of SubscriptionData so I’d like to know is this a “good idea” or is there a better way to connect to a graphql subscription from another server.
Our situation is seemingly unique. I have a two servers one of them speaks graphql. From the other I want to subscribe to the other to get continuous updates.
Looking at the client code I found that the heart of the useSubscription function is the SubscriptionData class. I can’t find any examples on anyone doing this… but I got this to work fairly easily. This is the result:
import {
ApolloClient,
InMemoryCache,
} from '@apollo/client';
import { WebSocketLink } from '@apollo/client/link/ws';
import { SubscriptionData } from "@apollo/client/react/data";
const wsLink = new WebSocketLink({
uri: 'ws://example.com/graphql',
webSocketImpl: WebSocket,
options: {
reconnect: true
}
});
const client = new ApolloClient({
link: wsLink,
cache: new InMemoryCache()
});
const SUBSCRIPTION_QUERY= gql`
subscription StockCodeSubscription {
stockQuotes {
dateTime
stockCode
stockPrice
stockPriceChange
}
}
`;
(async function() {
const options = { subscription: SUBSCRIPTION_QUERY, client: client }
const s = new SubscriptionData({options});
s.currentObservable.query.subscribe(r=>{
console.log(r.data);
})();
This code can run on node and seems to work. The main reason I’m concerned is that SubscioptionData is not mentioned in the Apollo documentation. I’m worried that this could break in the future in a way that we can no longer access the logic that allows this to work.