Skip to content

Commit

Permalink
Refactor the appVersion functionality into a Provider (keystonejs#2433)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Feb 25, 2020
1 parent 3abc588 commit 8bdbb11
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-carrots-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/keystone': patch
---

Added a VersionProvider to generate the `appVersion` graphQL query.
19 changes: 2 additions & 17 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const {
const List = require('../List');
const { DEFAULT_DIST_DIR } = require('../../constants');
const { AccessDeniedError } = require('../List/graphqlErrors');
const { VersionProvider } = require('../providers');

const debugGraphQLSchemas = () => !!process.env.DEBUG_GRAPHQL_SCHEMAS;

Expand Down Expand Up @@ -81,12 +82,7 @@ module.exports = class Keystone {
this.registeredTypes = new Set();
this._schemaNames = schemaNames;
this.appVersion = appVersion;
this.appVersion.access = parseCustomAccess({
access: this.appVersion.access,
schemaNames: this._schemaNames,
defaultAccess: true,
});
this._providers = [];
this._providers = [new VersionProvider({ appVersion, schemaNames })];

if (adapters) {
this.adapters = adapters;
Expand Down Expand Up @@ -488,12 +484,6 @@ module.exports = class Keystone {
${unique(
flatten([
...firstClassLists.map(list => list.getGqlQueries({ schemaName })),
this.appVersion.access[schemaName]
? [
`"""The version of the Keystone application serving this API."""
appVersion: String`,
]
: [],
this._extendedQueries
.filter(({ access }) => access[schemaName])
.map(({ schema }) => schema),
Expand Down Expand Up @@ -631,11 +621,6 @@ module.exports = class Keystone {
// shouldn't be able to override list-level queries
...objMerge(firstClassLists.map(list => list.gqlAuxQueryResolvers())),
...objMerge(firstClassLists.map(list => list.gqlQueryResolvers({ schemaName }))),
...objMerge(
this.appVersion.access[schemaName]
? [{ appVersion: () => this.appVersion.version }]
: []
),
// And the Keystone meta queries must always be available
_ksListsMeta: (_, args, context) =>
this.listsArray
Expand Down
4 changes: 3 additions & 1 deletion packages/keystone/lib/providers/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { VersionProvider } = require('./version');

// The GraphQL Provider Framework expects to see classes with the following API:
//
// class Provider {
Expand All @@ -24,4 +26,4 @@
// }
// }

module.exports = {};
module.exports = { VersionProvider };
41 changes: 41 additions & 0 deletions packages/keystone/lib/providers/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { parseCustomAccess } = require('@keystonejs/access-control');

class VersionProvider {
constructor({ appVersion, schemaNames }) {
appVersion.access = parseCustomAccess({
access: appVersion.access,
schemaNames,
defaultAccess: true,
});
this._appVersion = appVersion;
}

getTypes({}) {
return [];
}
getQueries({ schemaName }) {
return this._appVersion.access[schemaName]
? [
`"""The version of the Keystone application serving this API."""
appVersion: String`,
]
: [];
}
getMutations({}) {
return [];
}

getTypeResolvers({}) {
return {};
}
getQueryResolvers({ schemaName }) {
return this._appVersion.access[schemaName]
? { appVersion: () => this._appVersion.version }
: {};
}
getMutationResolvers({}) {
return {};
}
}

module.exports = { VersionProvider };

0 comments on commit 8bdbb11

Please sign in to comment.