Skip to content

Commit

Permalink
Remove keystone prepare (#5226)
Browse files Browse the repository at this point in the history
* Remove legacy keystone.prepare() method

* Add changeset
  • Loading branch information
timleslie authored Mar 26, 2021
1 parent cbba50e commit 45272d0
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 206 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-timers-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone-legacy': major
---

Removed legacy method `keystone.prepare()`.
13 changes: 0 additions & 13 deletions packages/keystone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,6 @@ For more information about the first four arguments, please see the [Apollo docs
- The `access` argument for `types`, `queries`, and `mutations` are all either boolean values which are used at schema generation time to include or exclude the item from the schema, or a function which must return boolean.
- See the [Access control API](https://www.keystonejs.com/api/access-control#custom-schema-access-control) docs for more details.

### `prepare(config)`

Manually prepare middlewares. Returns a promise representing the processed middlewares. They are available as an array through the `middlewares` property of the returned object.

#### Usage

```javascript
const { middlewares } = await keystone.prepare({
apps,
dev: process.env.NODE_ENV !== 'production',
});
```

#### Config

| Option | Type | default | Description |
Expand Down
47 changes: 0 additions & 47 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const { ApolloServer, gql } = require('apollo-server-express');
const flattenDeep = require('lodash.flattendeep');
const memoize = require('micro-memoize');
const falsey = require('falsey');
const createCorsMiddleware = require('cors');
const { execute } = require('graphql');
const { GraphQLUpload } = require('graphql-upload');
const { objMerge, flatten, unique, filterValues } = require('@keystone-next/utils-legacy');
Expand Down Expand Up @@ -453,48 +450,4 @@ module.exports = class Keystone {
o => Object.entries(o).length > 0
);
}

async _prepareMiddlewares({ dev, apps, distDir, pinoOptions, cors }) {
return flattenDeep([
// Used by other middlewares such as authentication strategies. Important
// to be first so the methods added to `req` are available further down
// the request pipeline.
// TODO: set up a session test rig (maybe by wrapping an in-memory store)
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)
// We do this first to avoid it conflicting with any catch-all routes the
// user may have specified
...this.registeredTypes,
...flattenDeep(
Object.values(this.auth).map(authStrategies => Object.values(authStrategies))
),
...apps,
]
.filter(({ prepareMiddleware } = {}) => !!prepareMiddleware)
.map(app => app.prepareMiddleware({ keystone: this, dev, distDir: distDir || 'dist' }))
)),
]).filter(middleware => !!middleware);
}

async prepare({
dev = false,
apps = [],
distDir,
pinoOptions,
cors = { origin: true, credentials: true },
} = {}) {
this.createApolloServer({ schemaName: 'internal' });
const middlewares = await this._prepareMiddlewares({ dev, apps, distDir, pinoOptions, cors });
// These function can't be called after prepare(), so make them throw an error from now on.
['createList'].forEach(f => {
this[f] = () => {
throw new Error(`keystone.${f} must be called before keystone.prepare()`);
};
});

return { middlewares };
}
};
5 changes: 1 addition & 4 deletions packages/keystone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"@keystone-next/utils-legacy": "^7.0.0",
"apollo-errors": "^1.9.0",
"apollo-server-express": "^2.21.2",
"cors": "^2.8.5",
"cuid": "^2.1.8",
"ensure-error": "^3.0.1",
"express": "^4.17.1",
Expand All @@ -21,7 +20,6 @@
"graphql": "^15.5.0",
"graphql-type-json": "^0.3.2",
"graphql-upload": "^11.0.0",
"lodash.flattendeep": "^4.4.0",
"micro-memoize": "^4.0.9",
"p-waterfall": "^2.1.1",
"pino": "^6.11.2",
Expand All @@ -31,8 +29,7 @@
},
"devDependencies": {
"@keystone-next/fields-legacy": "^23.1.0",
"@keystone-next/test-utils-legacy": "*",
"p-is-promise": "^3.0.0"
"@keystone-next/test-utils-legacy": "*"
},
"repository": "https://github.com/keystonejs/keystone/tree/master/packages/keystone"
}
132 changes: 0 additions & 132 deletions packages/keystone/tests/Keystone.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const isPromise = require('p-is-promise');
const Keystone = require('../lib/Keystone');
const { List } = require('../lib/ListTypes');
class MockFieldAdapter {}
Expand Down Expand Up @@ -147,134 +146,3 @@ describe('Keystone.createList()', () => {
expect(keystone.lists['Comment'].fields.length).toEqual(3); // id, heading, content
});
});

