Skip to content

Commit

Permalink
Remove __legacy from field type implementations (#6691)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown authored Oct 1, 2021
1 parent 183e378 commit 5d3fc0b
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 76 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-apes-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': major
---

Removed `__legacy` property from field type implementations
7 changes: 0 additions & 7 deletions docs/pages/docs/guides/custom-fields.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@ import {
graphql,
orderDirectionEnum,
filters,
FieldDefaultValue,
} from '@keystone-next/keystone/types';

export type MyIntFieldConfig<TGeneratedListTypes extends BaseGeneratedListTypes> =
CommonFieldConfig<TGeneratedListTypes> & {
defaultValue?: FieldDefaultValue<number, TGeneratedListTypes>;
isRequired?: boolean;
isIndexed?: boolean | 'unique';
};

Expand All @@ -65,10 +62,6 @@ export const myInt =
},
output: graphql.field({ type: graphql.Int }),
views: require.resolve('./view.tsx'),
__legacy: {
isRequired,
defaultValue,
},
});
```

Expand Down
9 changes: 0 additions & 9 deletions examples/custom-field/stars-field/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
BaseGeneratedListTypes,
FieldDefaultValue,
fieldType,
FieldTypeFunc,
CommonFieldConfig,
Expand All @@ -16,17 +15,13 @@ import { graphql } from '@keystone-next/keystone';

export type StarsFieldConfig<TGeneratedListTypes extends BaseGeneratedListTypes> =
CommonFieldConfig<TGeneratedListTypes> & {
defaultValue?: FieldDefaultValue<number, TGeneratedListTypes>;
isRequired?: boolean;
isIndexed?: boolean | 'unique';
maxStars?: number;
};

export const stars =
<TGeneratedListTypes extends BaseGeneratedListTypes>({
isIndexed,
isRequired,
defaultValue,
maxStars = 5,
...config
}: StarsFieldConfig<TGeneratedListTypes> = {}): FieldTypeFunc =>
Expand Down Expand Up @@ -97,8 +92,4 @@ export const stars =
getAdminMeta() {
return { maxStars };
},
__legacy: {
isRequired,
defaultValue,
},
});
22 changes: 1 addition & 21 deletions packages/keystone/src/lib/core/mutations/create-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,31 +228,11 @@ async function getResolvedData(
},
nestedMutationState: NestedMutationState
) {
const { context, operation, inputData } = hookArgs;
const { context, operation } = hookArgs;

// Start with the original input
let resolvedData = hookArgs.inputData;

// Apply default values
// We don't expect any errors from here, so we can wrap all these operations
// in a generic catch-all error handler.
if (operation === 'create') {
resolvedData = Object.fromEntries(
await promiseAllRejectWithAllErrors(
Object.entries(list.fields).map(async ([fieldKey, field]) => {
let input = resolvedData[fieldKey];
if (input === undefined && field.__legacy?.defaultValue !== undefined) {
input =
typeof field.__legacy.defaultValue === 'function'
? await field.__legacy.defaultValue({ originalInput: inputData, context })
: field.__legacy.defaultValue;
}
return [fieldKey, input] as const;
})
)
);
}

// Apply non-relationship field type input resolvers
const resolverErrors: { error: Error; tag: string }[] = [];
resolvedData = Object.fromEntries(
Expand Down
23 changes: 0 additions & 23 deletions packages/keystone/src/lib/core/mutations/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,8 @@ export async function validateUpdateCreate({
list: InitialisedList;
hookArgs: Omit<UpdateCreateHookArgs, 'addValidationError'>;
}) {
const { operation, resolvedData } = hookArgs;
const messages: string[] = [];

// Check isRequired
for (const [fieldKey, field] of Object.entries(list.fields)) {
// yes, this is a massive hack, it's just to make image and file fields work well enough
let val = resolvedData[fieldKey];
if (field.dbField.kind === 'multi') {
if (Object.values(resolvedData[fieldKey]).every(x => x === null)) {
val = null;
}
if (Object.values(resolvedData[fieldKey]).every(x => x === undefined)) {
val = undefined;
}
}
if (
field.__legacy?.isRequired &&
((operation === 'create' && val == null) || (operation === 'update' && val === null))
) {
messages.push(
`${list.listKey}.${fieldKey}: Required field "${fieldKey}" is null or undefined.`
);
}
}

const fieldsErrors: { error: Error; tag: string }[] = [];
// Field validation hooks
for (const [fieldKey, field] of Object.entries(list.fields)) {
Expand Down
12 changes: 1 addition & 11 deletions packages/keystone/src/types/core.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import { IncomingMessage, ServerResponse } from 'http';
import type { GraphQLResolveInfo } from 'graphql';
import type { BaseGeneratedListTypes, GqlNames, MaybePromise } from './utils';
import type { GqlNames } from './utils';
import type { KeystoneContext, SessionContext } from './context';

type FieldDefaultValueArgs<TGeneratedListTypes extends BaseGeneratedListTypes> = {
context: KeystoneContext;
originalInput: TGeneratedListTypes['inputs']['create'];
};

export type DatabaseProvider = 'sqlite' | 'postgresql';

export type FieldDefaultValue<T, TGeneratedListTypes extends BaseGeneratedListTypes> =
| T
| null
| ((args: FieldDefaultValueArgs<TGeneratedListTypes>) => MaybePromise<T | null | undefined>);

export type CreateRequestContext = (
req: IncomingMessage,
res: ServerResponse
Expand Down
6 changes: 1 addition & 5 deletions packages/keystone/src/types/next-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Decimal from 'decimal.js';
import { graphql } from '..';
import { BaseGeneratedListTypes } from './utils';
import { CommonFieldConfig } from './config';
import { DatabaseProvider, FieldDefaultValue } from './core';
import { DatabaseProvider } from './core';
import { AdminMetaRootVal, JSONValue, KeystoneContext, MaybePromise } from '.';

export { Decimal };
Expand Down Expand Up @@ -371,10 +371,6 @@ export type FieldTypeWithoutDBField<
extraOutputFields?: Record<string, FieldTypeOutputField<TDBField>>;
getAdminMeta?: (adminMeta: AdminMetaRootVal) => JSONValue;
unreferencedConcreteInterfaceImplementations?: graphql.ObjectType<any>[];
__legacy?: {
isRequired?: boolean;
defaultValue?: FieldDefaultValue<any, BaseGeneratedListTypes>;
};
} & CommonFieldConfig<BaseGeneratedListTypes>;

type AnyInputObj = graphql.InputObjectType<Record<string, graphql.Arg<graphql.InputType, any>>>;
Expand Down

0 comments on commit 5d3fc0b

Please sign in to comment.