From 5e693b30d6ac84f4d31aeb2296623f8e22756e44 Mon Sep 17 00:00:00 2001 From: Ivan Artemiev <29709626+iartemiev@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:33:20 -0500 Subject: [PATCH] cleanup --- .../src/ClientSchema/Core/ClientModel.ts | 12 -- .../ClientSchema/utilities/ResolveField.ts | 33 ++-- .../data-schema/src/runtime/client/index.ts | 6 +- .../__tests__/custom-selection-set.test-d.ts | 145 +----------------- 4 files changed, 15 insertions(+), 181 deletions(-) diff --git a/packages/data-schema/src/ClientSchema/Core/ClientModel.ts b/packages/data-schema/src/ClientSchema/Core/ClientModel.ts index c77c7a8e3..2fd79c641 100644 --- a/packages/data-schema/src/ClientSchema/Core/ClientModel.ts +++ b/packages/data-schema/src/ClientSchema/Core/ClientModel.ts @@ -51,8 +51,6 @@ export interface ClientModel< __meta: { listOptionsPkParams: ListOptionsPkParams; disabledOperations: DisabledOpsToMap; - // return type sans async getters - used by custom selection set in the client types - flat: ClientFieldsFlat; }; } @@ -93,16 +91,6 @@ type ClientFields< AuthFields & Omit, keyof ResolveFields>; -type ClientFieldsFlat< - Bag extends Record, - Metadata extends SchemaMetadata, - IsRDS extends boolean, - T extends ModelTypeParamShape, -> = ResolveFields & - If, ImplicitIdentifier> & - AuthFields & - Omit, keyof ResolveFields>; - type SystemFields = IsRDS extends false ? { readonly createdAt: string; diff --git a/packages/data-schema/src/ClientSchema/utilities/ResolveField.ts b/packages/data-schema/src/ClientSchema/utilities/ResolveField.ts index c79b77be2..8da96ff0a 100644 --- a/packages/data-schema/src/ClientSchema/utilities/ResolveField.ts +++ b/packages/data-schema/src/ClientSchema/utilities/ResolveField.ts @@ -18,19 +18,15 @@ import { LazyLoader } from '../../runtime'; * The first type parameter (`Bag`) should always just be the top-level `ClientSchema` that * references and related model definitions can be resolved against. */ -export type ResolveFields< - Bag extends Record, - T, - Flat = false, -> = ShallowPretty< +export type ResolveFields, T> = ShallowPretty< { [K in keyof T as IsRequired extends true ? K - : never]: ResolveIndividualField; + : never]: ResolveIndividualField; } & { [K in keyof T as IsRequired extends true ? never - : K]+?: ResolveIndividualField; + : K]+?: ResolveIndividualField; } >; @@ -42,17 +38,13 @@ type ShallowPretty = { [K in keyof T]: T[K]; }; -export type ResolveIndividualField< - Bag extends Record, - T, - Flat = false, -> = +export type ResolveIndividualField, T> = T extends BaseModelField ? FieldShape : T extends RefType ? ResolveRef : T extends ModelRelationshipField - ? ResolveRelationship + ? ResolveRelationship : T extends CustomType ? ResolveFields | null : T extends EnumType @@ -65,17 +57,14 @@ export type ResolveIndividualField< type ResolveRelationship< Bag extends Record, RelationshipShape extends ModelRelationshipFieldParamShape, - Flat = false, > = DependentLazyLoaderOpIsAvailable extends true - ? Flat extends false - ? LazyLoader< - RelationshipShape['valueRequired'] extends true - ? Bag[RelationshipShape['relatedModel']]['type'] - : Bag[RelationshipShape['relatedModel']]['type'] | null, - RelationshipShape['array'] - > - : Bag[RelationshipShape['relatedModel']]['type'] + ? LazyLoader< + RelationshipShape['valueRequired'] extends true + ? Bag[RelationshipShape['relatedModel']]['type'] + : Bag[RelationshipShape['relatedModel']]['type'] | null, + RelationshipShape['array'] + > : never; type DependentLazyLoaderOpIsAvailable< diff --git a/packages/data-schema/src/runtime/client/index.ts b/packages/data-schema/src/runtime/client/index.ts index 97795b257..02e5e9f55 100644 --- a/packages/data-schema/src/runtime/client/index.ts +++ b/packages/data-schema/src/runtime/client/index.ts @@ -87,7 +87,7 @@ type ReturnValue< * Note: custom type field arrays are already handled correctly and don't need to be "restored", hence the `Result[K] extends Array` check * */ -export type RestoreArrays< +type RestoreArrays< Result, FlatModel, NonNullResult = NonNullable, @@ -187,7 +187,7 @@ type CustomSelectionSetReturnValue< * To work around this limitation, DeepPickFromPath flattens Arrays of Models (e.g. { comments: { id: string}[] } => { comments: { id: string} }) * Arrays are then restored downstream in RestoreArrays */ -export type DeepPickFromPath< +type DeepPickFromPath< FlatModel extends Model, Path extends string, > = FlatModel extends undefined @@ -307,7 +307,7 @@ export type ModelPath< * * ``` */ -export type ResolvedModel< +type ResolvedModel< Model extends Record, Depth extends number = 7, RecursionLoop extends number[] = [-1, 0, 1, 2, 3, 4, 5, 6], diff --git a/packages/integration-tests/__tests__/custom-selection-set.test-d.ts b/packages/integration-tests/__tests__/custom-selection-set.test-d.ts index b740c9b84..68d5b0554 100644 --- a/packages/integration-tests/__tests__/custom-selection-set.test-d.ts +++ b/packages/integration-tests/__tests__/custom-selection-set.test-d.ts @@ -1,20 +1,6 @@ import { a, ClientSchema } from '@aws-amplify/data-schema'; +import { Expect, Equal } from '@aws-amplify/data-schema-types'; import { generateClient, SelectionSet } from 'aws-amplify/api'; -import { - Expect, - Equal, - // Remove before PR - UnionToIntersection, - Prettify, -} from '@aws-amplify/data-schema-types'; - -// Temp: - -import { - ResolvedModel, - DeepPickFromPath, - RestoreArrays, -} from '../../data-schema/src/runtime'; type Json = null | string | number | boolean | object | any[]; @@ -251,31 +237,6 @@ describe('Custom Selection Set', () => { ], }); - type FM = Prettify; - - type Actual = { - readonly id: string; - readonly comments: { - readonly post: { - readonly comments: { - readonly post: { - readonly comments: { - readonly post: { - readonly comments: { - readonly content: string; - readonly postId: string | null; - readonly id: string; - readonly createdAt: string; - readonly updatedAt: string; - }; - }; - }; - }; - }; - }; - }[]; - }[]; - type ExpectedType2 = { readonly id: string; readonly comments: { @@ -624,93 +585,16 @@ describe('Custom Selection Set', () => { type _ = Expect>; }); - type LocalRestoreArrays = { - [K in keyof Result]: K extends keyof FlatModel - ? Result[K] extends Array - ? FlatModel[K] extends Array - ? RestoreArrays[] - : never - : Result[K] extends object - ? RestoreArrays - : FlatModel[K] - : never; - }; - // https://github.com/aws-amplify/amplify-category-api/issues/2809 test('custom selection set on array of custom types', async () => { - // type UnwrapArray = T extends Array ? T[number] : T; - - /* - ~~Pass 1~~ - Result = { - title: string; - metas: { - requiredTags: string[]; - tags?: Nullable[] | null | undefined; - } | null | undefined; - } - - FlatModel = { - title: string; - metas?: ({ - requiredTags: string[]; - tags?: Nullable[] | null | undefined; - } | null | undefined)[] | null | undefined; - } - - - ~~Pass 2~~ - Result = { - requiredTags: string[]; - tags?: Nullable[] | null | undefined; - } | null | undefined; - - FlatModel = ({ - requiredTags: string[]; - tags?: Nullable[] | null | undefined; - } | null | undefined)[] | null | undefined; - - - */ - type UnwrapArray = - NonNullable extends Array ? NonNullable[number] : T; - - // Strip out null in DeepPick and restore in - const selSet = ['title', 'metas.*'] as const; const { data: posts } = await client.models.Post7.list({ selectionSet: selSet, }); - type PostType = Schema['Post7']['type']; - type FlatModel = ResolvedModel; - type Metas = FlatModel['metas']; - type MetasUnwrapped = UnwrapArray; - - type DeepPick = DeepPickFromPath; - - type Result = Prettify>; - type PrettyResult = Prettify>; - - type LocalRestored = Prettify>; - - type ActualRestored = Prettify>; - - type Test1 = Array extends FlatModel['metas'] ? true : false; - type ActualType = typeof posts; - type AT = { - readonly title: string; - readonly metas: - | { - readonly requiredTags: string[]; - readonly tags: (string | null)[] | null; - }[] - | null; - }[]; - type ExpectedType = { readonly title: string; readonly metas: @@ -733,15 +617,6 @@ describe('Custom Selection Set', () => { selectionSet: selSet, }); - type PostType = Schema['Post7']['type']; - type FlatModel = ResolvedModel; - - type DeepPick = DeepPickFromPath; - - type Result = Prettify>; - - type Restored = Prettify>; - type ActualType = typeof posts; type ExpectedType = { @@ -766,15 +641,6 @@ describe('Custom Selection Set', () => { selectionSet: selSet, }); - type PostType = Schema['Post7']['type']; - type FlatModel = ResolvedModel; - - type DeepPick = DeepPickFromPath; - - type Result = UnionToIntersection; - - type Restored = Prettify>; - type ActualType = typeof posts; type ExpectedType = { @@ -796,15 +662,6 @@ describe('Custom Selection Set', () => { selectionSet: selSet, }); - type PostType = Schema['Post7']['type']; - type FlatModel = ResolvedModel; - - type DeepPick = DeepPickFromPath; - - type Result = Prettify>; - - type Restored = Prettify>; - type ActualType = typeof posts; type ExpectedType = {