Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
iartemiev committed Dec 2, 2024
1 parent 92946c0 commit 5e693b3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 181 deletions.
12 changes: 0 additions & 12 deletions packages/data-schema/src/ClientSchema/Core/ClientModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ export interface ClientModel<
__meta: {
listOptionsPkParams: ListOptionsPkParams<Bag, T>;
disabledOperations: DisabledOpsToMap<T['disabledOperations']>;
// return type sans async getters - used by custom selection set in the client types
flat: ClientFieldsFlat<Bag, Metadata, IsRDS, T>;
};
}

Expand Down Expand Up @@ -93,16 +91,6 @@ type ClientFields<
AuthFields<Metadata, T> &
Omit<SystemFields<IsRDS>, keyof ResolveFields<Bag, T['fields']>>;

type ClientFieldsFlat<
Bag extends Record<string, unknown>,
Metadata extends SchemaMetadata<any>,
IsRDS extends boolean,
T extends ModelTypeParamShape,
> = ResolveFields<Bag, T['fields'], true> &
If<Not<IsRDS>, ImplicitIdentifier<T>> &
AuthFields<Metadata, T> &
Omit<SystemFields<IsRDS>, keyof ResolveFields<Bag, T['fields']>>;

type SystemFields<IsRDS extends boolean> = IsRDS extends false
? {
readonly createdAt: string;
Expand Down
33 changes: 11 additions & 22 deletions packages/data-schema/src/ClientSchema/utilities/ResolveField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>,
T,
Flat = false,
> = ShallowPretty<
export type ResolveFields<Bag extends Record<string, any>, T> = ShallowPretty<
{
[K in keyof T as IsRequired<T[K]> extends true
? K
: never]: ResolveIndividualField<Bag, T[K], Flat>;
: never]: ResolveIndividualField<Bag, T[K]>;
} & {
[K in keyof T as IsRequired<T[K]> extends true
? never
: K]+?: ResolveIndividualField<Bag, T[K], Flat>;
: K]+?: ResolveIndividualField<Bag, T[K]>;
}
>;

Expand All @@ -42,17 +38,13 @@ type ShallowPretty<T> = {
[K in keyof T]: T[K];
};

export type ResolveIndividualField<
Bag extends Record<string, any>,
T,
Flat = false,
> =
export type ResolveIndividualField<Bag extends Record<string, any>, T> =
T extends BaseModelField<infer FieldShape>
? FieldShape
: T extends RefType<infer RefShape, any, any>
? ResolveRef<RefShape, Bag>
: T extends ModelRelationshipField<infer RelationshipShape, any, any, any>
? ResolveRelationship<Bag, RelationshipShape, Flat>
? ResolveRelationship<Bag, RelationshipShape>
: T extends CustomType<infer CT>
? ResolveFields<Bag, CT['fields']> | null
: T extends EnumType<infer values>
Expand All @@ -65,17 +57,14 @@ export type ResolveIndividualField<
type ResolveRelationship<
Bag extends Record<string, any>,
RelationshipShape extends ModelRelationshipFieldParamShape,
Flat = false,
> =
DependentLazyLoaderOpIsAvailable<Bag, RelationshipShape> 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<
Expand Down
6 changes: 3 additions & 3 deletions packages/data-schema/src/runtime/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>` check
*
*/
export type RestoreArrays<
type RestoreArrays<
Result,
FlatModel,
NonNullResult = NonNullable<Result>,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -307,7 +307,7 @@ export type ModelPath<
*
* ```
*/
export type ResolvedModel<
type ResolvedModel<
Model extends Record<string, unknown>,
Depth extends number = 7,
RecursionLoop extends number[] = [-1, 0, 1, 2, 3, 4, 5, 6],
Expand Down
145 changes: 1 addition & 144 deletions packages/integration-tests/__tests__/custom-selection-set.test-d.ts
Original file line number Diff line number Diff line change
@@ -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[];

Expand Down Expand Up @@ -251,31 +237,6 @@ describe('Custom Selection Set', () => {
],
});

type FM = Prettify<Schema['Comment']['type']>;

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: {
Expand Down Expand Up @@ -624,93 +585,16 @@ describe('Custom Selection Set', () => {
type _ = Expect<Equal<ActualType, ExpectedType>>;
});

type LocalRestoreArrays<Result, FlatModel> = {
[K in keyof Result]: K extends keyof FlatModel
? Result[K] extends Array<infer S>
? FlatModel[K] extends Array<infer O>
? RestoreArrays<S, O>[]
: never
: Result[K] extends object
? RestoreArrays<Result[K], FlatModel[K]>
: 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> = T extends Array<any> ? T[number] : T;

/*
~~Pass 1~~
Result = {
title: string;
metas: {
requiredTags: string[];
tags?: Nullable<string>[] | null | undefined;
} | null | undefined;
}
FlatModel = {
title: string;
metas?: ({
requiredTags: string[];
tags?: Nullable<string>[] | null | undefined;
} | null | undefined)[] | null | undefined;
}
~~Pass 2~~
Result = {
requiredTags: string[];
tags?: Nullable<string>[] | null | undefined;
} | null | undefined;
FlatModel = ({
requiredTags: string[];
tags?: Nullable<string>[] | null | undefined;
} | null | undefined)[] | null | undefined;
*/
type UnwrapArray<T> =
NonNullable<T> extends Array<any> ? NonNullable<T>[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<PostType>;
type Metas = FlatModel['metas'];
type MetasUnwrapped = UnwrapArray<Metas>;

type DeepPick = DeepPickFromPath<FlatModel, (typeof selSet)[number]>;

type Result = Prettify<UnionToIntersection<DeepPick>>;
type PrettyResult = Prettify<UnionToIntersection<DeepPick>>;

type LocalRestored = Prettify<LocalRestoreArrays<Result, FlatModel>>;

type ActualRestored = Prettify<RestoreArrays<Result, FlatModel>>;

type Test1 = Array<any> 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:
Expand All @@ -733,15 +617,6 @@ describe('Custom Selection Set', () => {
selectionSet: selSet,
});

type PostType = Schema['Post7']['type'];
type FlatModel = ResolvedModel<PostType>;

type DeepPick = DeepPickFromPath<FlatModel, (typeof selSet)[number]>;

type Result = Prettify<UnionToIntersection<DeepPick>>;

type Restored = Prettify<RestoreArrays<Result, FlatModel>>;

type ActualType = typeof posts;

type ExpectedType = {
Expand All @@ -766,15 +641,6 @@ describe('Custom Selection Set', () => {
selectionSet: selSet,
});

type PostType = Schema['Post7']['type'];
type FlatModel = ResolvedModel<PostType>;

type DeepPick = DeepPickFromPath<FlatModel, (typeof selSet)[number]>;

type Result = UnionToIntersection<DeepPick>;

type Restored = Prettify<RestoreArrays<Result, FlatModel>>;

type ActualType = typeof posts;

type ExpectedType = {
Expand All @@ -796,15 +662,6 @@ describe('Custom Selection Set', () => {
selectionSet: selSet,
});

type PostType = Schema['Post7']['type'];
type FlatModel = ResolvedModel<PostType>;

type DeepPick = DeepPickFromPath<FlatModel, (typeof selSet)[number]>;

type Result = Prettify<UnionToIntersection<DeepPick>>;

type Restored = Prettify<RestoreArrays<Result, FlatModel>>;

type ActualType = typeof posts;

type ExpectedType = {
Expand Down

0 comments on commit 5e693b3

Please sign in to comment.