gqlws-server
v0.19.8
Published
gqlws-server
Downloads
9
Readme
GQLWS Server
GraphQL over WebSockets (Server)
:warning: Work In Progress
Install
yarn add gqlws-server
or
npm i gqlws-server --save
Use
const GQLWSServer = require('gqlws-server');
const schema = `
type Query {
isPass: Boolean
}
type Mutation {
getProblem: Boolean
}
type Subscription {
testSub(channel: String!): String
}
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
`;
const resolvers = {
Query: {
isPass(root, args, connectionData) {
return true;
}
},
Mutation: {
getProblem(root, args, connectionData) {
throw new Error(`Problem for ${connectionData.connectionId}`);
}
},
Subscription: {
testSub(data) {
return data;
}
}
};
const server = new GQLWSServer({
schema,
resolvers,
port: 8080,
// server: require('http').createServer(),
ioOptions: {
namespace: '/',
transports: ['websocket']
}
});
server.run().then(() => {
console.log('GQLWSServer ready');
});
Connection Events
server.on('connection', connectionData => {
// let { connectionId, ...customData } = connectionData;
console.log('open connection', connectionData);
});
server.on('close', connectionData => {
console.log('close connection', connectionData);
});
Connection Custom Data
server.getData(connectionId); // -> { connectionId }
server.setData(connectionId, 'userId', '14');
server.getData(connectionId, 'userId'); // -> '14'
server.getData(connectionId); // -> { connectionId, userId }
server.delData(connectionId, 'userId');
Publish to subscription
server.publish('testSub', 'somedata', { channel: 'ch1' }, { userId: '14' });