From 51ab28615a706012150bedde914b8f9cdf71c9d6 Mon Sep 17 00:00:00 2001 From: Mitchell Hamilton Date: Tue, 27 Sep 2022 16:22:43 +1000 Subject: [PATCH] Remove `filters` export from `@keystone-6/core/types` (#7919) --- .changeset/silent-dolphins-scream.md | 5 ++ docs/pages/docs/guides/custom-fields.md | 8 --- examples/custom-field/1-text-field/index.ts | 5 -- examples/custom-field/1-text-field/views.tsx | 69 ------------------ examples/custom-field/2-stars-field/index.ts | 5 -- examples/custom-field/2-stars-field/views.tsx | 71 +------------------ examples/custom-field/schema.graphql | 41 ----------- .../{types => fields}/filters/enum-filter.ts | 2 +- packages/core/src/fields/filters/index.ts | 1 + .../index.ts => fields/filters/internal.ts} | 0 .../filters/providers/mysql.ts | 2 +- .../filters/providers/postgresql.ts | 4 +- .../filters/providers/sqlite.ts | 2 +- .../core/src/fields/types/bigInt/index.ts | 2 +- .../src/fields/types/calendarDay/index.ts | 2 +- .../core/src/fields/types/checkbox/index.ts | 2 +- .../core/src/fields/types/decimal/index.ts | 2 +- packages/core/src/fields/types/float/index.ts | 2 +- .../core/src/fields/types/integer/index.ts | 2 +- .../core/src/fields/types/select/index.ts | 2 +- packages/core/src/fields/types/text/index.ts | 2 +- .../core/src/fields/types/timestamp/index.ts | 2 +- packages/core/src/types/index.ts | 1 - prisma-utils/src/index.ts | 14 +--- 24 files changed, 24 insertions(+), 224 deletions(-) create mode 100644 .changeset/silent-dolphins-scream.md rename packages/core/src/{types => fields}/filters/enum-filter.ts (98%) create mode 100644 packages/core/src/fields/filters/index.ts rename packages/core/src/{types/filters/index.ts => fields/filters/internal.ts} (100%) rename packages/core/src/{types => fields}/filters/providers/mysql.ts (99%) rename packages/core/src/{types => fields}/filters/providers/postgresql.ts (99%) rename packages/core/src/{types => fields}/filters/providers/sqlite.ts (99%) diff --git a/.changeset/silent-dolphins-scream.md b/.changeset/silent-dolphins-scream.md new file mode 100644 index 00000000000..4817ce9d1bb --- /dev/null +++ b/.changeset/silent-dolphins-scream.md @@ -0,0 +1,5 @@ +--- +'@keystone-6/core': major +--- + +Removes `filters` export from `@keystone-6/core/types` diff --git a/docs/pages/docs/guides/custom-fields.md b/docs/pages/docs/guides/custom-fields.md index 831443cc6b2..ec8f629917e 100644 --- a/docs/pages/docs/guides/custom-fields.md +++ b/docs/pages/docs/guides/custom-fields.md @@ -30,7 +30,6 @@ import { CommonFieldConfig, fieldType, orderDirectionEnum, - filters, } from '@keystone-6/core/types'; import { graphql } from '@keystone-6/core'; @@ -53,7 +52,6 @@ export const myInt = })({ ...config, input: { - where: { arg: graphql.arg({ type: filters[meta.provider].Int.optional }), resolve: filters.resolveCommon }, create: { arg: graphql.arg({ type: graphql.Int }) }, update: { arg: graphql.arg({ type: graphql.Int }) }, orderBy: { arg: graphql.arg({ type: orderDirectionEnum }) }, @@ -74,7 +72,6 @@ The `input` object defines the GraphQL inputs for the field type. ```ts input: { - where: { arg: graphql.arg({ type: filters[meta.provider].Int.optional }), resolve: filters.resolveCommon }, create: { arg: graphql.arg({ type: graphql.Int }) }, update: { arg: graphql.arg({ type: graphql.Int }) }, orderBy: { arg: graphql.arg({ type: orderDirectionEnum }) }, @@ -85,7 +82,6 @@ You can also provide resolvers to transform the value coming from GraphQL into t ```ts input: { - where: { arg: graphql.arg({ type: filters[meta.provider].Int.optional }), resolve: filters.resolveCommon }, create: { arg: graphql.arg({ type: graphql.Int }), resolve: (val, context) => val }, update: { arg: graphql.arg({ type: graphql.Int }), resolve: (val, context) => val }, orderBy: { arg: graphql.arg({ type: orderDirectionEnum }), resolve: (val, context) => val }, @@ -139,10 +135,6 @@ export const controller = (config: FieldControllerConfig): FieldController ({ [config.path]: value === '' ? null : parseInt(value, 10) }), - filter: { - // the filters section is omitted for brevity - // see what this looks like for the integer field at https://github.com/keystonejs/keystone/blob/e0d1b2068de2ea3b6770c58af91221b01e6a20cf/packages-next/fields/src/types/integer/views/index.tsx#L60-L128 - } }; }; ``` diff --git a/examples/custom-field/1-text-field/index.ts b/examples/custom-field/1-text-field/index.ts index dd63dfcba6c..43960a501e3 100644 --- a/examples/custom-field/1-text-field/index.ts +++ b/examples/custom-field/1-text-field/index.ts @@ -4,7 +4,6 @@ import { FieldTypeFunc, CommonFieldConfig, orderDirectionEnum, - filters, } from '@keystone-6/core/types'; import { graphql } from '@keystone-6/core'; @@ -26,10 +25,6 @@ export function text({ })({ ...config, input: { - where: { - arg: graphql.arg({ type: filters[meta.provider].String.optional }), - resolve: filters.resolveCommon, - }, create: { arg: graphql.arg({ type: graphql.String }), // eslint-disable-next-line @typescript-eslint/no-unused-vars diff --git a/examples/custom-field/1-text-field/views.tsx b/examples/custom-field/1-text-field/views.tsx index a51898bc3a8..9ebc19076be 100644 --- a/examples/custom-field/1-text-field/views.tsx +++ b/examples/custom-field/1-text-field/views.tsx @@ -61,74 +61,5 @@ export const controller = ( return typeof value === 'string' ? value : null; }, serialize: value => ({ [config.path]: value }), - filter: { - Filter(props) { - return ( - { - props.onChange(event.target.value); - }} - value={props.value} - autoFocus={props.autoFocus} - /> - ); - }, - - graphql: ({ type, value }) => { - const key = type === 'is' ? config.path : `${config.path}_${type}`; - const valueWithoutWhitespace = value.replace(/\s/g, ''); - - return { - [key]: ['in', 'not_in'].includes(type) - ? valueWithoutWhitespace.split(',').map(i => parseInt(i)) - : parseInt(valueWithoutWhitespace), - }; - }, - Label({ label, value, type }) { - let renderedValue = value; - if (['in', 'not_in'].includes(type)) { - renderedValue = value - .split(',') - .map(value => value.trim()) - .join(', '); - } - return `${label.toLowerCase()}: ${renderedValue}`; - }, - types: { - is: { - label: 'Is exactly', - initialValue: '', - }, - not: { - label: 'Is not exactly', - initialValue: '', - }, - gt: { - label: 'Is greater than', - initialValue: '', - }, - lt: { - label: 'Is less than', - initialValue: '', - }, - gte: { - label: 'Is greater than or equal to', - initialValue: '', - }, - lte: { - label: 'Is less than or equal to', - initialValue: '', - }, - in: { - label: 'Is one of', - initialValue: '', - }, - not_in: { - label: 'Is not one of', - initialValue: '', - }, - }, - }, }; }; diff --git a/examples/custom-field/2-stars-field/index.ts b/examples/custom-field/2-stars-field/index.ts index 8a9c44bad86..ff639ce9bd9 100644 --- a/examples/custom-field/2-stars-field/index.ts +++ b/examples/custom-field/2-stars-field/index.ts @@ -4,7 +4,6 @@ import { FieldTypeFunc, CommonFieldConfig, orderDirectionEnum, - filters, } from '@keystone-6/core/types'; import { graphql } from '@keystone-6/core'; @@ -49,10 +48,6 @@ export const stars = }, // all of these inputs are optional if they don't make sense for a particular field type input: { - where: { - arg: graphql.arg({ type: filters[meta.provider].Int.optional }), - resolve: filters.resolveCommon, - }, create: { arg: graphql.arg({ type: graphql.Int }), // this field type doesn't need to do anything special diff --git a/examples/custom-field/2-stars-field/views.tsx b/examples/custom-field/2-stars-field/views.tsx index bf97387f168..a4d0e578ea4 100644 --- a/examples/custom-field/2-stars-field/views.tsx +++ b/examples/custom-field/2-stars-field/views.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { FieldContainer, FieldDescription, FieldLabel, TextInput } from '@keystone-ui/fields'; +import { FieldContainer, FieldDescription, FieldLabel } from '@keystone-ui/fields'; import { CellLink, CellContainer } from '@keystone-6/core/admin-ui/components'; import { @@ -57,74 +57,5 @@ export const controller = ( return typeof value === 'number' ? value : null; }, serialize: value => ({ [config.path]: value }), - filter: { - Filter(props) { - return ( - { - props.onChange(event.target.value.replace(/[^\d,\s-]/g, '')); - }} - value={props.value} - autoFocus={props.autoFocus} - /> - ); - }, - - graphql: ({ type, value }) => { - const key = type === 'is' ? config.path : `${config.path}_${type}`; - const valueWithoutWhitespace = value.replace(/\s/g, ''); - - return { - [key]: ['in', 'not_in'].includes(type) - ? valueWithoutWhitespace.split(',').map(i => parseInt(i)) - : parseInt(valueWithoutWhitespace), - }; - }, - Label({ label, value, type }) { - let renderedValue = value; - if (['in', 'not_in'].includes(type)) { - renderedValue = value - .split(',') - .map(value => value.trim()) - .join(', '); - } - return `${label.toLowerCase()}: ${renderedValue}`; - }, - types: { - is: { - label: 'Is exactly', - initialValue: '', - }, - not: { - label: 'Is not exactly', - initialValue: '', - }, - gt: { - label: 'Is greater than', - initialValue: '', - }, - lt: { - label: 'Is less than', - initialValue: '', - }, - gte: { - label: 'Is greater than or equal to', - initialValue: '', - }, - lte: { - label: 'Is less than or equal to', - initialValue: '', - }, - in: { - label: 'Is one of', - initialValue: '', - }, - not_in: { - label: 'Is not one of', - initialValue: '', - }, - }, - }, }; }; diff --git a/examples/custom-field/schema.graphql b/examples/custom-field/schema.graphql index e9e8f3f30d5..1469ac1e926 100644 --- a/examples/custom-field/schema.graphql +++ b/examples/custom-field/schema.graphql @@ -17,8 +17,6 @@ input PostWhereInput { OR: [PostWhereInput!] NOT: [PostWhereInput!] id: IDFilter - content: StringNullableFilter - rating: IntNullableFilter pair: PairFilter } @@ -33,45 +31,6 @@ input IDFilter { not: IDFilter } -input StringNullableFilter { - equals: String - in: [String!] - notIn: [String!] - lt: String - lte: String - gt: String - gte: String - contains: String - startsWith: String - endsWith: String - not: NestedStringNullableFilter -} - -input NestedStringNullableFilter { - equals: String - in: [String!] - notIn: [String!] - lt: String - lte: String - gt: String - gte: String - contains: String - startsWith: String - endsWith: String - not: NestedStringNullableFilter -} - -input IntNullableFilter { - equals: Int - in: [Int!] - notIn: [Int!] - lt: Int - lte: Int - gt: Int - gte: Int - not: IntNullableFilter -} - input PairFilter { equals: String } diff --git a/packages/core/src/types/filters/enum-filter.ts b/packages/core/src/fields/filters/enum-filter.ts similarity index 98% rename from packages/core/src/types/filters/enum-filter.ts rename to packages/core/src/fields/filters/enum-filter.ts index 8c03e08658b..320386bb399 100644 --- a/packages/core/src/types/filters/enum-filter.ts +++ b/packages/core/src/fields/filters/enum-filter.ts @@ -1,4 +1,4 @@ -import { graphql } from '../schema'; +import { graphql } from '../../types/schema'; // yes, these two types have the fields but they're semantically different types // (even though, yes, having EnumFilter by defined as EnumNullableFilter, would be the same type but names would show up differently in editors for example) diff --git a/packages/core/src/fields/filters/index.ts b/packages/core/src/fields/filters/index.ts new file mode 100644 index 00000000000..3e7a57417e7 --- /dev/null +++ b/packages/core/src/fields/filters/index.ts @@ -0,0 +1 @@ +export * as filters from './internal'; diff --git a/packages/core/src/types/filters/index.ts b/packages/core/src/fields/filters/internal.ts similarity index 100% rename from packages/core/src/types/filters/index.ts rename to packages/core/src/fields/filters/internal.ts diff --git a/packages/core/src/types/filters/providers/mysql.ts b/packages/core/src/fields/filters/providers/mysql.ts similarity index 99% rename from packages/core/src/types/filters/providers/mysql.ts rename to packages/core/src/fields/filters/providers/mysql.ts index 2c743119c66..7b81dff2002 100644 --- a/packages/core/src/types/filters/providers/mysql.ts +++ b/packages/core/src/fields/filters/providers/mysql.ts @@ -1,7 +1,7 @@ // Do not manually modify this file, it is automatically generated by the package at /prisma-utils in this repo. // Update the script if you need this file to be different -import { graphql } from '../../schema'; +import { graphql } from '../../../types/schema'; type StringNullableFilterType = graphql.InputObjectType<{ // can be null diff --git a/packages/core/src/types/filters/providers/postgresql.ts b/packages/core/src/fields/filters/providers/postgresql.ts similarity index 99% rename from packages/core/src/types/filters/providers/postgresql.ts rename to packages/core/src/fields/filters/providers/postgresql.ts index 1acbb9b369e..95eb9ec4ad4 100644 --- a/packages/core/src/types/filters/providers/postgresql.ts +++ b/packages/core/src/fields/filters/providers/postgresql.ts @@ -1,9 +1,9 @@ // Do not manually modify this file, it is automatically generated by the package at /prisma-utils in this repo. // Update the script if you need this file to be different -import { graphql } from '../../schema'; +import { graphql } from '../../../types/schema'; -import { QueryMode } from '../../next-fields'; +import { QueryMode } from '../../../types/next-fields'; type StringNullableFilterType = graphql.InputObjectType<{ // can be null diff --git a/packages/core/src/types/filters/providers/sqlite.ts b/packages/core/src/fields/filters/providers/sqlite.ts similarity index 99% rename from packages/core/src/types/filters/providers/sqlite.ts rename to packages/core/src/fields/filters/providers/sqlite.ts index 2c743119c66..7b81dff2002 100644 --- a/packages/core/src/types/filters/providers/sqlite.ts +++ b/packages/core/src/fields/filters/providers/sqlite.ts @@ -1,7 +1,7 @@ // Do not manually modify this file, it is automatically generated by the package at /prisma-utils in this repo. // Update the script if you need this file to be different -import { graphql } from '../../schema'; +import { graphql } from '../../../types/schema'; type StringNullableFilterType = graphql.InputObjectType<{ // can be null diff --git a/packages/core/src/fields/types/bigInt/index.ts b/packages/core/src/fields/types/bigInt/index.ts index deed492469a..7f1b1f9ac2c 100644 --- a/packages/core/src/fields/types/bigInt/index.ts +++ b/packages/core/src/fields/types/bigInt/index.ts @@ -5,7 +5,6 @@ import { FieldTypeFunc, CommonFieldConfig, orderDirectionEnum, - filters, } from '../../../types'; import { graphql } from '../../..'; import { @@ -13,6 +12,7 @@ import { assertReadIsNonNullAllowed, getResolvedIsNullable, } from '../../non-null-graphql'; +import { filters } from '../../filters'; export type BigIntFieldConfig = CommonFieldConfig & { diff --git a/packages/core/src/fields/types/calendarDay/index.ts b/packages/core/src/fields/types/calendarDay/index.ts index a36d9ad7677..f3e94ef45f7 100644 --- a/packages/core/src/fields/types/calendarDay/index.ts +++ b/packages/core/src/fields/types/calendarDay/index.ts @@ -5,7 +5,6 @@ import { FieldTypeFunc, CommonFieldConfig, orderDirectionEnum, - filters, } from '../../../types'; import { graphql } from '../../..'; import { @@ -13,6 +12,7 @@ import { assertReadIsNonNullAllowed, getResolvedIsNullable, } from '../../non-null-graphql'; +import { filters } from '../../filters'; import { CalendarDayFieldMeta } from './views'; export type CalendarDayFieldConfig = diff --git a/packages/core/src/fields/types/checkbox/index.ts b/packages/core/src/fields/types/checkbox/index.ts index f025ad16f51..281d4551b78 100644 --- a/packages/core/src/fields/types/checkbox/index.ts +++ b/packages/core/src/fields/types/checkbox/index.ts @@ -5,10 +5,10 @@ import { fieldType, FieldTypeFunc, orderDirectionEnum, - filters, } from '../../../types'; import { graphql } from '../../..'; import { assertCreateIsNonNullAllowed, assertReadIsNonNullAllowed } from '../../non-null-graphql'; +import { filters } from '../../filters'; export type CheckboxFieldConfig = CommonFieldConfig & { diff --git a/packages/core/src/fields/types/decimal/index.ts b/packages/core/src/fields/types/decimal/index.ts index d979ccc86f2..6a2b2698b1d 100644 --- a/packages/core/src/fields/types/decimal/index.ts +++ b/packages/core/src/fields/types/decimal/index.ts @@ -6,7 +6,6 @@ import { CommonFieldConfig, orderDirectionEnum, Decimal, - filters, FieldData, } from '../../../types'; import { graphql } from '../../..'; @@ -15,6 +14,7 @@ import { assertReadIsNonNullAllowed, getResolvedIsNullable, } from '../../non-null-graphql'; +import { filters } from '../../filters'; export type DecimalFieldConfig = CommonFieldConfig & { diff --git a/packages/core/src/fields/types/float/index.ts b/packages/core/src/fields/types/float/index.ts index a4befb6eab4..351c6a2eac3 100644 --- a/packages/core/src/fields/types/float/index.ts +++ b/packages/core/src/fields/types/float/index.ts @@ -6,7 +6,6 @@ import { CommonFieldConfig, fieldType, orderDirectionEnum, - filters, } from '../../../types'; import { graphql } from '../../..'; import { @@ -14,6 +13,7 @@ import { assertReadIsNonNullAllowed, getResolvedIsNullable, } from '../../non-null-graphql'; +import { filters } from '../../filters'; export type FloatFieldConfig = CommonFieldConfig & { diff --git a/packages/core/src/fields/types/integer/index.ts b/packages/core/src/fields/types/integer/index.ts index bdee5f3d3d7..65f358ba11c 100644 --- a/packages/core/src/fields/types/integer/index.ts +++ b/packages/core/src/fields/types/integer/index.ts @@ -5,7 +5,6 @@ import { FieldTypeFunc, CommonFieldConfig, orderDirectionEnum, - filters, } from '../../../types'; import { graphql } from '../../..'; import { @@ -13,6 +12,7 @@ import { assertReadIsNonNullAllowed, getResolvedIsNullable, } from '../../non-null-graphql'; +import { filters } from '../../filters'; export type IntegerFieldConfig = CommonFieldConfig & { diff --git a/packages/core/src/fields/types/select/index.ts b/packages/core/src/fields/types/select/index.ts index acf63fde30f..84af552729d 100644 --- a/packages/core/src/fields/types/select/index.ts +++ b/packages/core/src/fields/types/select/index.ts @@ -6,7 +6,6 @@ import { FieldTypeFunc, CommonFieldConfig, orderDirectionEnum, - filters, } from '../../../types'; import { graphql } from '../../..'; import { @@ -14,6 +13,7 @@ import { assertReadIsNonNullAllowed, getResolvedIsNullable, } from '../../non-null-graphql'; +import { filters } from '../../filters'; export type SelectFieldConfig = CommonFieldConfig & diff --git a/packages/core/src/fields/types/text/index.ts b/packages/core/src/fields/types/text/index.ts index f83dc5c2b9a..3e0bb8a6468 100644 --- a/packages/core/src/fields/types/text/index.ts +++ b/packages/core/src/fields/types/text/index.ts @@ -5,10 +5,10 @@ import { fieldType, orderDirectionEnum, FieldTypeFunc, - filters, } from '../../../types'; import { graphql } from '../../..'; import { assertCreateIsNonNullAllowed, assertReadIsNonNullAllowed } from '../../non-null-graphql'; +import { filters } from '../../filters'; export type TextFieldConfig = CommonFieldConfig & { diff --git a/packages/core/src/fields/types/timestamp/index.ts b/packages/core/src/fields/types/timestamp/index.ts index 70e3c4dd10d..f9805c08ca0 100644 --- a/packages/core/src/fields/types/timestamp/index.ts +++ b/packages/core/src/fields/types/timestamp/index.ts @@ -5,7 +5,6 @@ import { FieldTypeFunc, CommonFieldConfig, orderDirectionEnum, - filters, } from '../../../types'; import { graphql } from '../../..'; import { @@ -13,6 +12,7 @@ import { assertReadIsNonNullAllowed, getResolvedIsNullable, } from '../../non-null-graphql'; +import { filters } from '../../filters'; import { TimestampFieldMeta } from './views'; export type TimestampFieldConfig = diff --git a/packages/core/src/types/index.ts b/packages/core/src/types/index.ts index 632b018bab7..7797ad19fa5 100644 --- a/packages/core/src/types/index.ts +++ b/packages/core/src/types/index.ts @@ -5,6 +5,5 @@ export * from './session'; export * from './admin-meta'; export * from './context'; export * from './next-fields'; -export * as filters from './filters'; export { jsonFieldTypePolyfilledForSQLite } from './json-field-type-polyfill-for-sqlite'; export * from './type-info'; diff --git a/prisma-utils/src/index.ts b/prisma-utils/src/index.ts index 4babb0863f7..c6e27735513 100644 --- a/prisma-utils/src/index.ts +++ b/prisma-utils/src/index.ts @@ -97,23 +97,15 @@ ${ ); } - await fs.outputFile( - `${__dirname}/generated/only-filters/${provider}.json`, - JSON.stringify( - Object.fromEntries([...referencedTypes].map(typeName => [typeName, types[typeName]])), - null, - 2 - ) - ); - const filepath = `${__dirname}/../../packages/core/src/types/filters/providers/${provider}.ts`; + const filepath = `${__dirname}/../../packages/core/src/fields/filters/providers/${provider}.ts`; const newContent = format( `// Do not manually modify this file, it is automatically generated by the package at /prisma-utils in this repo. // Update the script if you need this file to be different - import { graphql } from '../../schema'; +import { graphql } from '../../../types/schema' -${provider === 'postgresql' ? `import { QueryMode } from '../../next-fields'` : ''} +${provider === 'postgresql' ? `import { QueryMode } from '../../../types/next-fields'` : ''} ${[...referencedTypes].map(typeName => printInputTypeForGraphQLTS(typeName, types)).join('\n\n')}