Skip to content

Commit b71e212

Browse files
committed
test: update examples and tests
1 parent 6124d72 commit b71e212

File tree

5 files changed

+282
-60
lines changed

5 files changed

+282
-60
lines changed

.changeset/silly-falcons-kneel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@aws-amplify/data-schema": minor
3+
---
4+
5+
optimize custom selection set type

examples/client-types-example/app.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,14 @@ import type { Schema } from './resource';
33

44
const client = generateClient<Schema>();
55

6-
async function createPost() {
7-
await client.models.Post.create({
8-
title: 'Hello world',
9-
location: {
10-
lat: 123,
11-
long: 123,
12-
},
13-
});
14-
}
6+
async function createPost() {}
157

168
async function test() {
17-
const {
18-
data: [post],
19-
} = await client.models.Post.list();
20-
21-
const { data } = await client.models.Post.get({ id: 'MyId' });
22-
23-
const { data: data2 } = await client.mutations.myMutation();
24-
25-
type TM = typeof data2;
26-
27-
type TPost = typeof post;
28-
// ^?
9+
const res = await client.models.Network.list({
10+
selectionSet: [
11+
'name',
12+
'articles.*',
13+
'articles.articleOriginalWorks.person.name',
14+
],
15+
});
2916
}
Lines changed: 225 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,230 @@
11
import { a, ClientSchema } from '@aws-amplify/data-schema';
2-
import { __modelMeta__ } from '@aws-amplify/data-schema-types';
3-
import { configure } from '@aws-amplify/data-schema/internals';
2+
import { __modelMeta__, Prettify } from '@aws-amplify/data-schema-types';
3+
import { GraphQLFormattedError } from '@aws-amplify/data-schema/runtime';
44

