Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove gqlType option on autoIncrement field type #6280

Merged
merged 6 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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