Skip to content

Commit

Permalink
Made handling of field-specific config options more consistent (#2919)
Browse files Browse the repository at this point in the history
* Made handling of field-specific config options more consistent
  • Loading branch information
Vultraz authored May 7, 2020
1 parent babed62 commit e3d46ce
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
8 changes: 8 additions & 0 deletions .changeset/long-wombats-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@keystonejs/demo-custom-fields': patch
'@keystonejs/fields': patch
'@keystonejs/fields-auto-increment': patch
'@keystonejs/cypress-project-basic': patch
---

Made handling of field-specific config options more consistent.
7 changes: 6 additions & 1 deletion demo-projects/custom-fields/fields/Stars/Implementation.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const { Integer } = require('@keystonejs/fields');

class Stars extends Integer.implementation {
constructor(path, { starCount = 5 }) {
super(...arguments);
this.starCount = starCount;
}

extendAdminMeta(meta) {
return { ...meta, starCount: this.config.starCount || 5 };
return { ...meta, starCount: this.starCount };
}
}

Expand Down
23 changes: 13 additions & 10 deletions packages/fields-auto-increment/src/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ import { Implementation } from '@keystonejs/fields';
import { KnexFieldAdapter } from '@keystonejs/adapter-knex';

export class AutoIncrementImplementation extends Implementation {
constructor(path, config = {}, context = {}) {
constructor(path, { gqlType, isUnique = true, access = {}, ...config } = {}, context = {}) {
// Apply some field type defaults before we hand off to super; see README.md
if (typeof config.isUnique === 'undefined') config.isUnique = true;
if (typeof config.access === 'undefined') config.access = {};
if (typeof config.access === 'object') {
config.access = { create: false, update: false, delete: false, ...config.access };
if (typeof access === 'object') {
access = { create: false, update: false, delete: false, ...access };
}

// The base implementation takes care of everything else
super(path, config, context);
super(
path,
{
...config,
isUnique,
access,
},
context
);

// If no valid gqlType is supplied, default based on whether or not we're the primary key
const gqlTypeDefault = this.isPrimaryKey ? 'ID' : 'Int';
this.gqlType = ['ID', 'Int'].includes(this.config.gqlType)
? this.config.gqlType
: gqlTypeDefault;
this.gqlType = ['ID', 'Int'].includes(gqlType) ? gqlType : this.isPrimaryKey ? 'ID' : 'Int';
}

gqlOutputFields() {
Expand Down
18 changes: 12 additions & 6 deletions packages/fields/src/types/Virtual/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@ import { KnexFieldAdapter } from '@keystonejs/adapter-knex';
import { parseFieldAccess } from '@keystonejs/access-control';

export class Virtual extends Implementation {
constructor() {
constructor(
path,
{ resolver, graphQLReturnType = 'String', graphQLReturnFragment = '', extendGraphQLTypes = [] }
) {
super(...arguments);
this.resolver = resolver;
this.graphQLReturnType = graphQLReturnType;
this.graphQLReturnFragment = graphQLReturnFragment;
this.extendGraphQLTypes = extendGraphQLTypes;
}

gqlOutputFields() {
return [`${this.path}: ${this.config.graphQLReturnType || `String`}`];
return [`${this.path}: ${this.graphQLReturnType}`];
}
getGqlAuxTypes() {
return this.config.extendGraphQLTypes || [];
return this.extendGraphQLTypes;
}
gqlOutputFieldResolvers() {
return { [`${this.path}`]: this.config.resolver };
return { [`${this.path}`]: this.resolver };
}
gqlQueryInputFields() {
return [];
}
extendAdminMeta(meta) {
return {
...meta,
isOrderable: false,
graphQLSelection: this.config.graphQLReturnFragment || '',
graphQLSelection: this.graphQLReturnFragment,
isReadOnly: true,
};
}
Expand Down
7 changes: 6 additions & 1 deletion test-projects/basic/custom-fields/Stars/Implementation.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const { Integer } = require('@keystonejs/fields');

class Stars extends Integer.implementation {
constructor(path, { starCount = 5 }) {
super(...arguments);
this.starCount = starCount;
}

extendAdminMeta(meta) {
return { ...meta, starCount: this.config.starCount || 5 };
return { ...meta, starCount: this.starCount };
}
}

Expand Down

0 comments on commit e3d46ce

Please sign in to comment.