Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscriptions? #5

Closed
thealjey opened this issue Nov 21, 2017 · 2 comments
Closed

Subscriptions? #5

thealjey opened this issue Nov 21, 2017 · 2 comments

Comments

@thealjey
Copy link
Contributor

How about websocket subscriptions?

@thealjey
Copy link
Contributor Author

thealjey commented Jan 8, 2018

turns out it is really simple 😄

/* @flow */

import {withData} from 'next-apollo';
import {HttpLink} from 'apollo-link-http';
import {WebSocketLink} from 'apollo-link-ws';
import {split} from 'apollo-client-preset';
import {getMainDefinition} from 'apollo-utilities';
import {port} from '../constants';

export default withData(headers => {
  const httpLink = new HttpLink({
    uri: `http://localhost:${port}/graphql`,
    // this goes here directly (there's actually no "opts" config)
    credentials: 'same-origin',
    // resend the headers on the server side, to be truly isomorphic (makes authentication possible)
    headers
  });

  return {
    link: process.browser
      ? split(
        ({query}) => {
          const {kind, operation} = getMainDefinition(query);

          return 'OperationDefinition' === kind && 'subscription' === operation;
        },
        // that's all you need to make subscriptions work :)
        new WebSocketLink({
          uri: `ws://localhost:${port}/subscriptions`,
          options: {reconnect: true}
        }),
        httpLink
      )
      : httpLink
  };
});

@adamsoffer
Copy link
Owner

awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants