Skip to content

Commit dd31e18

Browse files
AaronZyLeedependabot[bot]Dane Pilcher
authored
Merge pull request #812 from aws-amplify/merge-main-gen2
* build(deps): bump tar from 6.2.0 to 6.2.1 Bumps [tar](https://github.com/isaacs/node-tar) from 6.2.0 to 6.2.1. - [Release notes](https://github.com/isaacs/node-tar/releases) - [Changelog](https://github.com/isaacs/node-tar/blob/main/CHANGELOG.md) - [Commits](isaacs/node-tar@v6.2.0...v6.2.1) --- updated-dependencies: - dependency-name: tar dependency-type: indirect ... Signed-off-by: dependabot[bot] <[email protected]> * fix: use standalone ajv validation for model introspection schema (#807) * fix: use standalone ajv validation for model introspection schema * test: add unit tests for validator * fix: missing targetNames in hasOne reference --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Dane Pilcher <[email protected]>
2 parents a6efa72 + 02a581f commit dd31e18

File tree

9 files changed

+86
-24
lines changed

9 files changed

+86
-24
lines changed

.eslintrc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ module.exports = {
268268
'/packages/*/CHANGELOG.md',
269269

270270
// Ignore lint in e2e test apps
271-
'test-apps'
271+
'test-apps',
272+
273+
// Ignore lint for standalone JSON validation function
274+
'/packages/appsync-modelgen-plugin/src/validate-cjs.js'
272275
]
273276
};

