Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Multiple schemas on different endpoins #199

Closed
Anthonyzou opened this issue Jul 1, 2017 · 2 comments
Closed

Multiple schemas on different endpoins #199

Anthonyzou opened this issue Jul 1, 2017 · 2 comments

Comments

@Anthonyzou
Copy link

Hi,

I have multiple schemas in my application for a multitenant app.

I have code that is something like

SubscriptionServer.create(
  {
    execute,
    subscribe,
    schema: schema1,
  },
  {
    path: `/graphql/test1`,
    server
  }
);
SubscriptionServer.create(
  {
    execute,
    subscribe,
    schema: schema2
  },
  {
    path: `/graphql/test2`,
    server
  }
);

However once I add in another endpoint the client will fail to connect. I will get the error WebSocket connection to 'ws://localhost:4000/graphql/test1' failed: Invalid frame header.

Is there any sort of work around for this?

@mistic
Copy link
Contributor

mistic commented Jul 6, 2017

Hey @Anthonyzou could you please try the suggested official way from ws (websockets/ws#885)?

so it will be something like:

const server = http.createServer();
const wsServer1 = SubscriptionServer.create(
  {
    execute,
    subscribe,
    schema: schema1,
  },
  {
    noServer: true
  }
);
const wsServer2 = SubscriptionServer.create(
  {
    execute,
    subscribe,
    schema: schema2
  },
  {
    noServer: true
  }
);

server.on('upgrade', (request, socket, head) => {
  const pathname = url.parse(request.url).pathname;

  if (pathname === '/graphql/test1') {
    wsServer1.wsServer.handleUpgrade(request, socket, head, (ws) => {
      wsServer1.wsServer.emit('connection', ws);
    });
  } else if (pathname === '/graphql/test2') {
    wsServer2.wsServer.handleUpgrade(request, socket, head, (ws) => {
      wsServer2.wsServer.emit('connection', ws);
    });
  } else {
    socket.destroy();
  }
});

Let me know if it worked.

@DxCx
Copy link
Contributor

DxCx commented Aug 25, 2017

Thanks @mistic it works except for the fact emit of connection should provide request as well,
also, wsServer is private so need to access it using
so wsServerX['wsServer'].emit('connection', ws, req)

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

No branches or pull requests

3 participants