Skip to content

Commit

Permalink
Use a separate server for graphql subscriptions and fix channels.crea…
Browse files Browse the repository at this point in the history
…te (#2)

* Use a separate server for graphql subscriptions

* Fix an issue with channel creation in REST API
  • Loading branch information
kamilkisiela authored Sep 16, 2017
1 parent 9dd51b3 commit 161f9a1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
8 changes: 4 additions & 4 deletions packages/rocketchat-api/server/v1/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ function createChannelValidator(params) {
throw new Error('unauthorized');
}

if (!params.name.value) {
if (!params.name || !params.name.value) {
throw new Error(`Param "${ params.name.key }" is required`);
}

if (params.members.value && !_.isArray(params.members.value)) {
if (params.members && params.members.value && !_.isArray(params.members.value)) {
throw new Error(`Param "${ params.members.key }" must be an array if provided`);
}

if (params.customFields.value && !(typeof params.customFields.value === 'object')) {
if (params.customFields && params.customFields.value && !(typeof params.customFields.value === 'object')) {
throw new Error(`Param "${ params.customFields.key }" must be an object if provided`);
}
}
Expand Down Expand Up @@ -176,7 +176,7 @@ RocketChat.API.v1.addRoute('channels.create', { authRequired: true }, {
let error;

try {
RocketChat.API.create.validate({
RocketChat.API.channels.create.validate({
user: {
value: userId
},
Expand Down
42 changes: 27 additions & 15 deletions packages/rocketchat-graphql/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import cors from 'cors';

import { executableSchema } from './schema';

const subscriptionPort = 3100;

// the Meteor GraphQL server is an Express server
const graphQLServer = express();

graphQLServer.use(cors());
graphQLServer.use(bodyParser.urlencoded({ extended: true }));

graphQLServer.use(
'/graphql',
Expand All @@ -33,20 +34,31 @@ graphQLServer.use(
})
);

graphQLServer.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
subscriptionsEndpoint: 'ws://localhost:3000/subscriptions'
}));

new SubscriptionServer({
schema: executableSchema,
execute,
subscribe,
onConnect: (connectionParams) => ({ authToken: connectionParams.Authorization })
},
{
path: '/subscriptions',
server: WebApp.httpServer
graphQLServer.use(
'/graphiql',
graphiqlExpress({
endpointURL: '/graphql',
subscriptionsEndpoint: `ws://localhost:${ subscriptionPort }`
})
);

function startSubscriptionServer() {
SubscriptionServer.create({
schema: executableSchema,
execute,
subscribe,
onConnect: (connectionParams) => ({ authToken: connectionParams.Authorization })
},
{
port: subscriptionPort,
host: process.env.BIND_IP || '0.0.0.0'
});

console.log('GraphQL Subscription server runs on port:', subscriptionPort);
}

WebApp.onListening(() => {
startSubscriptionServer();
});

// this binds the specified paths to the Express server running Apollo + GraphiQL
Expand Down

0 comments on commit 161f9a1

Please sign in to comment.