From e9f3c42d5b9d42872cecbd18fbe9bf9d7d53ed82 Mon Sep 17 00:00:00 2001 From: Mitchell Hamilton Date: Tue, 10 Aug 2021 14:39:58 +1000 Subject: [PATCH] Remove `gqlType` option on `autoIncrement` field type (#6280) --- .changeset/beige-dancers-remember.md | 5 ++ .../fields/src/types/autoIncrement/index.ts | 57 +++--------------- .../autoIncrement/tests/test-fixtures.ts | 58 +++++-------------- 3 files changed, 28 insertions(+), 92 deletions(-) create mode 100644 .changeset/beige-dancers-remember.md diff --git a/.changeset/beige-dancers-remember.md b/.changeset/beige-dancers-remember.md new file mode 100644 index 00000000000..92ad52629be --- /dev/null +++ b/.changeset/beige-dancers-remember.md @@ -0,0 +1,5 @@ +--- +'@keystone-next/fields': major +--- + +Removed `gqlType` option to `autoIncrement` field type. The field type will now always be represented with an `Int` in GraphQL diff --git a/packages/fields/src/types/autoIncrement/index.ts b/packages/fields/src/types/autoIncrement/index.ts index ceb6ef68e0f..fe3f0421daf 100644 --- a/packages/fields/src/types/autoIncrement/index.ts +++ b/packages/fields/src/types/autoIncrement/index.ts @@ -17,7 +17,6 @@ export type AutoIncrementFieldConfig = {}): FieldTypeFunc => meta => { - const type = meta.fieldKey === 'id' || gqlType === 'ID' ? schema.ID : schema.Int; const __legacy = { isRequired, defaultValue, filters: { fields: { - ...legacyFilters.fields.equalityInputFields(meta.fieldKey, type), - ...legacyFilters.fields.orderingInputFields(meta.fieldKey, type), - ...legacyFilters.fields.inInputFields(meta.fieldKey, type), + ...legacyFilters.fields.equalityInputFields(meta.fieldKey, schema.Int), + ...legacyFilters.fields.orderingInputFields(meta.fieldKey, schema.Int), + ...legacyFilters.fields.inInputFields(meta.fieldKey, schema.Int), }, impls: { ...equalityConditions(meta.fieldKey, x => Number(x) || -1), @@ -47,40 +44,6 @@ export const autoIncrement = }, }, }; - if (meta.fieldKey === 'id') { - return fieldType({ - kind: 'scalar', - mode: 'required', - scalar: 'Int', - default: { kind: 'autoincrement' }, - })({ - ...config, - input: { - // TODO: fix the fact that TS did not catch that a resolver is needed here - uniqueWhere: { - arg: schema.arg({ type }), - resolve(value) { - return Number(value); - }, - }, - orderBy: { arg: schema.arg({ type: orderDirectionEnum }) }, - }, - output: schema.field({ - type: schema.nonNull(schema.ID), - resolve({ value }) { - return value.toString(); - }, - }), - views: resolveView('integer/views'), - __legacy, - }); - } - const inputResolver = (val: number | string | null | undefined) => { - if (val == null) { - return val; - } - return Number(val); - }; return fieldType({ kind: 'scalar', mode: 'optional', @@ -90,18 +53,12 @@ export const autoIncrement = })({ ...config, input: { - uniqueWhere: isUnique ? { arg: schema.arg({ type }), resolve: x => Number(x) } : undefined, - create: { arg: schema.arg({ type }), resolve: inputResolver }, - update: { arg: schema.arg({ type }), resolve: inputResolver }, + uniqueWhere: isUnique ? { arg: schema.arg({ type: schema.Int }) } : undefined, + create: { arg: schema.arg({ type: schema.Int }) }, + update: { arg: schema.arg({ type: schema.Int }) }, orderBy: { arg: schema.arg({ type: orderDirectionEnum }) }, }, - output: schema.field({ - type, - resolve({ value }) { - if (value === null) return null; - return type === schema.ID ? value.toString() : value; - }, - }), + output: schema.field({ type: schema.Int }), views: resolveView('integer/views'), __legacy, }); diff --git a/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts b/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts index 80796704315..29ad4532694 100644 --- a/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts +++ b/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts @@ -2,13 +2,10 @@ import { KeystoneContext } from '@keystone-next/types'; import { text } from '../../text'; import { autoIncrement } from '..'; -type MatrixValue = typeof testMatrix[number]; - export const name = 'AutoIncrement'; export const typeFunction = autoIncrement; -export const testMatrix = ['ID', 'Int'] as const; -export const exampleValue = (matrixValue: MatrixValue) => (matrixValue === 'ID' ? '35' : 35); -export const exampleValue2 = (matrixValue: MatrixValue) => (matrixValue === 'ID' ? '36' : 36); +export const exampleValue = () => 35; +export const exampleValue2 = () => 36; export const supportsUnique = true; export const fieldName = 'orderNumber'; export const skipCreateTest = false; @@ -16,21 +13,9 @@ export const skipUpdateTest = true; export const unSupportedAdapterList = ['sqlite']; -// Be default, `AutoIncrement` are read-only. But for `isRequired` test purpose, we need to bypass these restrictions. -export const fieldConfig = (matrixValue: MatrixValue) => ({ - gqlType: matrixValue, - access: { create: true, update: true }, -}); - -export const getTestFields = (matrixValue: MatrixValue) => ({ +export const getTestFields = () => ({ name: text(), - orderNumber: autoIncrement({ - // The gqlType argument is not currently available on the type. - // This will be reviewed when we do our full field type API review - // @ts-ignore - gqlType: matrixValue, - access: { create: true }, - }), + orderNumber: autoIncrement(), }); export const initItems = () => { @@ -45,32 +30,21 @@ export const initItems = () => { ]; }; -export const storedValues = (matrixValue: MatrixValue) => - matrixValue === 'ID' - ? [ - { name: 'product1', orderNumber: '1' }, - { name: 'product2', orderNumber: '2' }, - { name: 'product3', orderNumber: '3' }, - { name: 'product4', orderNumber: '4' }, - { name: 'product5', orderNumber: '5' }, - { name: 'product6', orderNumber: '6' }, - { name: 'product7', orderNumber: '7' }, - ] - : [ - { name: 'product1', orderNumber: 1 }, - { name: 'product2', orderNumber: 2 }, - { name: 'product3', orderNumber: 3 }, - { name: 'product4', orderNumber: 4 }, - { name: 'product5', orderNumber: 5 }, - { name: 'product6', orderNumber: 6 }, - { name: 'product7', orderNumber: 7 }, - ]; +export const storedValues = () => [ + { name: 'product1', orderNumber: 1 }, + { name: 'product2', orderNumber: 2 }, + { name: 'product3', orderNumber: 3 }, + { name: 'product4', orderNumber: 4 }, + { name: 'product5', orderNumber: 5 }, + { name: 'product6', orderNumber: 6 }, + { name: 'product7', orderNumber: 7 }, +]; export const supportedFilters = () => []; -export const filterTests = (withKeystone: (arg: any) => any, matrixValue: MatrixValue) => { - const _storedValues = storedValues(matrixValue); - const _f = matrixValue === 'ID' ? (x: any) => x.toString() : (x: any) => x; +export const filterTests = (withKeystone: (arg: any) => any) => { + const _storedValues = storedValues(); + const _f = (x: any) => x; const match = async (context: KeystoneContext, where: Record, expected: any[]) => expect( await context.lists.Test.findMany({