glennreyes/graphpack

Support applying server middlewares

glennreyes opened this issue · 4 comments

We need to be able to import the server instance to apply middlewares on it.

For example, in src/config.js adding following:

import { server } from 'graphpack';

server.applyMiddleware(/* ... */);

More details about applying middlewares in the Apollo Server docs.

@glennreyes For this to happen, we need to expose a start mechanism instead of auto starting the server. This way user can start the server on demand once all the middlewares are applied.

This also scales well for other server properties we might need to support later.

import { server } from 'graphpack';

server.applyMiddlewares(/* ...middlewares */);
server.start();

@farskid Yep exactly. I'm okay with that as long as we don't have to do it when there's no middleware set up. Any idea or preference how to design that api? Currenty thinking of:

// src/config.js
export default {
  applyMiddleware: { app },
  introspection: true,
  playground: true, 
}

@glennreyes I think it's good as it matches with Apollo docs. I'm up with consistency too.

@farskid @glennreyes have you guys tested this? can't get it working

this is my graphpack.config.js:

const express = require('express');
const app = express();

app.get('/version', (req, res) => {
  res.send('hello world');
});


module.exports = (mode) => {
  const IS_DEV = mode !== 'production';
  return {
    server: {
      introspection: IS_DEV,
      playground: IS_DEV,
      applyMiddleware: { app }
    }
  }
};

It ends up with following error:

(node:14783) UnhandledPromiseRejectionWarning: ReferenceError: applyMiddleware is not defined
    at startServer (/Users/turan/.../build/index.js:273:28)
    at <anonymous>
(node:14783) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled w
ith .catch(). (rejection id: 1)
(node:14783) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I think something might be wrong with this line:

server.applyMiddleware(applyMiddleware);

Or am I doing something wrong in my config?