describe('keystone.prepare()', () => {
test('returns a Promise', () => {
const config = {
adapter: new MockAdapter(),
};
const keystone = new Keystone(config);

// NOTE: We add a `.catch()` to silence the "unhandled promise rejection"
// warning
expect(isPromise(keystone.prepare().catch(() => {}))).toBeTruthy();
});

test('returns the middlewares array', async () => {
const config = {
adapter: new MockAdapter(),
};
const keystone = new Keystone(config);
const { middlewares } = await keystone.prepare();

expect(middlewares).toBeInstanceOf(Array);
});

test('handles apps:undefined', async () => {
const config = {
adapter: new MockAdapter(),
};
const keystone = new Keystone(config);
const { middlewares } = await keystone.prepare({ apps: undefined });

expect(middlewares).toBeInstanceOf(Array);
});

test('handles apps:[]', async () => {
const config = {
adapter: new MockAdapter(),
};
const keystone = new Keystone(config);
const { middlewares } = await keystone.prepare({ apps: [] });

expect(middlewares).toBeInstanceOf(Array);
});

test('Handles apps without a `prepareMiddleware`', async () => {
const config = {
adapter: new MockAdapter(),
};
const keystone = new Keystone(config);
// For less-brittle tests, we grab the list of middlewares when prepare is
// given no apps, then compare it with the one that did.
const { middlewares: defaultMiddlewares } = await keystone.prepare();
const { middlewares } = await keystone.prepare({ apps: [{ foo: 'bar' }] });

expect(middlewares).toBeInstanceOf(Array);
expect(middlewares).toHaveLength(defaultMiddlewares.length);
});

test('filters out null middleware results', async () => {
const config = {
adapter: new MockAdapter(),
};
const keystone = new Keystone(config);
// For less-brittle tests, we grab the list of middlewares when prepare is
// given no apps, then compare it with the one that did.
const { middlewares: defaultMiddlewares } = await keystone.prepare();
const { middlewares } = await keystone.prepare({ apps: [{ prepareMiddleware: () => {} }] });

expect(middlewares).toBeInstanceOf(Array);
expect(middlewares).toHaveLength(defaultMiddlewares.length);
});

test('filters out empty middleware arrays', async () => {
const config = {
adapter: new MockAdapter(),
};
const keystone = new Keystone(config);
// For less-brittle tests, we grab the list of middlewares when prepare is
// given no apps, then compare it with the one that did.
const { middlewares: defaultMiddlewares } = await keystone.prepare();
const { middlewares } = await keystone.prepare({ apps: [{ prepareMiddleware: () => [] }] });

expect(middlewares).toBeInstanceOf(Array);
expect(middlewares).toHaveLength(defaultMiddlewares.length);
});

test('returns middlewares', async () => {
const config = {
adapter: new MockAdapter(),
};
const middleware = jest.fn(() => {});
const keystone = new Keystone(config);
const { middlewares } = await keystone.prepare({
apps: [{ prepareMiddleware: () => middleware }],
});

expect(middlewares).toBeInstanceOf(Array);
expect(middlewares).toEqual(expect.arrayContaining([middleware]));
});

test('flattens deeply nested middlewares', async () => {
const config = {
adapter: new MockAdapter(),
};
const keystone = new Keystone(config);
const fn0 = jest.fn(() => {});
const fn1 = jest.fn(() => {});
const fn2 = jest.fn(() => {});
const { middlewares } = await keystone.prepare({
apps: [{ prepareMiddleware: () => [[fn0, fn1], fn2] }],
});

expect(middlewares).toBeInstanceOf(Array);
expect(middlewares).toEqual(expect.arrayContaining([fn0, fn1, fn2]));
});

test('should create `internal` GraphQL schema instance', async () => {
const config = {
adapter: new MockAdapter(),
};
const keystone = new Keystone(config);

// Prepare middlewares
await keystone.prepare();

expect(keystone._schemas['internal']).not.toBe(null);
});

test('orders field middlewares before app middlewares', async () => {});

test('calls prepareMiddleware with correct params', async () => {});
});
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9142,11 +9142,6 @@ lodash.flatten@^4.4.0:
resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f"
integrity sha1-8xwiIlqWMtK7+OSt2+8kCqdlph8=

lodash.flattendeep@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz#fb030917f86a3134e5bc9bec0d69e0013ddfedb2"
integrity sha1-+wMJF/hqMTTlvJvsDWngAT3f7bI=

lodash.groupby@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.groupby/-/lodash.groupby-4.6.0.tgz#0b08a1dcf68397c397855c3239783832df7403d1"
Expand Down Expand Up @@ -10476,11 +10471,6 @@ p-finally@^2.0.0:
resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-2.0.1.tgz#bd6fcaa9c559a096b680806f4d657b3f0f240561"
integrity sha512-vpm09aKwq6H9phqRQzecoDpD8TmVyGw70qmWlyq5onxY7tqyTTFVvxMykxQSQKILBSFlbXpypIw2T1Ml7+DDtw==

p-is-promise@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/p-is-promise/-/p-is-promise-3.0.0.tgz#58e78c7dfe2e163cf2a04ff869e7c1dba64a5971"
integrity sha512-Wo8VsW4IRQSKVXsJCn7TomUaVtyfjVDn3nUP7kE967BQk0CwFpdbZs0X0uk5sW9mkBa9eNM7hCMaG93WUAwxYQ==

p-lazy@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/p-lazy/-/p-lazy-3.1.0.tgz#4b1e40482b7ee87853abbcf31824ff64e1816d61"
Expand Down

1 comment on commit 45272d0

@vercel
Copy link

@vercel vercel bot commented on 45272d0 Mar 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.