Skip to content

Commit

Permalink
Remove native type mapping (#3629)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Sep 10, 2020
1 parent bbd6d32 commit 77aa2d7
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 121 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-brooms-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/keystone': major
---

Removed the automatic mapping of native types to keystone field types when defining fields. Keystone will no longer convert `{ type: String }` to `{ type: Text }`, `{ type: Number }` to `{ type: Float }`, or `{ type: Boolean }` to `{ type: Checkbox }`.
7 changes: 4 additions & 3 deletions packages/fields-authed-relationship/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ Great for setting fields like `Post.author` or `Product.owner`, etc.

```js
const { AuthedRelationship } = require('@keystonejs/fields-authed-relationship');
const { Text } = require('@keystonejs/fields');

keystone.createList('User', {
fields: {
name: { type: String },
name: { type: Text },
},
});

Expand All @@ -41,11 +42,11 @@ This example allows "admins" to overwrite the value

```js
const { AuthedRelationship } = require('@keystonejs/fields-authed-relationship');
const { Checkbox } = require('@keystonejs/fields');
const { Checkbox, Text } = require('@keystonejs/fields');

keystone.createList('User', {
fields: {
name: { type: String },
name: { type: Text },
isAdmin: { type: Checkbox, default: false },
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function setupKeystone(adapterName) {
});
keystone.createList('Post', {
fields: {
title: { type: String },
title: { type: Text },
// Automatically set to the currently logged in user on create
author: {
type: AuthedRelationship,
Expand Down
4 changes: 2 additions & 2 deletions packages/fields-location-google/src/test-fixtures.skip.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });

import { Text } from '@keystonejs/fields';
import { LocationGoogle } from './';

export const name = 'LocationGoogle';
Expand All @@ -13,7 +13,7 @@ export const subfieldName = 'googlePlaceID';
export const fieldConfig = () => ({ googleMapsKey: process.env.GOOGLE_API_KEY });

export const getTestFields = () => ({
name: { type: String },
name: { type: Text },
venue: { type, googleMapsKey: process.env.GOOGLE_API_KEY },
});

Expand Down
22 changes: 0 additions & 22 deletions packages/fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,25 +158,3 @@ _Note_: Field level access control does not accept graphQL where clauses.
[HTTP cache hint](https://keystonejs.com/api/create-list#cacheHint) for field.

Only static hints are supported for fields.

## Native type aliases

Keystone allows the use of a few native JavaScript types for fields. They are converted to their Keystone field equivalents at runtime.

| Native type | Field type equivalent |
| ----------- | --------------------- |
| `Boolean` | `Checkbox` |
| `Number` | `Float` |
| `String` | `Text` |

### Usage

```javascript
keystone.createList('Post', {
fields: {
title: {
type: String,
}
}
}
```
6 changes: 1 addition & 5 deletions packages/keystone/lib/ListTypes/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const {
labelToPath,
labelToClass,
opToType,
mapNativeTypeToKeystoneType,
getDefaultLabelResolver,
mapToFields,
} = require('./utils');
Expand Down Expand Up @@ -165,10 +164,7 @@ module.exports = class List {
if (this.fieldsInitialised) return;
this.fieldsInitialised = true;

let sanitisedFieldsConfig = mapKeys(this._fields, (fieldConfig, path) => ({
...fieldConfig,
type: mapNativeTypeToKeystoneType(fieldConfig.type, this.key, path),
}));
let sanitisedFieldsConfig = this._fields;

// Add an 'id' field if none supplied
if (!sanitisedFieldsConfig.id) {
Expand Down
45 changes: 0 additions & 45 deletions packages/keystone/lib/ListTypes/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const { upcase, resolveAllKeys, arrayToObject } = require('@keystonejs/utils');
const { logger } = require('@keystonejs/logger');

const keystoneLogger = logger('keystone');

const preventInvalidUnderscorePrefix = str => str.replace(/^__/, '_');

Expand Down Expand Up @@ -31,47 +28,6 @@ const opToType = {
delete: 'mutation',
};

const mapNativeTypeToKeystoneType = (type, listKey, fieldPath) => {
const { Text, Checkbox, Float } = require('@keystonejs/fields');

const nativeTypeMap = new Map([
[
Boolean,
{
name: 'Boolean',
keystoneType: Checkbox,
},
],
[
String,
{
name: 'String',
keystoneType: Text,
},
],
[
Number,
{
name: 'Number',
keystoneType: Float,
},
],
]);

if (!nativeTypeMap.has(type)) {
return type;
}

const { name, keystoneType } = nativeTypeMap.get(type);

keystoneLogger.warn(
{ nativeType: type, keystoneType, listKey, fieldPath },
`Mapped field ${listKey}.${fieldPath} from native JavaScript type '${name}', to '${keystoneType.type.type}' from the @keystonejs/fields package.`
);

return keystoneType;
};

const getDefaultLabelResolver = labelField => item => {
const value = item[labelField || 'name'];
if (typeof value === 'number') {
Expand All @@ -96,7 +52,6 @@ module.exports = {
labelToPath,
labelToClass,
opToType,
mapNativeTypeToKeystoneType,
getDefaultLabelResolver,
mapToFields,
};
35 changes: 0 additions & 35 deletions packages/keystone/tests/List.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ jest.doMock('@keystonejs/logger', () => ({
const { List } = require('../lib/ListTypes');
const { AccessDeniedError } = require('../lib/ListTypes/graphqlErrors');
const { Text, Checkbox, Float, Relationship, Integer } = require('@keystonejs/fields');
const { getType } = require('@keystonejs/utils');
const path = require('path');

let fieldsPackagePath = path.dirname(require.resolve('@keystonejs/fields/package.json'));
Expand Down Expand Up @@ -1323,37 +1322,3 @@ describe('List Hooks', () => {
});
});
});

describe('Maps from Native JS types to Keystone types', () => {
const adapter = new MockAdapter();

[
{
nativeType: Boolean,
keystoneType: Checkbox,
},
{
nativeType: String,
keystoneType: Text,
},
{
nativeType: Number,
keystoneType: Float,
},
].forEach(({ nativeType, keystoneType }) => {
test(`${getType(nativeType.prototype)} -> ${keystoneType.type}`, () => {
const list = new List(
'Test',
{ fields: { foo: { type: nativeType } } },
{
adapter,
defaultAccess: { list: true, field: true },
registerType: () => {},
schemaNames: ['public'],
}
);
list.initFields();
expect(list.fieldsByPath.foo).toBeInstanceOf(keystoneType.implementation);
});
});
});
16 changes: 8 additions & 8 deletions packages/server-side-graphql-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const { createItem } = require('@keystonejs/server-side-graphql-client');
keystone.createList('User', {
fields: {
name: { type: Text },
email: { type: String },
email: { type: Text },
},
});

Expand Down Expand Up @@ -160,7 +160,7 @@ const { createItems } = require('@keystonejs/server-side-graphql-client');
keystone.createList('User', {
fields: {
name: { type: Text },
email: { type: String },
email: { type: Text },
},
});

Expand Down Expand Up @@ -202,7 +202,7 @@ const { getItem } = require('@keystonejs/server-side-graphql-client');
keystone.createList('User', {
fields: {
name: { type: Text },
email: { type: String },
email: { type: Text },
},
});

Expand Down Expand Up @@ -238,7 +238,7 @@ const { getItems } = require('@keystonejs/server-side-graphql-client');
keystone.createList('User', {
fields: {
name: { type: Text },
email: { type: String },
email: { type: Text },
},
});

Expand Down Expand Up @@ -280,7 +280,7 @@ const { updateItem } = require('@keystonejs/server-side-graphql-client');
keystone.createList('User', {
fields: {
name: { type: Text },
email: { type: String },
email: { type: Text },
},
});

Expand Down Expand Up @@ -316,7 +316,7 @@ const { updateItems } = require('@keystonejs/server-side-graphql-client');
keystone.createList('User', {
fields: {
name: { type: Text },
email: { type: String },
email: { type: Text },
},
});

Expand Down Expand Up @@ -358,7 +358,7 @@ const { deleteItem } = require('@keystonejs/server-side-graphql-client');
keystone.createList('User', {
fields: {
name: { type: Text },
email: { type: String },
email: { type: Text },
},
});

Expand Down Expand Up @@ -389,7 +389,7 @@ const { deleteItems } = require('@keystonejs/server-side-graphql-client');
keystone.createList('User', {
fields: {
name: { type: Text },
email: { type: String },
email: { type: Text },
},
});

Expand Down

0 comments on commit 77aa2d7

Please sign in to comment.