Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add validate directive and transformer package #3103

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
"@aws-amplify/data-construct/@aws-amplify/graphql-transformer",
"@aws-amplify/data-construct/@aws-amplify/graphql-transformer-core",
"@aws-amplify/data-construct/@aws-amplify/graphql-transformer-interfaces",
"@aws-amplify/data-construct/@aws-amplify/graphql-validate-transformer",
"@aws-amplify/data-construct/@aws-amplify/platform-core",
"@aws-amplify/data-construct/@aws-amplify/plugin-types",
"@aws-amplify/data-construct/@aws-crypto/crc32",
Expand Down Expand Up @@ -249,6 +250,7 @@
"@aws-amplify/graphql-api-construct/@aws-amplify/graphql-transformer",
"@aws-amplify/graphql-api-construct/@aws-amplify/graphql-transformer-core",
"@aws-amplify/graphql-api-construct/@aws-amplify/graphql-transformer-interfaces",
"@aws-amplify/graphql-api-construct/@aws-amplify/graphql-validate-transformer",
"@aws-amplify/graphql-api-construct/@aws-amplify/platform-core",
"@aws-amplify/graphql-api-construct/@aws-amplify/plugin-types",
"@aws-amplify/graphql-api-construct/@aws-crypto/crc32",
Expand Down
3 changes: 2 additions & 1 deletion packages/amplify-data-construct/.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@aws-amplify/graphql-transformer": "2.2.4",
"@aws-amplify/graphql-transformer-core": "3.3.3",
"@aws-amplify/graphql-transformer-interfaces": "4.2.1",
"@aws-amplify/graphql-validate-transformer": "1.0.0",
"@aws-amplify/platform-core": "^1.0.0",
"@aws-amplify/plugin-types": "^1.0.0",
"@aws-crypto/crc32": "5.2.0",
Expand Down Expand Up @@ -4066,5 +4067,5 @@
},
"types": {},
"version": "1.14.5",
"fingerprint": "h8JgEE53f8Elm8wHbFGYQeS2+vzBXu7a1e+D3qfivKw="
"fingerprint": "Nwx2x2+l7rE9t4xzhhMADaygR5P8Fp+OmCex+8NDpws="
}
2 changes: 2 additions & 0 deletions packages/amplify-data-construct/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@aws-amplify/graphql-transformer",
"@aws-amplify/graphql-transformer-core",
"@aws-amplify/graphql-transformer-interfaces",
"@aws-amplify/graphql-validate-transformer",
"@aws-amplify/platform-core",
"@aws-amplify/plugin-types",
"@aws-crypto/crc32",
Expand Down Expand Up @@ -178,6 +179,7 @@
"@aws-amplify/graphql-transformer": "2.2.4",
"@aws-amplify/graphql-transformer-core": "3.3.3",
"@aws-amplify/graphql-transformer-interfaces": "4.2.1",
"@aws-amplify/graphql-validate-transformer": "1.0.0",
"@aws-amplify/platform-core": "^1.0.0",
"@aws-amplify/plugin-types": "^1.0.0",
"@aws-crypto/crc32": "5.2.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/amplify-graphql-api-construct/.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@aws-amplify/graphql-transformer": "2.2.4",
"@aws-amplify/graphql-transformer-core": "3.3.3",
"@aws-amplify/graphql-transformer-interfaces": "4.2.1",
"@aws-amplify/graphql-validate-transformer": "1.0.0",
"@aws-amplify/platform-core": "^1.0.0",
"@aws-amplify/plugin-types": "^1.0.0",
"@aws-crypto/crc32": "5.2.0",
Expand Down Expand Up @@ -9404,5 +9405,5 @@
}
},
"version": "1.18.5",
"fingerprint": "CzW0HJtSQYy+98fjwfOr6nhSU0bNVsf8C7SJgZiyL2Y="
"fingerprint": "33y/nxWu153mmPZyPp+CsQX4QFgH8aCfNPChuU79bDE="
}
2 changes: 2 additions & 0 deletions packages/amplify-graphql-api-construct/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@aws-amplify/graphql-transformer",
"@aws-amplify/graphql-transformer-core",
"@aws-amplify/graphql-transformer-interfaces",
"@aws-amplify/graphql-validate-transformer",
"@aws-amplify/platform-core",
"@aws-amplify/plugin-types",
"@aws-crypto/crc32",
Expand Down Expand Up @@ -178,6 +179,7 @@
"@aws-amplify/graphql-transformer": "2.2.4",
"@aws-amplify/graphql-transformer-core": "3.3.3",
"@aws-amplify/graphql-transformer-interfaces": "4.2.1",
"@aws-amplify/graphql-validate-transformer": "1.0.0",
"@aws-amplify/platform-core": "^1.0.0",
"@aws-amplify/plugin-types": "^1.0.0",
"@aws-crypto/crc32": "5.2.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/amplify-graphql-directives/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ export const V1Directives: readonly Directive[];
// @public (undocumented)
export const V2Directives: readonly Directive[];

