How to using multi server with multi schema #204
-
My application has separator admin and client so my graphql also sepatator
i saw in documents work with multi server
so when |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey there! The Adapting the example to your use case, what you're trying to achieve would look something like this: import express from 'express';
import { graphqlHTTP } from 'express-graphql';
import ws from 'ws';
import url from 'url';
import { useServer } from 'graphql-ws/lib/use/ws';
const app = express();
// register admin and user routes as usual
app.use('/admin', graphqlHTTP({ schema: adminSchema }));
app.use('/graphql', graphqlHTTP({ schema: userSchema }));
// create an admin WS server and use it with the admin schema
const adminWS = new ws.Server({ noServer: true });
useServer({ schema: adminSchema }, adminWS);
// create a user WS server and use it with the user schema
const userWS = new ws.Server({ noServer: true });
useServer({ schema: userSchema }, userWS);
// start the server
const server = app.listen(4000, () => {
// delegate upgrade requests to relevant destinations
server.on('upgrade', (request, socket, head) => {
const pathname = url.parse(request.url).pathname;
// admin ws route for admin schema
if (pathname === '/admin') {
return adminWS.handleUpgrade(request, socket, head, (client) => {
adminWS.emit('connection', client, request);
});
}
// graphql ws route for user chema
if (pathname === '/graphql') {
return userWS.handleUpgrade(request, socket, head, (client) => {
userWS.emit('connection', client, request);
});
}
// otherwise dont upgrade
return socket.destroy();
});
}); P.S. I converted the issue (#203) to this Q&A discussion since it is a question and not an issue. |
Beta Was this translation helpful? Give feedback.
Hey there! The
useServer
function actually means "use this server for graphql". It, on its own, does not spin up any new servers, it just "uses" the one you provide. Meaning, you can have as many servers as you want.Adapting the example to your use case, what you're trying to achieve would look something like this:
⚠️ Heads up, untested code ahead!