From d6265ab532bacfd6be7a8bbc8926d56eed4a8866 Mon Sep 17 00:00:00 2001 From: mitchellhamilton Date: Mon, 9 Aug 2021 15:41:15 +1000 Subject: [PATCH 1/4] Remove `gqlType` option on `autoIncrement` field type --- .changeset/beige-dancers-remember.md | 5 + .../fields/src/types/autoIncrement/index.ts | 57 +----- .../autoIncrement/tests/test-fixtures.ts | 169 +++--------------- 3 files changed, 33 insertions(+), 198 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..20e89f88deb 100644 --- a/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts +++ b/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts @@ -1,36 +1,18 @@ -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; 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,129 +27,20 @@ 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 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; - const match = async (context: KeystoneContext, where: Record, expected: any[]) => - expect( - await context.lists.Test.findMany({ - where, - orderBy: { name: 'asc' }, - query: 'name orderNumber', - }) - ).toEqual(expected.map(i => _storedValues[i])); - - test( - 'Filter: orderNumber', - withKeystone(({ context }: { context: KeystoneContext }) => - match(context, { orderNumber: _f(1) }, [0]) - ) - ); - - test( - 'Filter: orderNumber_not', - withKeystone(({ context }: { context: KeystoneContext }) => - match(context, { orderNumber_not: _f(1) }, [1, 2, 3, 4, 5, 6]) - ) - ); - - test( - 'Filter: orderNumber_not null', - withKeystone(({ context }: { context: KeystoneContext }) => - match(context, { orderNumber_not: null }, [0, 1, 2, 3, 4, 5, 6]) - ) - ); - - test( - 'Filter: orderNumber_lt', - withKeystone(({ context }: { context: KeystoneContext }) => - match(context, { orderNumber_lt: _f(2) }, [0]) - ) - ); - - test( - 'Filter: orderNumber_lte', - withKeystone(({ context }: { context: KeystoneContext }) => - match(context, { orderNumber_lte: _f(2) }, [0, 1]) - ) - ); - - test( - 'Filter: orderNumber_gt', - withKeystone(({ context }: { context: KeystoneContext }) => - match(context, { orderNumber_gt: _f(2) }, [2, 3, 4, 5, 6]) - ) - ); - - test( - 'Filter: orderNumber_gte', - withKeystone(({ context }: { context: KeystoneContext }) => - match(context, { orderNumber_gte: _f(2) }, [1, 2, 3, 4, 5, 6]) - ) - ); - - test( - 'Filter: orderNumber_in (empty list)', - withKeystone(({ context }: { context: KeystoneContext }) => - match(context, { orderNumber_in: [] }, []) - ) - ); - - test( - 'Filter: orderNumber_not_in (empty list)', - withKeystone(({ context }: { context: KeystoneContext }) => - match(context, { orderNumber_not_in: [] }, [0, 1, 2, 3, 4, 5, 6]) - ) - ); - - test( - 'Filter: orderNumber_in', - withKeystone(({ context }: { context: KeystoneContext }) => - match(context, { orderNumber_in: ([1, 2, 3] as const).map(_f) }, [0, 1, 2]) - ) - ); - - test( - 'Filter: orderNumber_not_in', - withKeystone(({ context }: { context: KeystoneContext }) => - match(context, { orderNumber_not_in: [1, 2, 3].map(_f) }, [3, 4, 5, 6]) - ) - ); - - test( - 'Filter: orderNumber_in null', - withKeystone(({ context }: { context: KeystoneContext }) => - match(context, { orderNumber_in: [null] }, []) - ) - ); - - test( - 'Filter: orderNumber_not_in null', - withKeystone(({ context }: { context: KeystoneContext }) => - match(context, { orderNumber_not_in: [null] }, [0, 1, 2, 3, 4, 5, 6]) - ) - ); -}; +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 = () => [ + 'null_equality', + 'equality', + 'ordering', + 'in_empty_null', + 'in_equal', +]; From f58469d1553abe0e43fb90a6823b5b5e5bd46abd Mon Sep 17 00:00:00 2001 From: mitchellhamilton Date: Mon, 9 Aug 2021 15:49:46 +1000 Subject: [PATCH 2/4] Fix things --- .../fields/src/types/autoIncrement/index.ts | 26 +++---------------- .../autoIncrement/tests/test-fixtures.ts | 2 ++ 2 files changed, 5 insertions(+), 23 deletions(-) diff --git a/packages/fields/src/types/autoIncrement/index.ts b/packages/fields/src/types/autoIncrement/index.ts index fe3f0421daf..b9d830d884b 100644 --- a/packages/fields/src/types/autoIncrement/index.ts +++ b/packages/fields/src/types/autoIncrement/index.ts @@ -38,9 +38,9 @@ export const autoIncrement = ...legacyFilters.fields.inInputFields(meta.fieldKey, schema.Int), }, impls: { - ...equalityConditions(meta.fieldKey, x => Number(x) || -1), - ...legacyFilters.impls.orderingConditions(meta.fieldKey, x => Number(x) || -1), - ...inConditions(meta.fieldKey, x => x.map((xx: any) => Number(xx) || -1)), + ...legacyFilters.impls.equalityConditions(meta.fieldKey), + ...legacyFilters.impls.orderingConditions(meta.fieldKey), + ...legacyFilters.impls.inConditions(meta.fieldKey), }, }, }; @@ -63,23 +63,3 @@ export const autoIncrement = __legacy, }); }; - -function equalityConditions(fieldKey: string, f: (a: any) => any) { - return { - [fieldKey]: (value: T) => ({ [fieldKey]: f(value) }), - [`${fieldKey}_not`]: (value: T) => ({ NOT: { [fieldKey]: f(value) } }), - }; -} - -function inConditions(fieldKey: string, f: (a: any) => any) { - return { - [`${fieldKey}_in`]: (value: (T | null)[]) => - value.includes(null) - ? { [fieldKey]: { in: f(value.filter(x => x !== null)) } } - : { [fieldKey]: { in: f(value) } }, - [`${fieldKey}_not_in`]: (value: (T | null)[]) => - value.includes(null) - ? { AND: [{ NOT: { [fieldKey]: { in: f(value.filter(x => x !== null)) } } }] } - : { NOT: { [fieldKey]: { in: f(value) } } }, - }; -} diff --git a/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts b/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts index 20e89f88deb..98ed3749d3e 100644 --- a/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts +++ b/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts @@ -10,6 +10,8 @@ export const fieldName = 'orderNumber'; export const skipCreateTest = false; export const skipUpdateTest = true; +export const unSupportedAdapterList = ['sqlite']; + export const getTestFields = () => ({ name: text(), orderNumber: autoIncrement(), From 812d4dc28f6fd3a4fe10b069b9308d3f7a31e021 Mon Sep 17 00:00:00 2001 From: mitchellhamilton Date: Mon, 9 Aug 2021 15:59:59 +1000 Subject: [PATCH 3/4] Fix a thing --- .../fields/src/types/autoIncrement/tests/test-fixtures.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts b/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts index 98ed3749d3e..5807d492bb3 100644 --- a/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts +++ b/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts @@ -35,8 +35,8 @@ export const storedValues = () => [ { name: 'product3', orderNumber: 3 }, { name: 'product4', orderNumber: 4 }, { name: 'product5', orderNumber: 5 }, - { name: 'product6', orderNumber: 6 }, - { name: 'product7', orderNumber: 7 }, + { name: 'product6', orderNumber: null }, + { name: 'product7', orderNumber: null }, ]; export const supportedFilters = () => [ From 48c23bdab631f50d30338678e383163618b6a59d Mon Sep 17 00:00:00 2001 From: mitchellhamilton Date: Tue, 10 Aug 2021 14:18:10 +1000 Subject: [PATCH 4/4] Revert some things --- .../fields/src/types/autoIncrement/index.ts | 26 +++- .../autoIncrement/tests/test-fixtures.ts | 117 ++++++++++++++++-- 2 files changed, 131 insertions(+), 12 deletions(-) diff --git a/packages/fields/src/types/autoIncrement/index.ts b/packages/fields/src/types/autoIncrement/index.ts index b9d830d884b..fe3f0421daf 100644 --- a/packages/fields/src/types/autoIncrement/index.ts +++ b/packages/fields/src/types/autoIncrement/index.ts @@ -38,9 +38,9 @@ export const autoIncrement = ...legacyFilters.fields.inInputFields(meta.fieldKey, schema.Int), }, impls: { - ...legacyFilters.impls.equalityConditions(meta.fieldKey), - ...legacyFilters.impls.orderingConditions(meta.fieldKey), - ...legacyFilters.impls.inConditions(meta.fieldKey), + ...equalityConditions(meta.fieldKey, x => Number(x) || -1), + ...legacyFilters.impls.orderingConditions(meta.fieldKey, x => Number(x) || -1), + ...inConditions(meta.fieldKey, x => x.map((xx: any) => Number(xx) || -1)), }, }, }; @@ -63,3 +63,23 @@ export const autoIncrement = __legacy, }); }; + +function equalityConditions(fieldKey: string, f: (a: any) => any) { + return { + [fieldKey]: (value: T) => ({ [fieldKey]: f(value) }), + [`${fieldKey}_not`]: (value: T) => ({ NOT: { [fieldKey]: f(value) } }), + }; +} + +function inConditions(fieldKey: string, f: (a: any) => any) { + return { + [`${fieldKey}_in`]: (value: (T | null)[]) => + value.includes(null) + ? { [fieldKey]: { in: f(value.filter(x => x !== null)) } } + : { [fieldKey]: { in: f(value) } }, + [`${fieldKey}_not_in`]: (value: (T | null)[]) => + value.includes(null) + ? { AND: [{ NOT: { [fieldKey]: { in: f(value.filter(x => x !== null)) } } }] } + : { NOT: { [fieldKey]: { in: f(value) } } }, + }; +} diff --git a/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts b/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts index 5807d492bb3..29ad4532694 100644 --- a/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts +++ b/packages/fields/src/types/autoIncrement/tests/test-fixtures.ts @@ -1,3 +1,4 @@ +import { KeystoneContext } from '@keystone-next/types'; import { text } from '../../text'; import { autoIncrement } from '..'; @@ -35,14 +36,112 @@ export const storedValues = () => [ { name: 'product3', orderNumber: 3 }, { name: 'product4', orderNumber: 4 }, { name: 'product5', orderNumber: 5 }, - { name: 'product6', orderNumber: null }, - { name: 'product7', orderNumber: null }, + { name: 'product6', orderNumber: 6 }, + { name: 'product7', orderNumber: 7 }, ]; -export const supportedFilters = () => [ - 'null_equality', - 'equality', - 'ordering', - 'in_empty_null', - 'in_equal', -]; +export const supportedFilters = () => []; + +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({ + where, + orderBy: { name: 'asc' }, + query: 'name orderNumber', + }) + ).toEqual(expected.map(i => _storedValues[i])); + + test( + 'Filter: orderNumber', + withKeystone(({ context }: { context: KeystoneContext }) => + match(context, { orderNumber: _f(1) }, [0]) + ) + ); + + test( + 'Filter: orderNumber_not', + withKeystone(({ context }: { context: KeystoneContext }) => + match(context, { orderNumber_not: _f(1) }, [1, 2, 3, 4, 5, 6]) + ) + ); + + test( + 'Filter: orderNumber_not null', + withKeystone(({ context }: { context: KeystoneContext }) => + match(context, { orderNumber_not: null }, [0, 1, 2, 3, 4, 5, 6]) + ) + ); + + test( + 'Filter: orderNumber_lt', + withKeystone(({ context }: { context: KeystoneContext }) => + match(context, { orderNumber_lt: _f(2) }, [0]) + ) + ); + + test( + 'Filter: orderNumber_lte', + withKeystone(({ context }: { context: KeystoneContext }) => + match(context, { orderNumber_lte: _f(2) }, [0, 1]) + ) + ); + + test( + 'Filter: orderNumber_gt', + withKeystone(({ context }: { context: KeystoneContext }) => + match(context, { orderNumber_gt: _f(2) }, [2, 3, 4, 5, 6]) + ) + ); + + test( + 'Filter: orderNumber_gte', + withKeystone(({ context }: { context: KeystoneContext }) => + match(context, { orderNumber_gte: _f(2) }, [1, 2, 3, 4, 5, 6]) + ) + ); + + test( + 'Filter: orderNumber_in (empty list)', + withKeystone(({ context }: { context: KeystoneContext }) => + match(context, { orderNumber_in: [] }, []) + ) + ); + + test( + 'Filter: orderNumber_not_in (empty list)', + withKeystone(({ context }: { context: KeystoneContext }) => + match(context, { orderNumber_not_in: [] }, [0, 1, 2, 3, 4, 5, 6]) + ) + ); + + test( + 'Filter: orderNumber_in', + withKeystone(({ context }: { context: KeystoneContext }) => + match(context, { orderNumber_in: ([1, 2, 3] as const).map(_f) }, [0, 1, 2]) + ) + ); + + test( + 'Filter: orderNumber_not_in', + withKeystone(({ context }: { context: KeystoneContext }) => + match(context, { orderNumber_not_in: [1, 2, 3].map(_f) }, [3, 4, 5, 6]) + ) + ); + + test( + 'Filter: orderNumber_in null', + withKeystone(({ context }: { context: KeystoneContext }) => + match(context, { orderNumber_in: [null] }, []) + ) + ); + + test( + 'Filter: orderNumber_not_in null', + withKeystone(({ context }: { context: KeystoneContext }) => + match(context, { orderNumber_not_in: [null] }, [0, 1, 2, 3, 4, 5, 6]) + ) + ); +};