// @public (undocumented)
export const ValidateDirective: Directive;

// @public (undocumented)
export const VersionedDirectiveV1: Directive<VersionedDirectiveV1Defaults>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,32 @@ Object {
}
`;

exports[`Directive Definitions ValidateDirective 1`] = `
Object {
"defaults": Object {},
"definition": "
directive @validate(
type: ValidationType!
value: [String!]!
errorMessage: String
) on FIELD_DEFINITION

enum ValidationType {
gt
lt
gte
lte
minLength
maxLength
startsWith
endsWith
matches
}
",
"name": "validate",
}
`;

exports[`Directive Definitions VersionedDirectiveV1 1`] = `
Object {
"defaults": Object {
Expand Down Expand Up @@ -696,6 +722,7 @@ Array [
"refersTo",
"searchable",
"sql",
"validate",
]
`;

Expand Down Expand Up @@ -727,5 +754,6 @@ Array [
"refersTo",
"searchable",
"sql",
"validate",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export { PrimaryKeyDirective } from './primary-key';
export { RefersToDirective } from './refers-to';
export { SearchableDirective } from './searchable';
export { SqlDirective } from './sql';
export { ValidateDirective } from './validate';
29 changes: 29 additions & 0 deletions packages/amplify-graphql-directives/src/directives/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Directive } from './directive';

const name = 'validate';
const definition = /* GraphQL */ `
directive @${name}(
type: ValidationType!
value: [String!]!
errorMessage: String
) on FIELD_DEFINITION

enum ValidationType {
gt
lt
gte
lte
minLength
maxLength
startsWith
endsWith
matches
}
`;
const defaults = {};

export const ValidateDirective: Directive = {
name,
definition,
defaults,
};
2 changes: 2 additions & 0 deletions packages/amplify-graphql-directives/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
SearchableDirectiveV1,
SqlDirective,
VersionedDirectiveV1,
ValidateDirective,
} from './directives';

export const AppSyncDirectives: readonly Directive[] = [
Expand Down Expand Up @@ -69,6 +70,7 @@ export const V2Directives: readonly Directive[] = [
RefersToDirective,
SearchableDirective,
SqlDirective,
ValidateDirective,
];

/**
Expand Down
1 change: 1 addition & 0 deletions packages/amplify-graphql-transformer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@aws-amplify/graphql-sql-transformer": "0.4.11",
"@aws-amplify/graphql-transformer-core": "3.3.3",
"@aws-amplify/graphql-transformer-interfaces": "4.2.1",
"@aws-amplify/graphql-validate-transformer": "1.0.0",
"@aws-amplify/plugin-types": "^1.0.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
defaultPrintTransformerLog,
} from '../graphql-transformer';

const numOfTransformers = 18;
const numOfTransformers = 19;
describe('constructTransformerChain', () => {
it(`returns ${numOfTransformers} transformers when no custom transformers are provided`, () => {
expect(constructTransformerChain().length).toEqual(numOfTransformers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import { IFunction } from 'aws-cdk-lib/aws-lambda';
import { GenerationTransformer } from '@aws-amplify/graphql-generation-transformer';
import { ConversationTransformer } from '@aws-amplify/graphql-conversation-transformer';
import { BackendOutputEntry, BackendOutputStorageStrategy } from '@aws-amplify/plugin-types';
import { ValidateTransformer } from '@aws-amplify/graphql-validate-transformer';

/**
* Arguments passed into a TransformerFactory
* Used to determine how to create a new GraphQLTransform
Expand Down Expand Up @@ -95,6 +97,7 @@ export const constructTransformerChain = (options?: TransformerFactoryArgs): Tra
new SqlTransformer(),
new RefersToTransformer(),
new SearchableModelTransformer(),
new ValidateTransformer(),
...(options?.customTransformers ?? []),
];
};
Expand Down
19 changes: 19 additions & 0 deletions packages/amplify-graphql-validate-transformer/API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## API Report File for "@aws-amplify/graphql-validate-transformer"

> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).

```ts

import { TransformerContextProvider } from '@aws-amplify/graphql-transformer-interfaces';
import { TransformerPluginBase } from '@aws-amplify/graphql-transformer-core';

// @public (undocumented)
export class ValidateTransformer extends TransformerPluginBase {
constructor();
// (undocumented)
generateResolvers: (ctx: TransformerContextProvider) => void;
}

// (No @packageDocumentation comment for this package)

```
12 changes: 12 additions & 0 deletions packages/amplify-graphql-validate-transformer/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const baseConfig = require('../../jest.config.base.js'); // eslint-disable-line @typescript-eslint/no-var-requires

module.exports = {
...baseConfig,
coverageThreshold: {
global: {
branches: 90,
lines: 90,
functions: 90,
},
},
};
42 changes: 42 additions & 0 deletions packages/amplify-graphql-validate-transformer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@aws-amplify/graphql-validate-transformer",
"version": "1.0.0",
"description": "Amplify GraphQL @validate transformer",
"repository": {
"type": "git",
"url": "https://github.com/aws-amplify/amplify-category-api.git",
"directory": "packages/amplify-graphql-validate-transformer"
},
"author": "Amazon Web Services",
"license": "Apache-2.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"keywords": [
"graphql",
"cloudformation",
"aws",
"amplify",
"validate",
"transformer"
],
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"clean": "rimraf ./lib",
"test": "jest",
"extract-api": "ts-node ../../scripts/extract-api.ts"
},
"dependencies": {
"@aws-amplify/graphql-directives": "2.6.1",
"@aws-amplify/graphql-transformer-core": "3.3.3",
"@aws-amplify/graphql-transformer-interfaces": "4.2.1",
"graphql": "^15.5.0",
"graphql-transformer-common": "5.1.2"
},
"devDependencies": {
"@aws-amplify/graphql-transformer-test-utils": "1.0.10"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ValidateTransformer } from '..';

test('amplify-graphql-validate-transformer', () => {
const transformer = new ValidateTransformer();
expect(transformer).toBeDefined();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ValidateDirective } from '@aws-amplify/graphql-directives';
import { TransformerPluginBase } from '@aws-amplify/graphql-transformer-core';
import { TransformerContextProvider } from '@aws-amplify/graphql-transformer-interfaces';

export class ValidateTransformer extends TransformerPluginBase {
constructor() {
super('amplify-graphql-validate-transformer', ValidateDirective.definition);
}

generateResolvers = (ctx: TransformerContextProvider): void => {
console.log('generateResolvers', ctx);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ValidateTransformer } from './graphql-validate-transformer';
14 changes: 14 additions & 0 deletions packages/amplify-graphql-validate-transformer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
},
"references": [
{ "path": "../amplify-graphql-directives" },
{ "path": "../amplify-graphql-transformer-core" },
{ "path": "../amplify-graphql-transformer-interfaces" },
{ "path": "../graphql-mapping-template" },
{ "path": "../graphql-transformer-common" }
]
}
Loading