Skip to content

Commit

Permalink
Move cors and pino logger middlewares to Keystone (#1618)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Sep 11, 2019
1 parent 76c3efa commit f8ad097
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 20 deletions.
77 changes: 77 additions & 0 deletions .changeset/three-mirrors-beg/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"releases": [
{ "name": "@keystone-alpha/app-graphql", "type": "major" },
{ "name": "@keystone-alpha/keystone", "type": "minor" }
],
"dependents": [
{
"name": "@keystone-alpha/api-tests",
"type": "patch",
"dependencies": [
"@keystone-alpha/test-utils",
"@keystone-alpha/app-graphql",
"@keystone-alpha/keystone"
]
},
{
"name": "@keystone-alpha/demo-project-blog",
"type": "patch",
"dependencies": ["@keystone-alpha/app-graphql", "@keystone-alpha/keystone"]
},
{
"name": "@keystone-alpha/demo-project-meetup",
"type": "patch",
"dependencies": ["@keystone-alpha/app-graphql", "@keystone-alpha/keystone"]
},
{
"name": "@keystone-alpha/demo-project-todo",
"type": "patch",
"dependencies": ["@keystone-alpha/app-graphql", "@keystone-alpha/keystone"]
},
{
"name": "@keystone-alpha/example-projects-blank",
"type": "patch",
"dependencies": ["@keystone-alpha/app-graphql", "@keystone-alpha/keystone"]
},
{
"name": "@keystone-alpha/example-projects-starter",
"type": "patch",
"dependencies": ["@keystone-alpha/app-graphql", "@keystone-alpha/keystone"]
},
{
"name": "@keystone-alpha/example-projects-todo",
"type": "patch",
"dependencies": ["@keystone-alpha/app-graphql", "@keystone-alpha/keystone"]
},
{
"name": "@keystone-alpha/test-utils",
"type": "patch",
"dependencies": ["@keystone-alpha/app-graphql", "@keystone-alpha/keystone"]
},
{
"name": "@keystone-alpha/cypress-project-access-control",
"type": "patch",
"dependencies": ["@keystone-alpha/app-graphql", "@keystone-alpha/keystone"]
},
{
"name": "@keystone-alpha/cypress-project-basic",
"type": "patch",
"dependencies": ["@keystone-alpha/app-graphql", "@keystone-alpha/keystone"]
},
{
"name": "@keystone-alpha/cypress-project-client-validation",
"type": "patch",
"dependencies": ["@keystone-alpha/app-graphql", "@keystone-alpha/keystone"]
},
{
"name": "@keystone-alpha/cypress-project-login",
"type": "patch",
"dependencies": ["@keystone-alpha/app-graphql", "@keystone-alpha/keystone"]
},
{
"name": "@keystone-alpha/cypress-project-social-login",
"type": "patch",
"dependencies": ["@keystone-alpha/app-graphql", "@keystone-alpha/keystone"]
}
]
}
1 change: 1 addition & 0 deletions .changeset/three-mirrors-beg/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The `cors` and `pinoOptions` parameters now live on `keystone.prepare()` rather than `new GraphQLApp()`
25 changes: 10 additions & 15 deletions packages/app-graphql/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
const createCorsMiddleware = require('cors');
const falsey = require('falsey');
const assert = require('nanoassert');
const createGraphQLMiddleware = require('./lib/graphql');
const { createApolloServer } = require('./lib/apolloServer');

class GraphQLApp {
constructor({
cors = { origin: true, credentials: true },
apiPath = '/admin/api',
graphiqlPath = '/admin/graphiql',
schemaName = 'public',
apollo = {},
pinoOptions,
// Deprecated options:
cors,
pinoOptions,
cookieSecret,
sessionStore,
} = {}) {
Expand All @@ -26,11 +24,16 @@ class GraphQLApp {
typeof sessionStore === 'undefined',
'The `sessionStore` option has moved to the Keystone constructor: `new Keystone({ sessionStore: myStore })`'
);

assert(
typeof pinoOptions === 'undefined',
'The `pinoOptions` option has moved to the Keystone.pepare() method: `keystone.prepare({ pinoOptions })`'
);
assert(
typeof cors === 'undefined',
'The `cors` option has moved to the Keystone.pepare() method: `keystone.prepare({ cors })`'
);
this._apiPath = apiPath;
this._graphiqlPath = graphiqlPath;
this._pinoOptions = pinoOptions;
this._cors = cors;
this._apollo = apollo;
this._schemaName = schemaName;
}
Expand All @@ -41,14 +44,6 @@ class GraphQLApp {
prepareMiddleware({ keystone, dev }) {
const middlewares = [];

if (falsey(process.env.DISABLE_LOGGING)) {
middlewares.push(require('express-pino-logger')(this._pinoOptions));
}

if (this._cors) {
middlewares.push(createCorsMiddleware(this._cors));
}

const server = createApolloServer(keystone, this._apollo, this._schemaName, dev);
// GraphQL API always exists independent of any adminUI or Session
// settings We currently make the admin UI public. In the future we want
Expand Down
4 changes: 1 addition & 3 deletions packages/app-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
"apollo-server-express": "^2.9.1",
"body-parser": "^1.18.2",
"chalk": "^2.4.2",
"cors": "^2.8.4",
"cuid": "^2.1.6",
"ensure-error": "^1.0.0",
"express-pino-logger": "^4.0.0",
"falsey": "^1.0.0",
"graphql": "^14.4.2",
"graphql-tag": "^2.10.1",
Expand All @@ -33,4 +31,4 @@
"peerDependencies": {
"express": "^4.17.1"
}
}
}
12 changes: 11 additions & 1 deletion packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const fs = require('fs');
const gql = require('graphql-tag');
const flattenDeep = require('lodash.flattendeep');
const fastMemoize = require('fast-memoize');
const falsey = require('falsey');
const createCorsMiddleware = require('cors');
const { print } = require('graphql/language/printer');
const { graphql } = require('graphql');
const {
Expand Down Expand Up @@ -543,7 +545,13 @@ module.exports = class Keystone {
return mergeRelationships(createdItems, createdRelationships);
}

async prepare({ dev = false, apps = [], distDir } = {}) {
async prepare({
dev = false,
apps = [],
distDir,
pinoOptions,
cors = { origin: true, credentials: true },
} = {}) {
const middlewares = flattenDeep([
// Used by other middlewares such as authentication strategies. Important
// to be first so the methods added to `req` are available further down
Expand All @@ -555,6 +563,8 @@ module.exports = class Keystone {
secureCookies: this._secureCookies,
cookieMaxAge: this._cookieMaxAge,
}),
falsey(process.env.DISABLE_LOGGING) && require('express-pino-logger')(pinoOptions),
cors && createCorsMiddleware(cors),
...(await Promise.all(
[
// Inject any field middlewares (eg; WYSIWIG's static assets)
Expand Down
5 changes: 4 additions & 1 deletion packages/keystone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
"arg": "^4.1.0",
"chalk": "^2.4.2",
"ci-info": "^2.0.0",
"cors": "^2.8.4",
"dev-null": "^0.1.1",
"endent": "^1.3.0",
"express": "^4.17.1",
"express-pino-logger": "^4.0.0",
"fast-memoize": "^2.4.0",
"falsey": "^1.0.0",
"fs-extra": "^7.0.0",
"globby": "^9.1.0",
"graphql": "^14.4.2",
Expand All @@ -44,4 +47,4 @@
"p-is-promise": "^3.0.0",
"tmp": "^0.1.0"
}
}
}

0 comments on commit f8ad097

Please sign in to comment.