Skip to content

Commit 43d81af

Browse files
author
Dane Pilcher
authored
feat: lazy loading conditional type (#489)
1 parent cee2789 commit 43d81af

File tree

11 files changed

+1139
-278
lines changed

11 files changed

+1139
-278
lines changed

packages/amplify-codegen/src/commands/models.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const glob = require('glob-all');
55
const { FeatureFlags, pathManager } = require('amplify-cli-core');
66
const gqlCodeGen = require('@graphql-codegen/core');
77
const appSyncDataStoreCodeGen = require('@aws-amplify/appsync-modelgen-plugin');
8+
const { version: packageVersion } = require('../../package.json');
89
const { validateDartSDK } = require('../utils/validateDartSDK');
910
const { validateAmplifyFlutterCapableZeroThreeFeatures } = require('../utils/validateAmplifyFlutterCapableZeroThreeFeatures');
1011
const { validateAmplifyFlutterCoreLibraryDependency } = require('../utils/validateAmplifyFlutterCoreLibraryDependency');
@@ -77,13 +78,13 @@ async function generateModels(context, overrideOutputDir = null, isIntrospection
7778

7879
const schemaContent = loadSchema(apiResourcePath);
7980

80-
const baseOutputDir = path.join(projectRoot, getModelOutputPath(context))
81+
const baseOutputDir = path.join(projectRoot, getModelOutputPath(context));
8182
const schema = parse(schemaContent);
8283
const projectConfig = context.amplify.getProjectConfig();
8384

8485
const generateIndexRules = readFeatureFlag('codegen.generateIndexRules');
8586
const emitAuthProvider = readFeatureFlag('codegen.emitAuthProvider');
86-
const usePipelinedTransformer = readFeatureFlag('graphQLTransformer.useExperimentalPipelinedTransformer')
87+
const usePipelinedTransformer = readFeatureFlag('graphQLTransformer.useExperimentalPipelinedTransformer');
8788
const transformerVersion = readNumericFeatureFlag('graphQLTransformer.transformerVersion');
8889
const respectPrimaryKeyAttributesOnConnectionField = readFeatureFlag('graphQLTransformer.respectPrimaryKeyAttributesOnConnectionField');
8990

@@ -117,7 +118,7 @@ async function generateModels(context, overrideOutputDir = null, isIntrospection
117118
baseOutputDir,
118119
schema,
119120
config: {
120-
target: isIntrospection ? 'introspection' : (platformToLanguageMap[projectConfig.frontend] || projectConfig.frontend),
121+
target: isIntrospection ? 'introspection' : platformToLanguageMap[projectConfig.frontend] || projectConfig.frontend,
121122
directives: directiveDefinitions,
122123
isTimestampFieldsAdded,
123124
emitAuthProvider,
@@ -129,6 +130,7 @@ async function generateModels(context, overrideOutputDir = null, isIntrospection
129130
transformerVersion,
130131
dartUpdateAmplifyCoreDependency,
131132
respectPrimaryKeyAttributesOnConnectionField,
133+
codegenVersion: packageVersion,
132134
overrideOutputDir, // This needs to live under `config` in order for the GraphQL types to work out.
133135
},
134136
});

packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-javascript-visitor.test.ts.snap

Lines changed: 191 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
exports[`Javascript visitor with connected models of custom pk hasMany/belongsTo relation should generate correct declaration for hasMany bi-connection model when custom pk support is enabled 1`] = `
44
"import { ModelInit, MutableModel, __modelMeta__, CompositeIdentifier } from \\"@aws-amplify/datastore\\";
5+
// @ts-ignore
6+
import { LazyLoading, LazyLoadingDisabled, AsyncCollection, AsyncItem } from \\"@aws-amplify/datastore\\";
57
68
79
810
911
10-
export declare class Post {
12+
type EagerPost = {
1113
readonly [__modelMeta__]: {
1214
identifier: CompositeIdentifier<Post, ['customPostId', 'title']>;
1315
readOnlyFields: 'createdAt' | 'updatedAt';
@@ -17,11 +19,27 @@ export declare class Post {
1719
readonly comments?: (Comment | null)[] | null;
1820
readonly createdAt?: string | null;
1921
readonly updatedAt?: string | null;
20-
constructor(init: ModelInit<Post>);
21-
static copyOf(source: Post, mutator: (draft: MutableModel<Post>) => MutableModel<Post> | void): Post;
2222
}
2323
24-
export declare class Comment {
24+
type LazyPost = {
25+
readonly [__modelMeta__]: {
26+
identifier: CompositeIdentifier<Post, ['customPostId', 'title']>;
27+
readOnlyFields: 'createdAt' | 'updatedAt';
28+
};
29+
readonly customPostId: string;
30+
readonly title: string;
31+
readonly comments: AsyncCollection<Comment>;
32+
readonly createdAt?: string | null;
33+
readonly updatedAt?: string | null;
34+
}
35+
36+
export declare type Post = LazyLoading extends LazyLoadingDisabled ? EagerPost : LazyPost
37+
38+
export declare const Post: (new (init: ModelInit<Post>) => Post) & {
39+
copyOf(source: Post, mutator: (draft: MutableModel<Post>) => MutableModel<Post> | void): Post;
40+
}
41+
42+
type EagerComment = {
2543
readonly [__modelMeta__]: {
2644
identifier: CompositeIdentifier<Comment, ['customPostId', 'content']>;
2745
readOnlyFields: 'createdAt' | 'updatedAt';
@@ -33,19 +51,39 @@ export declare class Comment {
3351
readonly updatedAt?: string | null;
3452
readonly postCommentsCustomPostId?: string | null;
3553
readonly postCommentsTitle?: string | null;
36-
constructor(init: ModelInit<Comment>);
37-
static copyOf(source: Comment, mutator: (draft: MutableModel<Comment>) => MutableModel<Comment> | void): Comment;
54+
}
55+
56+
type LazyComment = {
57+
readonly [__modelMeta__]: {
58+
identifier: CompositeIdentifier<Comment, ['customPostId', 'content']>;
59+
readOnlyFields: 'createdAt' | 'updatedAt';
60+
};
61+
readonly customPostId: string;
62+
readonly content: string;
63+
readonly post: AsyncItem<Post | undefined>;
64+
readonly createdAt?: string | null;
65+
readonly updatedAt?: string | null;
66+
readonly postCommentsCustomPostId?: string | null;
67+
readonly postCommentsTitle?: string | null;
68+
}
69+
70+
export declare type Comment = LazyLoading extends LazyLoadingDisabled ? EagerComment : LazyComment
71+
72+
export declare const Comment: (new (init: ModelInit<Comment>) => Comment) & {
73+
copyOf(source: Comment, mutator: (draft: MutableModel<Comment>) => MutableModel<Comment> | void): Comment;
3874
}"
3975
`;
4076
4177
exports[`Javascript visitor with connected models of custom pk hasMany/belongsTo relation should generate correct declaration for hasMany uni-connection model when custom pk support is enabled 1`] = `
4278
"import { ModelInit, MutableModel, __modelMeta__, CompositeIdentifier } from \\"@aws-amplify/datastore\\";
79+
// @ts-ignore
80+
import { LazyLoading, LazyLoadingDisabled, AsyncCollection } from \\"@aws-amplify/datastore\\";
4381
4482
4583
4684
4785
48-
export declare class Post {
86+
type EagerPost = {
4987
readonly [__modelMeta__]: {
5088
identifier: CompositeIdentifier<Post, ['id', 'title']>;
5189
readOnlyFields: 'createdAt' | 'updatedAt';
@@ -55,11 +93,40 @@ export declare class Post {
5593
readonly comments?: (Comment | null)[] | null;
5694
readonly createdAt?: string | null;
5795
readonly updatedAt?: string | null;
58-
constructor(init: ModelInit<Post>);
59-
static copyOf(source: Post, mutator: (draft: MutableModel<Post>) => MutableModel<Post> | void): Post;
6096
}
6197
62-
export declare class Comment {
98+
type LazyPost = {
99+
readonly [__modelMeta__]: {
100+
identifier: CompositeIdentifier<Post, ['id', 'title']>;
101+
readOnlyFields: 'createdAt' | 'updatedAt';
102+
};
103+
readonly id: string;
104+
readonly title: string;
105+
readonly comments: AsyncCollection<Comment>;
106+
readonly createdAt?: string | null;
107+
readonly updatedAt?: string | null;
108+
}
109+
110+
export declare type Post = LazyLoading extends LazyLoadingDisabled ? EagerPost : LazyPost
111+
112+
export declare const Post: (new (init: ModelInit<Post>) => Post) & {
113+
copyOf(source: Post, mutator: (draft: MutableModel<Post>) => MutableModel<Post> | void): Post;
114+
}
115+
116+
type EagerComment = {
117+
readonly [__modelMeta__]: {
118+
identifier: CompositeIdentifier<Comment, ['id', 'content']>;
119+
readOnlyFields: 'createdAt' | 'updatedAt';
120+
};
121+
readonly id: string;
122+
readonly content: string;
123+
readonly createdAt?: string | null;
124+
readonly updatedAt?: string | null;
125+
readonly postCommentsId?: string | null;
126+
readonly postCommentsTitle?: string | null;
127+
}
128+
129+
type LazyComment = {
63130
readonly [__modelMeta__]: {
64131
identifier: CompositeIdentifier<Comment, ['id', 'content']>;
65132
readOnlyFields: 'createdAt' | 'updatedAt';
@@ -70,19 +137,25 @@ export declare class Comment {
70137
readonly updatedAt?: string | null;
71138
readonly postCommentsId?: string | null;
72139
readonly postCommentsTitle?: string | null;
73-
constructor(init: ModelInit<Comment>);
74-
static copyOf(source: Comment, mutator: (draft: MutableModel<Comment>) => MutableModel<Comment> | void): Comment;
140+
}
141+
142+
export declare type Comment = LazyLoading extends LazyLoadingDisabled ? EagerComment : LazyComment
143+
144+
export declare const Comment: (new (init: ModelInit<Comment>) => Comment) & {
145+
copyOf(source: Comment, mutator: (draft: MutableModel<Comment>) => MutableModel<Comment> | void): Comment;
75146
}"
76147
`;
77148
78149
exports[`Javascript visitor with connected models of custom pk hasOne/belongsTo relation should generate correct declaration when custom pk support is enabled 1`] = `
79150
"import { ModelInit, MutableModel, __modelMeta__, CompositeIdentifier } from \\"@aws-amplify/datastore\\";
151+
// @ts-ignore
152+
import { LazyLoading, LazyLoadingDisabled, AsyncItem } from \\"@aws-amplify/datastore\\";
80153
81154
82155
83156
84157
85-
export declare class Project {
158+
type EagerProject = {
86159
readonly [__modelMeta__]: {
87160
identifier: CompositeIdentifier<Project, ['id', 'name']>;
88161
readOnlyFields: 'createdAt' | 'updatedAt';
@@ -94,11 +167,29 @@ export declare class Project {
94167
readonly updatedAt?: string | null;
95168
readonly projectTeamId?: string | null;
96169
readonly projectTeamName?: string | null;
97-
constructor(init: ModelInit<Project>);
98-
static copyOf(source: Project, mutator: (draft: MutableModel<Project>) => MutableModel<Project> | void): Project;
99170
}
100171
101-
export declare class Team {
172+
type LazyProject = {
173+
readonly [__modelMeta__]: {
174+
identifier: CompositeIdentifier<Project, ['id', 'name']>;
175+
readOnlyFields: 'createdAt' | 'updatedAt';
176+
};
177+
readonly id: string;
178+
readonly name: string;
179+
readonly team: AsyncItem<Team | undefined>;
180+
readonly createdAt?: string | null;
181+
readonly updatedAt?: string | null;
182+
readonly projectTeamId?: string | null;
183+
readonly projectTeamName?: string | null;
184+
}
185+
186+
export declare type Project = LazyLoading extends LazyLoadingDisabled ? EagerProject : LazyProject
187+
188+
export declare const Project: (new (init: ModelInit<Project>) => Project) & {
189+
copyOf(source: Project, mutator: (draft: MutableModel<Project>) => MutableModel<Project> | void): Project;
190+
}
191+
192+
type EagerTeam = {
102193
readonly [__modelMeta__]: {
103194
identifier: CompositeIdentifier<Team, ['id', 'name']>;
104195
readOnlyFields: 'createdAt' | 'updatedAt';
@@ -110,19 +201,39 @@ export declare class Team {
110201
readonly updatedAt?: string | null;
111202
readonly teamProjectId?: string | null;
112203
readonly teamProjectName?: string | null;
113-
constructor(init: ModelInit<Team>);
114-
static copyOf(source: Team, mutator: (draft: MutableModel<Team>) => MutableModel<Team> | void): Team;
204+
}
205+
206+
type LazyTeam = {
207+
readonly [__modelMeta__]: {
208+
identifier: CompositeIdentifier<Team, ['id', 'name']>;
209+
readOnlyFields: 'createdAt' | 'updatedAt';
210+
};
211+
readonly id: string;
212+
readonly name: string;
213+
readonly project: AsyncItem<Project | undefined>;
214+
readonly createdAt?: string | null;
215+
readonly updatedAt?: string | null;
216+
readonly teamProjectId?: string | null;
217+
readonly teamProjectName?: string | null;
218+
}
219+
220+
export declare type Team = LazyLoading extends LazyLoadingDisabled ? EagerTeam : LazyTeam
221+
222+
export declare const Team: (new (init: ModelInit<Team>) => Team) & {
223+
copyOf(source: Team, mutator: (draft: MutableModel<Team>) => MutableModel<Team> | void): Team;
115224
}"
116225
`;
117226
118227
exports[`Javascript visitor with connected models of custom pk manyToMany relation should generate correct declaration for manyToMany model when custom pk is enabled 1`] = `
119228
"import { ModelInit, MutableModel, __modelMeta__, CompositeIdentifier, ManagedIdentifier } from \\"@aws-amplify/datastore\\";
229+
// @ts-ignore
230+
import { LazyLoading, LazyLoadingDisabled, AsyncCollection, AsyncItem } from \\"@aws-amplify/datastore\\";
120231
121232
122233
123234
124235
125-
export declare class Post {
236+
type EagerPost = {
126237
readonly [__modelMeta__]: {
127238
identifier: CompositeIdentifier<Post, ['customPostId', 'title']>;
128239
readOnlyFields: 'createdAt' | 'updatedAt';
@@ -133,11 +244,28 @@ export declare class Post {
133244
readonly tags?: (PostTags | null)[] | null;
134245
readonly createdAt?: string | null;
135246
readonly updatedAt?: string | null;
136-
constructor(init: ModelInit<Post>);
137-
static copyOf(source: Post, mutator: (draft: MutableModel<Post>) => MutableModel<Post> | void): Post;
138247
}
139248
140-
export declare class Tag {
249+
type LazyPost = {
250+
readonly [__modelMeta__]: {
251+
identifier: CompositeIdentifier<Post, ['customPostId', 'title']>;
252+
readOnlyFields: 'createdAt' | 'updatedAt';
253+
};
254+
readonly customPostId: string;
255+
readonly title: string;
256+
readonly content?: string | null;
257+
readonly tags: AsyncCollection<PostTags>;
258+
readonly createdAt?: string | null;
259+
readonly updatedAt?: string | null;
260+
}
261+
262+
export declare type Post = LazyLoading extends LazyLoadingDisabled ? EagerPost : LazyPost
263+
264+
export declare const Post: (new (init: ModelInit<Post>) => Post) & {
265+
copyOf(source: Post, mutator: (draft: MutableModel<Post>) => MutableModel<Post> | void): Post;
266+
}
267+
268+
type EagerTag = {
141269
readonly [__modelMeta__]: {
142270
identifier: CompositeIdentifier<Tag, ['customTagId', 'label']>;
143271
readOnlyFields: 'createdAt' | 'updatedAt';
@@ -147,11 +275,27 @@ export declare class Tag {
147275
readonly posts?: (PostTags | null)[] | null;
148276
readonly createdAt?: string | null;
149277
readonly updatedAt?: string | null;
150-
constructor(init: ModelInit<Tag>);
151-
static copyOf(source: Tag, mutator: (draft: MutableModel<Tag>) => MutableModel<Tag> | void): Tag;
152278
}
153279
154-
export declare class PostTags {
280+
type LazyTag = {
281+
readonly [__modelMeta__]: {
282+
identifier: CompositeIdentifier<Tag, ['customTagId', 'label']>;
283+
readOnlyFields: 'createdAt' | 'updatedAt';
284+
};
285+
readonly customTagId: string;
286+
readonly label: string;
287+
readonly posts: AsyncCollection<PostTags>;
288+
readonly createdAt?: string | null;
289+
readonly updatedAt?: string | null;
290+
}
291+
292+
export declare type Tag = LazyLoading extends LazyLoadingDisabled ? EagerTag : LazyTag
293+
294+
export declare const Tag: (new (init: ModelInit<Tag>) => Tag) & {
295+
copyOf(source: Tag, mutator: (draft: MutableModel<Tag>) => MutableModel<Tag> | void): Tag;
296+
}
297+
298+
type EagerPostTags = {
155299
readonly [__modelMeta__]: {
156300
identifier: ManagedIdentifier<PostTags, 'id'>;
157301
readOnlyFields: 'createdAt' | 'updatedAt';
@@ -165,7 +309,27 @@ export declare class PostTags {
165309
readonly tag: Tag;
166310
readonly createdAt?: string | null;
167311
readonly updatedAt?: string | null;
168-
constructor(init: ModelInit<PostTags>);
169-
static copyOf(source: PostTags, mutator: (draft: MutableModel<PostTags>) => MutableModel<PostTags> | void): PostTags;
312+
}
313+
314+
type LazyPostTags = {
315+
readonly [__modelMeta__]: {
316+
identifier: ManagedIdentifier<PostTags, 'id'>;
317+
readOnlyFields: 'createdAt' | 'updatedAt';
318+
};
319+
readonly id: string;
320+
readonly postCustomPostId?: string | null;
321+
readonly posttitle?: string | null;
322+
readonly tagCustomTagId?: string | null;
323+
readonly taglabel?: string | null;
324+
readonly post: AsyncItem<Post>;
325+
readonly tag: AsyncItem<Tag>;
326+
readonly createdAt?: string | null;
327+
readonly updatedAt?: string | null;
328+
}
329+
330+
export declare type PostTags = LazyLoading extends LazyLoadingDisabled ? EagerPostTags : LazyPostTags
331+
332+
export declare const PostTags: (new (init: ModelInit<PostTags>) => PostTags) & {
333+
copyOf(source: PostTags, mutator: (draft: MutableModel<PostTags>) => MutableModel<PostTags> | void): PostTags;
170334
}"
171335
`;

0 commit comments

Comments
 (0)