Skip to content

Commit

Permalink
Remove gqlType option on autoIncrement field type (keystonejs#6280)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown authored and Nikitoring committed Sep 14, 2021
1 parent 8ce5f5e commit 1989b4c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 92 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-dancers-remember.md
Original file line number Diff line number Diff line change
@@ -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
57 changes: 7 additions & 50 deletions packages/fields/src/types/autoIncrement/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export type AutoIncrementFieldConfig<TGeneratedListTypes extends BaseGeneratedLi
isRequired?: boolean;
isIndexed?: boolean;
isUnique?: boolean;
gqlType?: 'ID' | 'Int';
};

export const autoIncrement =
Expand All @@ -26,19 +25,17 @@ export const autoIncrement =
defaultValue,
isIndexed,
isUnique,
gqlType,
...config
}: AutoIncrementFieldConfig<TGeneratedListTypes> = {}): 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),
Expand All @@ -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',
Expand All @@ -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,
});
Expand Down
58 changes: 16 additions & 42 deletions packages/fields/src/types/autoIncrement/tests/test-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,20 @@ 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 = () => {
Expand All @@ -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<string, any>, expected: any[]) =>
expect(
await context.lists.Test.findMany({
Expand Down

0 comments on commit 1989b4c

Please sign in to comment.