Skip to content

Commit 3c30a15

Browse files
authored
Merge pull request #580 from aws-amplify/main
Release Codegen Utility
2 parents 193dd60 + db1b20b commit 3c30a15

File tree

6 files changed

+110
-65
lines changed

6 files changed

+110
-65
lines changed

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"verdaccio-publish": "yarn verdaccio-clean && yarn publish-to-verdaccio",
2424
"verdaccio-disconnect": "source .circleci/local_publish_helpers.sh && unsetNpmRegistryUrl",
2525
"verdaccio-stop": "kill -9 $(lsof -n -t -iTCP:4873 -sTCP:LISTEN)",
26-
"setup-dev": "rm -rf yarn.lock && (yarn && lerna run build) && yarn add-cli-no-save && (yarn hoist-cli && yarn rm-dev-link && yarn link-dev)",
27-
"add-cli-no-save": "yarn add @aws-amplify/amplify-category-api @aws-amplify/cli-internal -W && git restore package.json yarn.lock",
26+
"setup-dev": "(yarn && lerna run build) && yarn add-cli-no-save && (yarn hoist-cli && yarn rm-dev-link && yarn link-dev)",
27+
"add-cli-no-save": "yarn add @aws-amplify/amplify-category-api @aws-amplify/cli-internal -W && git checkout -- package.json yarn.lock",
2828
"hoist-cli": "rm -rf node_modules/amplify-cli-internal && mkdir node_modules/amplify-cli-internal && cp -r node_modules/@aws-amplify/cli-internal/ node_modules/amplify-cli-internal",
2929
"link-dev": "cd node_modules/amplify-cli-internal && ln -s \"$(pwd)/bin/amplify\" \"$(yarn global bin)/amplify-dev\" && cd -",
3030
"rm-dev-link": "rm -f \"$(yarn global bin)/amplify-dev\"",
@@ -59,7 +59,7 @@
5959
"packages/*"
6060
],
6161
"devDependencies": {
62-
"@aws-amplify/amplify-cli-core": "^4.0.0",
62+
"@aws-amplify/amplify-cli-core": "^4.0.4",
6363
"@commitlint/cli": "^17.0.3",
6464
"@commitlint/config-conventional": "^17.0.3",
6565
"@commitlint/config-lerna-scopes": "^17.0.2",
@@ -107,7 +107,8 @@
107107
"cross-fetch": "^2.2.6",
108108
"glob-parent": "^6.0.2",
109109
"parse-url": "^8.1.0",
110-
"graphql": "15.8.0"
110+
"graphql": "15.8.0",
111+
"xml2js": "0.5.0"
111112
},
112113
"config": {
113114
"commitizen": {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type Post @model {
2+
postId: ID! @primaryKey
3+
node: PostNode! @belongsTo(fields: ["postId"])
4+
title: String!
5+
}
6+
7+
type PostNode @model {
8+
id: ID!
9+
post: Post! @hasOne
10+
}

packages/amplify-codegen-e2e-tests/src/__tests__/model-introspection-codegen.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('Model Introspection Codegen test', () => {
2828
// Model introspection is generated at correct location
2929
expect(isNotEmptyDir(join(projectRoot, outputDir))).toBe(true);
3030
});
31+
3132
it('should throw error when the output directory is not defined in the command', async () => {
3233
// init project and add API category
3334
await initJSProjectWithProfile(projectRoot);
@@ -36,6 +37,7 @@ describe('Model Introspection Codegen test', () => {
3637
//generate introspection schema
3738
await expect(generateModelIntrospection(projectRoot)).rejects.toThrowError();
3839
});
40+
3941
it('should throw error if the GraphQL schema is invalid', async () => {
4042
const invalidSchema = 'modelgen/model_gen_schema_with_errors.graphql';
4143
// init project and add API category
@@ -46,5 +48,20 @@ describe('Model Introspection Codegen test', () => {
4648
//generate introspection schema
4749
await expect(generateModelIntrospection(projectRoot, { outputDir })).rejects.toThrowError();
4850
});
51+
52+
it(`should handle a schema with connected PK`, async () => {
53+
const schemaName = 'modelgen/schema_with_connected_pk.graphql';
54+
55+
// init project and add API category
56+
await initJSProjectWithProfile(projectRoot);
57+
await addApiWithoutSchema(projectRoot, { apiName });
58+
await updateApiSchema(projectRoot, apiName, schemaName);
59+
60+
const outputDir = 'output';
61+
//generate introspection schema
62+
await expect(generateModelIntrospection(projectRoot, { outputDir })).resolves.not.toThrow();
63+
// Model introspection is generated at correct location
64+
expect(isNotEmptyDir(join(projectRoot, outputDir))).toBe(true);
65+
});
4966
});
5067

packages/appsync-modelgen-plugin/src/utils/fieldUtils.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,19 @@ export function getOtherSideBelongsToField(type: string, otherSideModel: CodeGen
2525
return otherSideModel.fields.filter(f => f.type === type).find(f => f.directives.find(d => d.name === TransformerV2DirectiveName.BELONGS_TO));
2626
}
2727

28+
/**
29+
* Given a model, it returns the primary and sort key fields if present, an empty list otherwise.
30+
* @param model Codegen Model object
31+
* @returns Array of primary and sort key codegen fields if present or an empty list
32+
*/
2833
export function getModelPrimaryKeyComponentFields(model: CodeGenModel): CodeGenField[] {
29-
const primaryKeyField = model.fields.find(field => field.primaryKeyInfo)!;
30-
const { sortKeyFields } = primaryKeyField.primaryKeyInfo!;
31-
return [ primaryKeyField, ...sortKeyFields ];
34+
const primaryKeyField = model.fields.find(field => field.primaryKeyInfo);
35+
const keyFields: CodeGenField[] = [];
36+
if (primaryKeyField) {
37+
keyFields.push(primaryKeyField);
38+
if ( primaryKeyField?.primaryKeyInfo?.sortKeyFields ) {
39+
keyFields.push(...primaryKeyField.primaryKeyInfo.sortKeyFields);
40+
};
41+
}
42+
return keyFields;
3243
}

packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -941,15 +941,17 @@ export class AppSyncModelVisitor<
941941
} else if (connectionInfo.kind === CodeGenConnectionType.HAS_ONE) {
942942
if (isCustomPKEnabled) {
943943
const connectedModelFields = getModelPrimaryKeyComponentFields(connectionInfo.connectedModel);
944-
connectionInfo.targetNames.forEach((target, index) => {
945-
addFieldToModel(model, {
946-
name: target,
947-
directives: [],
948-
type: connectedModelFields[index].type,
949-
isList: false,
950-
isNullable: field.isNullable,
944+
if (connectedModelFields?.length > 0) {
945+
connectionInfo.targetNames.forEach((target, index) => {
946+
addFieldToModel(model, {
947+
name: target,
948+
directives: [],
949+
type: connectedModelFields[index].type,
950+
isList: false,
951+
isNullable: field.isNullable,
952+
});
951953
});
952-
});
954+
}
953955
} else {
954956
addFieldToModel(model, {
955957
name: connectionInfo.targetName,
@@ -962,15 +964,17 @@ export class AppSyncModelVisitor<
962964
} else if (connectionInfo.kind === CodeGenConnectionType.BELONGS_TO) {
963965
if (isCustomPKEnabled) {
964966
const connectedModelFields = getModelPrimaryKeyComponentFields(connectionInfo.connectedModel);
965-
connectionInfo.targetNames.forEach((target, index) => {
966-
addFieldToModel(model, {
967-
name: target,
968-
directives: [],
969-
type: connectedModelFields[index].type,
970-
isList: false,
971-
isNullable: field.isNullable,
967+
if (connectedModelFields?.length > 0) {
968+
connectionInfo.targetNames.forEach((target, index) => {
969+
addFieldToModel(model, {
970+
name: target,
971+
directives: [],
972+
type: connectedModelFields[index].type,
973+
isList: false,
974+
isNullable: field.isNullable,
975+
});
972976
});
973-
});
977+
}
974978
} else {
975979
addFieldToModel(model, {
976980
name: connectionInfo.targetName,
@@ -1022,6 +1026,8 @@ export class AppSyncModelVisitor<
10221026
}
10231027
} else {
10241028
Object.values(this.modelMap).forEach(model => {
1029+
const primaryKeyFields = getModelPrimaryKeyComponentFields(model);
1030+
const primaryKeyName = (primaryKeyFields?.length > 0) ? this.getFieldName(primaryKeyFields[0]) : undefined;
10251031
model.fields.forEach(field => {
10261032
const connectionInfo = field.connectionInfo;
10271033
if (
@@ -1030,7 +1036,7 @@ export class AppSyncModelVisitor<
10301036
connectionInfo.kind !== CodeGenConnectionType.HAS_ONE &&
10311037
connectionInfo.targetName !== 'id' &&
10321038
!(this.config.target === 'introspection' &&
1033-
this.getFieldName(getModelPrimaryKeyComponentFields(model)[0]) === connectionInfo.targetName)
1039+
primaryKeyName && primaryKeyName === connectionInfo.targetName)
10341040
) {
10351041
// Need to remove the field that is targetName
10361042
removeFieldFromModel(model, connectionInfo.targetName);

yarn.lock

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,32 @@
4040
signedsource "^1.0.0"
4141
yargs "^15.3.1"
4242

43-
"@aws-amplify/amplify-cli-core@^4.0.0":
44-
version "4.0.1"
45-
resolved "https://registry.npmjs.org/@aws-amplify/amplify-cli-core/-/amplify-cli-core-4.0.1.tgz#e48c3ceb727fd8ec12c9b756abff0f1978842ccf"
46-
integrity sha512-MqTdJCGN/ZtHk2eM8gXLQfuOREVfUwvfDmbKIHWkworfVSxAuHznfItu+GtYJbjZBsR/iu8n56wev/LRzWQicg==
43+
"@aws-amplify/amplify-cli-core@^4.0.4":
44+
version "4.0.4"
45+
resolved "https://registry.npmjs.org/@aws-amplify/amplify-cli-core/-/amplify-cli-core-4.0.4.tgz#6c00c3e847a39c2627e6f62375863d655ed5eedc"
46+
integrity sha512-LwHV9e1Q+wSddYr9rHl1TXU9DBRePy3C6ewCMqdRAY0ys2qp6loyyZbU+eppHBTubpxE+/nrc42Im2uii//MzQ==
4747
dependencies:
48-
amplify-cli-core "4.0.1"
48+
amplify-cli-core "4.0.4"
4949

50-
"@aws-amplify/[email protected].0":
51-
version "1.3.0"
52-
resolved "https://registry.npmjs.org/@aws-amplify/amplify-cli-logger/-/amplify-cli-logger-1.3.0.tgz#7bc68710837a5ecb2f990a38f8103555150f9213"
53-
integrity sha512-ijfo6YY4mgn38HD7KId/MgGJla7oCod4n0/gTVPVHfr9zlpUC8B2L/QuydFjpLBR8LrBYO4xfrQa8UhelyPzGA==
50+
"@aws-amplify/[email protected].2":
51+
version "1.3.2"
52+
resolved "https://registry.npmjs.org/@aws-amplify/amplify-cli-logger/-/amplify-cli-logger-1.3.2.tgz#802667e38276361b78d138a770f6e9f1a009d3a3"
53+
integrity sha512-dQ7Dwh2MNIjhnqx+c48aDjsrsTMU391n23sF5fRyxoC+tmxvaMbz3N3g6XeciwNb1OB5zMBI5zCAjbm94k1S+A==
5454
dependencies:
5555
winston "^3.3.3"
5656
winston-daily-rotate-file "^4.5.0"
5757

58-
"@aws-amplify/[email protected].0":
59-
version "1.2.0"
60-
resolved "https://registry.npmjs.org/@aws-amplify/amplify-cli-shared-interfaces/-/amplify-cli-shared-interfaces-1.2.0.tgz#0a52e3e401a0e802cc3804a23f0dca350aac55ca"
61-
integrity sha512-0nBFF+rFdQCM1zJUZ8z6+vfqAACjcuzl1noMFBjMfRW1FFGfsVKqRuxJYOYv57GKA7FRrHQfrh6pa6qUAh/odA==
58+
"@aws-amplify/[email protected].2":
59+
version "1.2.2"
60+
resolved "https://registry.npmjs.org/@aws-amplify/amplify-cli-shared-interfaces/-/amplify-cli-shared-interfaces-1.2.2.tgz#e717ef421379837519ed6cba42226874574fe505"
61+
integrity sha512-iZZAhfPUBRZr5b4S5YnpN98oYQfZSMMxo0J/fpDQ7mh3CFOGdWcg6vIU3M9tqGZWUNYi3P0MkzreOgDE+9yJ6A==
6262

63-
"@aws-amplify/[email protected].6":
64-
version "2.6.6"
65-
resolved "https://registry.npmjs.org/@aws-amplify/amplify-prompts/-/amplify-prompts-2.6.6.tgz#ca14b5c5cbfe3f6d78cc9caa409c2459203b4b13"
66-
integrity sha512-JvIgSGu+7ubO16Y52dGVTIZAKhgjd1iF7qX4vDkdak+exg+mJY96jx9EwVbNelkgPwauk6CzM3ZiGU52v1g2nw==
63+
"@aws-amplify/[email protected].8":
64+
version "2.6.8"
65+
resolved "https://registry.npmjs.org/@aws-amplify/amplify-prompts/-/amplify-prompts-2.6.8.tgz#9f5975ffa76d4ea3187b053f39c0d99e96d0d342"
66+
integrity sha512-s3xtMgVOWlxyIPPTXF1s1BD/4r7jTEO1vsPEc//HrlB3hPJdZ6cKrDb3cXsbkGUJnrmU6UwEmManwNozrmKzWg==
6767
dependencies:
68-
amplify-prompts "2.6.6"
68+
amplify-prompts "2.6.8"
6969

7070
"@aws-amplify/[email protected]":
7171
version "5.2.31"
@@ -6024,13 +6024,13 @@ [email protected]:
60246024
isomorphic-unfetch "^3.0.0"
60256025
js-cookie "^2.2.1"
60266026

6027-
6028-
version "4.0.1"
6029-
resolved "https://registry.npmjs.org/amplify-cli-core/-/amplify-cli-core-4.0.1.tgz#86167d3c54264448110c95ce872d6c338b911975"
6030-
integrity sha512-NkpAW74flRrXDIF+kXL7kd39gkqphTEJuqj8mLgbv/AfCl3lpC55QsYA4aMOmeRYoxd8f7RDcbAA3VoU3ldOdQ==
6027+
6028+
version "4.0.4"
6029+
resolved "https://registry.npmjs.org/amplify-cli-core/-/amplify-cli-core-4.0.4.tgz#02fef5a69753814d1c85b04ba1c9eafea7eea010"
6030+
integrity sha512-gnbBNcbuB9CisbVn1PfXjc38EyhywtqA3UgYPUPBx+u/zgJU48npAi5VHJUxY+d+PPFnQW5UPgnHAZAp5OA8Eg==
60316031
dependencies:
6032-
"@aws-amplify/amplify-cli-logger" "1.3.0"
6033-
"@aws-amplify/amplify-prompts" "2.6.6"
6032+
"@aws-amplify/amplify-cli-logger" "1.3.2"
6033+
"@aws-amplify/amplify-prompts" "2.6.8"
60346034
"@aws-amplify/graphql-transformer-interfaces" "^2.1.1"
60356035
"@yarnpkg/lockfile" "^1.1.0"
60366036
ajv "^6.12.6"
@@ -6063,12 +6063,12 @@ amplify-headless-interface@^1.13.1:
60636063
resolved "https://registry.npmjs.org/amplify-headless-interface/-/amplify-headless-interface-1.17.1.tgz#37ffe7b0d51d91a3830500a6b138290dbe1244c8"
60646064
integrity sha512-sieL6xE4srcwqJ1LxzDrcI12uNGm9YoVi52hZUc//WbOGp5o0Qt7fmtI/zYqmiRA0tHiiL9wBwdsOnyhJ60twA==
60656065

6066-
6067-
version "2.6.6"
6068-
resolved "https://registry.npmjs.org/amplify-prompts/-/amplify-prompts-2.6.6.tgz#c2d7fc84ba44a188869f7640d6123641fb25dc0c"
6069-
integrity sha512-I12JHBks7VqUX1Lg2XVtFnDph8o22d5JR0NO+YwoKJBI6qyy5R10Vb/SLFIOnwIX6EC27yb1ZKKgDTJCS6V/jQ==
6066+
6067+
version "2.6.8"
6068+
resolved "https://registry.npmjs.org/amplify-prompts/-/amplify-prompts-2.6.8.tgz#4eeee956eee7393b4bf932005f872a4681a568a3"
6069+
integrity sha512-piWttrnUwswHpVb6LPUlrAg3O24jaOJvLxzNBXu4VxkmB5ASKdHBIp2+EnujiGT3QkRDhkdLARw+MFuXfjnolA==
60706070
dependencies:
6071-
"@aws-amplify/amplify-cli-shared-interfaces" "1.2.0"
6071+
"@aws-amplify/amplify-cli-shared-interfaces" "1.2.2"
60726072
chalk "^4.1.1"
60736073
enquirer "^2.3.6"
60746074

@@ -14167,9 +14167,9 @@ [email protected]:
1416714167
integrity sha512-Z6Uz+TYwEqE7ZN50gwn+1LCVo9ZVrpxRPOhOLnncYkY1ZzOYtrX8Fwf/rFktZ8R5mJms6EZf5TqNOMeZmnPq9Q==
1416814168

1416914169
vm2@^3.9.8:
14170-
version "3.9.15"
14171-
resolved "https://registry.yarnpkg.com/vm2/-/vm2-3.9.15.tgz#c544e6a9bc31e4e40d2e5f532342cf799ea56a6e"
14172-
integrity sha512-XqNqknHGw2avJo13gbIwLNZUumvrSHc9mLqoadFZTpo3KaNEJoe1I0lqTFhRXmXD7WkLyG01aaraXdXT0pa4ag==
14170+
version "3.9.17"
14171+
resolved "https://registry.yarnpkg.com/vm2/-/vm2-3.9.17.tgz#251b165ff8a0e034942b5181057305e39570aeab"
14172+
integrity sha512-AqwtCnZ/ERcX+AVj9vUsphY56YANXxRuqMb7GsDtAr0m0PcQX3u0Aj3KWiXM0YAHy7i6JEeHrwOnwXbGYgRpAw==
1417314173
dependencies:
1417414174
acorn "^8.7.0"
1417514175
acorn-walk "^8.2.0"
@@ -14504,23 +14504,23 @@ xml-name-validator@^3.0.0:
1450414504
resolved "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-3.0.0.tgz#6ae73e06de4d8c6e47f9fb181f78d648ad457c6a"
1450514505
integrity sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw==
1450614506

14507-
14508-
version "0.4.19"
14509-
resolved "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz#686c20f213209e94abf0d1bcf1efaa291c7827a7"
14510-
integrity sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==
14507+
14508+
version "0.5.0"
14509+
resolved "https://registry.npmjs.org/xml2js/-/xml2js-0.5.0.tgz#d9440631fbb2ed800203fad106f2724f62c493b7"
14510+
integrity sha512-drPFnkQJik/O+uPKpqSgr22mpuFHqKdbS835iAQrUC73L2F5WkboIRd63ai/2Yg6I1jzifPFKH2NTK+cfglkIA==
1451114511
dependencies:
1451214512
sax ">=0.6.0"
14513-
xmlbuilder "~9.0.1"
14513+
xmlbuilder "~11.0.0"
1451414514

1451514515
xml@^1.0.1:
1451614516
version "1.0.1"
1451714517
resolved "https://registry.npmjs.org/xml/-/xml-1.0.1.tgz#78ba72020029c5bc87b8a81a3cfcd74b4a2fc1e5"
1451814518
integrity sha512-huCv9IH9Tcf95zuYCsQraZtWnJvBtLVE0QHMOs8bWyZAFZNDcYjsPq1nEx8jKA9y+Beo9v+7OBPRisQTjinQMw==
1451914519

14520-
xmlbuilder@~9.0.1:
14521-
version "9.0.7"
14522-
resolved "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz#132ee63d2ec5565c557e20f4c22df9aca686b10d"
14523-
integrity sha512-7YXTQc3P2l9+0rjaUbLwMKRhtmwg1M1eDf6nag7urC7pIPYLD9W/jmzQ4ptRSUbodw5S0jfoGTflLemQibSpeQ==
14520+
xmlbuilder@~11.0.0:
14521+
version "11.0.1"
14522+
resolved "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz#be9bae1c8a046e76b31127726347d0ad7002beb3"
14523+
integrity sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==
1452414524

1452514525
xmlchars@^2.2.0:
1452614526
version "2.2.0"

0 commit comments

Comments
 (0)