5-
const schema = configure({
6-
database: { engine: 'mysql', connectionUri: {} as any },
7-
}).schema({
8-
Post: a.model({
9-
title: a.string().required(),
10-
description: a.string(),
11-
location: a.ref('Location').required(),
12-
}),
5+
const masterType = {
6+
id: a.id().required(),
7+
name: a.string().required(),
8+
type: a.string().required(),
9+
sort: a.integer().required(),
10+
};
1311

14-
Location: a.customType({
15-
lat: a.float(),
16-
long: a.float(),
17-
}),
18-
});
12+
const schema = a
13+
.schema({
14+
Network: a
15+
.model({
16+
...masterType,
17+
articles: a.hasMany('Article', 'networkId'),
18+
})
19+
.secondaryIndexes((index) => [
20+
index('type').sortKeys(['sort']).queryField('networkListByTypeAndId'),
21+
]),
22+
Category: a
23+
.model({
24+
...masterType,
25+
articles: a.hasMany('Article', 'categoryId'),
26+
})
27+
.secondaryIndexes((index) => [
28+
index('type').sortKeys(['sort']).queryField('categoryListByTypeAndId'),
29+
]),
30+
Season: a
31+
.model({
32+
...masterType,
33+
articles: a.hasMany('Article', 'seasonId'),
34+
})
35+
.secondaryIndexes((index) => [
36+
index('type').sortKeys(['sort']).queryField('seasonListByTypeAndId'),
37+
]),
38+
Person: a
39+
.model({
40+
...masterType,
41+
articleCasts: a.hasMany('ArticleCast', 'personId'),
42+
articleAuthors: a.hasMany('ArticleAuthor', 'personId'),
43+
articleDirectors: a.hasMany('ArticleDirector', 'personId'),
44+
articleProducers: a.hasMany('ArticleProducer', 'personId'),
45+
articleScreenwriters: a.hasMany('ArticleScreenwriter', 'personId'),
46+
articleOriginalWorks: a.hasMany('ArticleOriginalWork', 'personId'),
47+
image: a.string(),
48+
})
49+
.secondaryIndexes((index) => [
50+
index('type').sortKeys(['sort']).queryField('personListByTypeAndId'),
51+
]),
52+
ArticleVod: a
53+
.model({
54+
articleId: a.id().required(),
55+
vodId: a.id().required(),
56+
vod: a.belongsTo('Vod', 'vodId'),
57+
article: a.belongsTo('Article', 'articleId'),
58+
})
59+
.identifier(['articleId', 'vodId']),
60+
Vod: a
61+
.model({
62+
...masterType,
63+
articles: a.hasMany('ArticleVod', 'vodId'),
64+
microcopy: a.string(),
65+
url: a.string(),
66+
})
67+
.secondaryIndexes((index) => [
68+
index('type').sortKeys(['sort']).queryField('vodListByTypeAndId'),
69+
]),
70+
ArticleCast: a
71+
.model({
72+
articleId: a.id().required(),
73+
personId: a.id().required(),
74+
roleName: a.string().required(),
75+
person: a.belongsTo('Person', 'personId'),
76+
article: a.belongsTo('Article', 'articleId'),
77+
})
78+
.identifier(['articleId', 'personId']),
79+
ArticleAuthor: a
80+
.model({
81+
articleId: a.id().required(),
82+
personId: a.id().required(),
83+
person: a.belongsTo('Person', 'personId'),
84+
article: a.belongsTo('Article', 'articleId'),
85+
})
86+
.identifier(['articleId', 'personId']),
87+
ArticleDirector: a
88+
.model({
89+
articleId: a.id().required(),
90+
personId: a.id().required(),
91+
person: a.belongsTo('Person', 'personId'),
92+
article: a.belongsTo('Article', 'articleId'),
93+
})
94+
.identifier(['articleId', 'personId']),
95+
ArticleProducer: a
96+
.model({
97+
articleId: a.id().required(),
98+
personId: a.id().required(),
99+
person: a.belongsTo('Person', 'personId'),
100+
article: a.belongsTo('Article', 'articleId'),
101+
})
102+
.identifier(['articleId', 'personId']),
103+
ArticleScreenwriter: a
104+
.model({
105+
articleId: a.id().required(),
106+
personId: a.id().required(),
107+
person: a.belongsTo('Person', 'personId'),
108+
article: a.belongsTo('Article', 'articleId'),
109+
})
110+
.identifier(['articleId', 'personId']),
111+
ArticleOriginalWork: a
112+
.model({
113+
articleId: a.id().required(),
114+
personId: a.id().required(),
115+
person: a.belongsTo('Person', 'personId'),
116+
article: a.belongsTo('Article', 'articleId'),
117+
})
118+
.identifier(['articleId', 'personId']),
119+
ArticleMusic: a
120+
.model({
121+
type: a.string().required(),
122+
articleId: a.id().required(),
123+
article: a.belongsTo('Article', 'articleId'),
124+
course: a.integer().required(),
125+
opArtist: a.string(),
126+
opSong: a.string(),
127+
edArtist: a.string(),
128+
edSong: a.string(),
129+
otherArtist: a.string(),
130+
otherSon: a.string(),
131+
sort: a.integer().required(), // articleId
132+
})
133+
.identifier(['articleId', 'course'])
134+
.secondaryIndexes((index) => [
135+
index('type')
136+
.sortKeys(['sort', 'course'])
137+
.queryField('musicListByTypeAndSortCourse'),
138+
]),
139+
PageSetting: a
140+
.model({
141+
articleId: a.id().required(),
142+
type: a.string().required(), // eg: anime-CAROUSEL / anime-SPOTLIGHT
143+
article: a.belongsTo('Article', 'articleId'),
144+
sort: a.integer().required(),
145+
})
146+
.identifier(['articleId', 'type'])
147+
.secondaryIndexes((index) => [
148+
index('type').sortKeys(['sort']).queryField('settingListByTypeAndSort'),
149+
]),
150+
Article: a
151+
.model({
152+
id: a.id().required(),
153+
genreType: a.enum(['movie', 'drama', 'variety', 'anime']),
154+
tagType: a.enum(['root', 'series', 'episode']),
155+
pathName: a.string().required(), // eg: spy_family | spy_family/season1 | spy_family/season1/episode1
156+
parentId: a.id(),
157+
childs: a.hasMany('Article', 'parentId'),
158+
parent: a.belongsTo('Article', 'parentId'),
159+
pageSetting: a.hasMany('PageSetting', 'articleId'),
160+
title: a.string().required(),
161+
titleMeta: a.string(),
162+
descriptionMeta: a.string(),
163+
networkId: a.id().required(),
164+
network: a.belongsTo('Network', 'networkId'),
165+
seasonId: a.id(),
166+
season: a.belongsTo('Season', 'seasonId'),
167+
thumbnail: a.string(),
168+
vods: a.hasMany('ArticleVod', 'articleId'),
169+
categoryId: a.id().required(),
170+
category: a.belongsTo('Category', 'categoryId'),
171+
summary: a.customType({
172+
title: a.string(),
173+
text: a.string(),
174+
}),
175+
authors: a.hasMany('ArticleAuthor', 'articleId'),
176+
authorOrganiation: a.string(),
177+
directors: a.hasMany('ArticleDirector', 'articleId'),
178+
producers: a.hasMany('ArticleProducer', 'articleId'),
179+
screenwriters: a.hasMany('ArticleScreenwriter', 'articleId'),
180+
staff: a.string(),
181+
production: a.string().array(),
182+
casts: a.hasMany('ArticleCast', 'articleId'),
183+
sns: a.url().array(),
184+
durationTime: a.string(),
185+
seriesNumber: a.string(),
186+
publisher: a.string(),
187+
otherPublisher: a.string(),
188+
website: a.url(),
189+
articleOriginalWorks: a.hasMany('ArticleOriginalWork', 'articleId'),
190+
originalWorkOrganization: a.string(),
191+
label: a.string(),
192+
durationPeriod: a.string(),
193+
volume: a.string(),
194+
content: a.customType({
195+
genre: a.string(),
196+
subgenre: a.string(),
197+
}),
198+
distributor: a.string(),
199+
distributorOverseas: a.string(),
200+
copyright: a.string(),
201+
productionYear: a.string(),
202+
musics: a.hasMany('ArticleMusic', 'articleId'),
203+
video: a.customType({
204+
text: a.string(),
205+
url: a.url(),
206+
}),
207+
sort: a.integer().required(),
208+
})
209+
.secondaryIndexes((index) => [
210+
index('genreType')
211+
.sortKeys(['sort'])
212+
.queryField('listByGenreTypeAndSort'),
213+
index('parentId')
214+
.sortKeys(['sort'])
215+
.queryField('listByParentIdAndSort'),
216+
index('seasonId')
217+
.sortKeys(['genreType', 'tagType', 'sort'])
218+
.queryField('listBySeasonIdAndTypeSort'),
219+
index('categoryId')
220+
.sortKeys(['genreType', 'tagType', 'sort'])
221+
.queryField('listByCategoryIdAndTypeSort'),
222+
index('pathName').queryField('listByPathName'),
223+
]),
224+
})
225+
.authorization((allow) => [allow.publicApiKey()]);
19226

20-
const s2 = schema.addMutations({
21-
myMutation: a.mutation().returns(a.ref('Post')),
22-
});
227+
export type Schema = ClientSchema<typeof schema>;
23228

24-
export type Schema = ClientSchema<typeof s2>;
229+
type TN = Prettify<Schema['Network']['type']>;
230+
type TNF = Prettify<Schema['Network']['__meta']['flatModel']>;

packages/integration-tests/__tests__/defined-behavior/1-patterns/read-data.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ describe('Read application data', () => {
356356

357357
const client = generateClient<Schema>();
358358

359+
type T = Schema['Blog']['__meta']['flatModel'];
360+
359361
// same way for all CRUDL: .create, .get, .update, .delete, .list, .observeQuery
360362
const { data: blogWithSubsetOfData, errors } = await client.models.Blog.get(
361363
{ id: '<MY_BLOG_ID>' },

0 commit comments

Comments
 (0)