Skip to content

Commit

Permalink
Use express.Router (#403)
Browse files Browse the repository at this point in the history
* use express.Router

* use express.Router() for app-schema-router

* middleware is an array

* use express.Router()

* use express.Router()
  • Loading branch information
dcousens committed Jun 21, 2023
1 parent a35dba0 commit 68c5ded
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/app-graphql-playground/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class GraphQLPlaygroundApp {
prepareMiddleware({ dev }) {
const graphiqlPath = this._graphiqlPath;
const apiPath = this._apiPath;
const app = express();
const app = express.Router();
if (dev && falsey(process.env.DISABLE_LOGGING)) {
// NOTE: Must come before we setup the GraphQL API
const devQueryPath = `${graphiqlPath}/go`;
Expand Down
2 changes: 1 addition & 1 deletion packages/app-graphql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GraphQLApp {
});
const apiPath = this._apiPath;
const graphiqlPath = this._graphiqlPath;
const app = express();
const app = express.Router();

if (dev && graphiqlPath) {
// This is a convenience to make the out of the box experience slightly simpler.
Expand Down
15 changes: 9 additions & 6 deletions packages/app-schema-router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ class SchemaRouterApp {
* @return Array<middlewares>
*/
prepareMiddleware({ keystone, dev }) {
const attachRouterId = express().use(this.apiPath, (req, res, next) => {
req.routerId = this.routerFn(req, res);
next();
});

const conditionalApps = [
Object.entries(this.apps).map(([routerId, app]) =>
conditionalMiddleware({ routerId, middleware: app.prepareMiddleware({ keystone, dev }) })
),
];

return [attachRouterId, ...conditionalApps];
return express
.Router()
.use(this.apiPath, [
(req, res, next) => {
req.routerId = this.routerFn(req, res);
next();
},
...conditionalApps
]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/app-static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class StaticApp {
}

prepareMiddleware({ dev, distDir }) {
const app = express();
const app = express.Router();
const folderToServe = dev ? this._src : getDistDir(this._src, distDir);
app.use(this._path, express.static(folderToServe));
if (this._fallback) {
Expand Down

0 comments on commit 68c5ded

Please sign in to comment.