packages/appsync-modelgen-plugin/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
"codegen"
1919
],
2020
"scripts": {
21-
"build": "tsc",
21+
"build": "yarn generate-schemas && yarn generate-standalone-validation-function && tsc",
2222
"watch": "tsc -w",
2323
"test-watch": "jest --watch",
2424
"test": "jest",
25-
"generate-schemas": "ts-node ./scripts/generateSchemas.ts",
25+
"generate-schemas": "ts-node ./scripts/generateSchemas.ts --overwrite",
26+
"generate-standalone-validation-function": "ts-node ./scripts/generateStandaloneValidationFunction.ts",
2627
"extract-api": "ts-node ../../scripts/extract-api.ts"
2728
},
2829
"dependencies": {
2930
"@graphql-codegen/plugin-helpers": "^1.18.8",
3031
"@graphql-codegen/visitor-plugin-common": "^1.22.0",
3132
"@graphql-tools/utils": "^6.0.18",
32-
"ajv": "^6.10.0",
3333
"chalk": "^3.0.0",
3434
"change-case": "^4.1.1",
3535
"graphql-transformer-common": "^4.25.1",
@@ -45,6 +45,7 @@
4545
"@types/fs-extra": "^8.1.2",
4646
"@types/node": "^12.12.6",
4747
"@types/pluralize": "0.0.29",
48+
"ajv": "^8.12.0",
4849
"graphql": "^15.5.0",
4950
"java-ast": "^0.3.0",
5051
"ts-json-schema-generator": "1.0.0"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Ajv from 'ajv';
2+
import modelIntrospectionSchemaDefinition from '../schemas/introspection/1/ModelIntrospectionSchema.json'
3+
import { join } from 'path';
4+
import { writeFileSync } from 'fs';
5+
6+
const standaloneCode = require("ajv/dist/standalone").default
7+
8+
const ajv = new Ajv({ code: { source: true } });
9+
const validate = ajv.compile(modelIntrospectionSchemaDefinition);
10+
11+
let moduleCode = standaloneCode(ajv, validate)
12+
13+
// Now you can write the module code to file
14+
writeFileSync(join(__dirname, "../src/validate-cjs.js"), moduleCode)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const validateModelIntrospectionSchema = require('../validate-cjs');
2+
import { ModelIntrospectionSchema } from '../interfaces/introspection'
3+
4+
describe('Standalone validation function', () => {
5+
const validSchema: ModelIntrospectionSchema = {
6+
version: 1,
7+
models: {},
8+
nonModels: {},
9+
enums: {},
10+
}
11+
it('should pass on the valid schema', () => {
12+
const result = validateModelIntrospectionSchema(validSchema);
13+
expect(result).toBe(true);
14+
});
15+
describe('should fail on the invalid schema', () => {
16+
it('invalid version', () => {
17+
const schema = {
18+
...validSchema,
19+
version: 100,
20+
};
21+
const result = validateModelIntrospectionSchema(schema);
22+
expect(result).toBe(false);
23+
});
24+
it('invalid fields', () => {
25+
const schema = {
26+
...validSchema,
27+
invalidField: {}
28+
};
29+
const result = validateModelIntrospectionSchema(schema);
30+
expect(result).toBe(false);
31+
});
32+
it('missing required fields', () => {
33+
const schema = {
34+
...validSchema,
35+
};
36+
delete (schema as any).models;
37+
const result = validateModelIntrospectionSchema(schema);
38+
expect(result).toBe(false);
39+
});
40+
});
41+
});

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3692,7 +3692,8 @@ exports[`custom references double linked references 1`] = `
36923692
\\"connectionType\\": \\"HAS_ONE\\",
36933693
\\"associatedWith\\": [
36943694
\\"bar1Id\\"
3695-
]
3695+
],
3696+
\\"targetNames\\": []
36963697
}
36973698
},
36983699
\\"bar2\\": {
@@ -3707,7 +3708,8 @@ exports[`custom references double linked references 1`] = `
37073708
\\"connectionType\\": \\"HAS_ONE\\",
37083709
\\"associatedWith\\": [
37093710
\\"bar2Id\\"
3710-
]
3711+
],
3712+
\\"targetNames\\": []
37113713
}
37123714
},
37133715
\\"createdAt\\": {
@@ -4244,7 +4246,8 @@ exports[`custom references sets the association to the references field for hasO
42444246
\\"connectionType\\": \\"HAS_ONE\\",
42454247
\\"associatedWith\\": [
42464248
\\"primaryId\\"
4247-
]
4249+
],
4250+
\\"targetNames\\": []
42484251
}
42494252
},
42504253
\\"createdAt\\": {
@@ -4467,7 +4470,8 @@ exports[`custom references sets the association to the references field for hasO
44674470
\\"connectionType\\": \\"HAS_ONE\\",
44684471
\\"associatedWith\\": [
44694472
\\"primaryId\\"
4470-
]
4473+
],
4474+
\\"targetNames\\": []
44714475
}
44724476
},
44734477
\\"createdAt\\": {

packages/appsync-modelgen-plugin/src/validate-cjs.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import { GraphQLSchema } from "graphql";
33
import { Argument, AssociationType, Field, Fields, FieldType, ModelAttribute, ModelIntrospectionSchema, PrimaryKeyInfo, SchemaEnum, SchemaModel, SchemaMutation, SchemaNonModel, SchemaQuery, SchemaSubscription, Input, InputFieldType } from "../interfaces/introspection";
44
import { METADATA_SCALAR_MAP } from "../scalars";
55
import { CodeGenConnectionType } from "../utils/process-connections";
6-
import { RawAppSyncModelConfig, ParsedAppSyncModelConfig, AppSyncModelVisitor, CodeGenEnum, CodeGenField, CodeGenModel, CodeGenPrimaryKeyType, CodeGenQuery, CodeGenSubscription, CodeGenMutation, CodeGenInputObject, CodeGenUnion, CodeGenInterface } from "./appsync-visitor";
7-
import fs from 'fs';
8-
import path from 'path';
9-
import Ajv from 'ajv';
10-
import modelIntrospectionSchema from '../schemas/introspection/1/ModelIntrospectionSchema.json';
6+
import { RawAppSyncModelConfig, ParsedAppSyncModelConfig, AppSyncModelVisitor, CodeGenEnum, CodeGenField, CodeGenModel, CodeGenPrimaryKeyType, CodeGenQuery, CodeGenSubscription, CodeGenMutation, CodeGenInputObject } from "./appsync-visitor";
7+
8+
const validateModelIntrospectionSchema = require('../validate-cjs');
119

1210
type UnionFieldType = { union: string };
1311
type InterfaceFieldType = { interface: string };
@@ -19,15 +17,13 @@ export class AppSyncModelIntrospectionVisitor<
1917
TPluginConfig extends ParsedAppSyncModelIntrospectionConfig = ParsedAppSyncModelIntrospectionConfig
2018
> extends AppSyncModelVisitor<TRawConfig, TPluginConfig> {
2119
private readonly introspectionVersion = 1;
22-
private schemaValidator: Ajv.ValidateFunction;
2320
constructor(
2421
schema: GraphQLSchema,
2522
rawConfig: TRawConfig,
2623
additionalConfig: Partial<TPluginConfig>,
2724
defaultScalars: NormalizedScalarsMap = DEFAULT_SCALARS,
2825
) {
2926
super(schema, rawConfig, additionalConfig, defaultScalars);
30-
this.schemaValidator = new Ajv().compile(modelIntrospectionSchema);
3127
}
3228

3329
generate(): string {
@@ -42,8 +38,8 @@ export class AppSyncModelIntrospectionVisitor<
4238
);
4339

4440
const modelIntrosepctionSchema = this.generateModelIntrospectionSchema();
45-
if (!this.schemaValidator(modelIntrosepctionSchema)) {
46-
throw new Error(`Data did not validate against the supplied schema. Underlying errors were ${JSON.stringify(this.schemaValidator.errors)}`);
41+
if (!validateModelIntrospectionSchema(modelIntrosepctionSchema)) {
42+
throw new Error(`Data did not validate against the supplied schema. Underlying errors were ${JSON.stringify(validateModelIntrospectionSchema.errors)}`);
4743
}
4844
return JSON.stringify(modelIntrosepctionSchema, null, 4);
4945
}
@@ -119,7 +115,7 @@ export class AppSyncModelIntrospectionVisitor<
119115
connectionAttribute.associatedWith = connectionInfo.associatedWithFields.map(f => this.getFieldName(f));
120116
} else if (connectionInfo.kind === CodeGenConnectionType.HAS_ONE) {
121117
connectionAttribute.associatedWith = connectionInfo.associatedWithFields.map(f => this.getFieldName(f));
122-
connectionAttribute.targetNames = connectionInfo.targetNames;
118+
connectionAttribute.targetNames = connectionInfo.targetNames ?? [];
123119
} else {
124120
connectionAttribute.targetNames = connectionInfo.targetNames;
125121
}

packages/appsync-modelgen-plugin/tsconfig.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"extends": "../../tsconfig.base.json",
33
"compilerOptions": {
44
"rootDir": "src",
5-
"outDir": "lib"
5+
"outDir": "lib",
6+
"allowJs": true,
67
},
78
"include": [
89
"src/**/*",
@@ -11,6 +12,7 @@
1112
"exclude": [
1213
"scripts",
1314
"lib",
14-
"src/__tests__"
15+
"src/__tests__",
16+
"coverage",
1517
]
1618
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6125,7 +6125,7 @@ ajv@^6.10.0, ajv@^6.10.2, ajv@^6.12.6, ajv@~6.12.6:
61256125
json-schema-traverse "^0.4.1"
61266126
uri-js "^4.2.2"
61276127

6128-
ajv@^8.0.1, ajv@^8.11.0:
6128+
ajv@^8.0.1, ajv@^8.11.0, ajv@^8.12.0:
61296129
version "8.12.0"
61306130
resolved "https://registry.npmjs.org/ajv/-/ajv-8.12.0.tgz#d1a0527323e22f53562c567c00991577dfbe19d1"
61316131
integrity sha512-sRu1kpcO9yLtYxBKvqfTeh9KzZEwO3STyX1HT+4CaDzC6HpTGYhIhPIzj9XuKU7KYDwnaeh5hcOwjy1QuJzBPA==
@@ -13560,9 +13560,9 @@ tar-stream@~2.2.0:
1356013560
readable-stream "^3.1.1"
1356113561

1356213562
tar@^6.1.0, tar@^6.1.11, tar@^6.1.2:
13563-
version "6.2.0"
13564-
resolved "https://registry.npmjs.org/tar/-/tar-6.2.0.tgz#b14ce49a79cb1cd23bc9b016302dea5474493f73"
13565-
integrity sha512-/Wo7DcT0u5HUV486xg675HtjNd3BXZ6xDbzsCUZPt5iw8bTQ63bP0Raut3mvro9u+CUyq7YQd8Cx55fsZXxqLQ==
13563+
version "6.2.1"
13564+
resolved "https://registry.yarnpkg.com/tar/-/tar-6.2.1.tgz#717549c541bc3c2af15751bea94b1dd068d4b03a"
13565+
integrity sha512-DZ4yORTwrbTj/7MZYq2w+/ZFdI6OZ/f9SFHR+71gIVUZhOQPHzVCLpvRnPgyaMpfWxxk/4ONva3GQSyNIKRv6A==
1356613566
dependencies:
1356713567
chownr "^2.0.0"
1356813568
fs-minipass "^2.0.0"

0 commit comments

Comments
 (0)