Skip to content

Commit

Permalink
Remove legacy keystone.createContext
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Mar 26, 2021
1 parent b8cd13f commit c69acba
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 125 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-lamps-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone-legacy': major
---

Removed legacy method `keystone.createContext()`.
119 changes: 0 additions & 119 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
const { gql } = require('apollo-server-express');
const memoize = require('micro-memoize');
const { execute } = require('graphql');
const { GraphQLUpload } = require('graphql-upload');
const { objMerge, flatten, unique, filterValues } = require('@keystone-next/utils-legacy');
const {
validateFieldAccessControl,
validateListAccessControl,
validateAuthAccessControl,
} = require('@keystone-next/access-control-legacy');

const { List } = require('../ListTypes');
const { ListCRUDProvider } = require('../providers');
Expand Down Expand Up @@ -45,119 +39,6 @@ module.exports = class Keystone {
}
}

_getAccessControlContext({ schemaName, authentication, skipAccessControl }) {
if (skipAccessControl) {
return {
getListAccessControlForUser: () => true,
getFieldAccessControlForUser: () => true,
getAuthAccessControlForUser: () => true,
};
}
// memoizing to avoid requests that hit the same type multiple times.
// We do it within the request callback so we can resolve it based on the
// request info (like who's logged in right now, etc)
const getListAccessControlForUser = memoize(
async (
access,
listKey,
originalInput,
operation,
{ gqlName, itemId, itemIds, context } = {}
) => {
return validateListAccessControl({
access: access[schemaName],
args: {
originalInput,
operation,
authentication: authentication && authentication.item ? authentication : {},
listKey,
gqlName,
itemId,
itemIds,
context,
},
});
},
{ isPromise: true }
);

const getFieldAccessControlForUser = memoize(
async (
access,
listKey,
fieldKey,
originalInput,
existingItem,
operation,
{ gqlName, itemId, itemIds, context } = {}
) => {
return validateFieldAccessControl({
access: access[schemaName],
args: {
originalInput,
existingItem,
operation,
authentication: authentication && authentication.item ? authentication : {},
fieldKey,
listKey,
gqlName,
itemId,
itemIds,
context,
},
});
},
{ isPromise: true }
);

const getAuthAccessControlForUser = memoize(
async (access, listKey, { gqlName, context } = {}) => {
return validateAuthAccessControl({
access: access[schemaName],
args: {
authentication: authentication && authentication.item ? authentication : {},
listKey,
gqlName,
context,
operation: 'auth',
},
});
},
{ isPromise: true }
);

return {
getListAccessControlForUser,
getFieldAccessControlForUser,
getAuthAccessControlForUser,
};
}

createContext({ schemaName = 'public', authentication = {}, skipAccessControl = false } = {}) {
const context = {
schemaName,
authedItem: authentication.item,
authedListKey: authentication.listKey,
...this._getAccessControlContext({ schemaName, authentication, skipAccessControl }),
totalResults: 0,
maxTotalResults: this.queryLimits.maxTotalResults,
};
// Locally bind the values we use as defaults into an object to make
// JS behave the way we want.
const defaults = { schemaName, authentication, skipAccessControl, context };
context.createContext = ({
schemaName = defaults.schemaName,
authentication = defaults.authentication,
skipAccessControl = defaults.skipAccessControl,
} = {}) => this.createContext({ schemaName, authentication, skipAccessControl });
context.sudo = () =>
this.createContext({ schemaName, authentication, skipAccessControl: true });
context.executeGraphQL = ({ context = defaults.context, query, variables }) =>
this.executeGraphQL({ context, query, variables });
context.gqlNames = listKey => this.lists[listKey].gqlNames;
return context;
}

executeGraphQL({ context, query, variables }) {
if (!context) {
context = this.createContext({});
Expand Down
1 change: 0 additions & 1 deletion packages/keystone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"graphql": "^15.5.0",
"graphql-type-json": "^0.3.2",
"graphql-upload": "^11.0.0",
"micro-memoize": "^4.0.9",
"p-waterfall": "^2.1.1",
"pino": "^6.11.2",
"pluralize": "^8.0.0",
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9625,11 +9625,6 @@ methods@^1.1.2, methods@~1.1.2:
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
integrity sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=

micro-memoize@^4.0.9:
version "4.0.9"
resolved "https://registry.yarnpkg.com/micro-memoize/-/micro-memoize-4.0.9.tgz#b44a38c9dffbee1cefc2fd139bc8947952268b62"
integrity sha512-Z2uZi/IUMGQDCXASdujXRqrXXEwSY0XffUrAOllhqzQI3wpUyZbiZTiE2JuYC0HSG2G7DbCS5jZmsEKEGZuemg==

micro@^9.3.2:
version "9.3.4"
resolved "https://registry.yarnpkg.com/micro/-/micro-9.3.4.tgz#745a494e53c8916f64fb6a729f8cbf2a506b35ad"
Expand Down

0 comments on commit c69acba

Please sign in to comment.