Skip to content

Commit

Permalink
WIP: Adding preProcess support to V2 transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvberg committed May 6, 2022
1 parent 12e0be7 commit 8f9fc88
Show file tree
Hide file tree
Showing 19 changed files with 475 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ packages/amplify-category-geo/lib/
packages/amplify-e2e-tests/amplify-e2e-reports
packages/amplify-console-integration-tests/lib
packages/amplify-console-integration-tests/console-integration-reports
packages/amplify-data-core/lib
packages/amplify-data-interfaces/lib
packages/amplify-migration-tests/amplify-migration-reports
packages/amplify-migration-tests/lib
packages/amplify-headless-interface/lib
Expand Down
17 changes: 17 additions & 0 deletions packages/amplify-data-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# 1.1.0 (2020-12-07)


### Bug Fixes

* fix winston error with 0 transports ([#5631](https://github.com/aws-amplify/amplify-cli/issues/5631)) ([4326ec6](https://github.com/aws-amplify/amplify-cli/commit/4326ec6cf2a62580cd2646241463d20d7b7fb062))
* fixed core references ([#6069](https://github.com/aws-amplify/amplify-cli/issues/6069)) ([32446ac](https://github.com/aws-amplify/amplify-cli/commit/32446ac77a5064bee928544861b8a70fba556d51))


### Features

* Cloudformation logging ([#5195](https://github.com/aws-amplify/amplify-cli/issues/5195)) ([19b2165](https://github.com/aws-amplify/amplify-cli/commit/19b21651375848c0858328952852201da47b17bb))
61 changes: 61 additions & 0 deletions packages/amplify-data-core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "amplify-data-core",
"version": "1.1.0",
"description": "Amplify CLI Data Core Library",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"test": "jest",
"build": "tsc",
"watch": "tsc -w",
"clean": "rimraf lib tsconfig.tsbuildinfo"
},
"repository": {
"type": "git",
"url": "git+https://github.com/aws-amplify/amplify-cli.git"
},
"author": "",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/aws-amplify/amplify-cli/issues"
},
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/src/__tests__/.*|(\\.|/)test)\\.tsx?$",
"coveragePathIgnorePatterns": [
"/node_modules/",
"/templates/"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"collectCoverage": true
},
"homepage": "https://github.com/aws-amplify/amplify-cli#readme",
"dependencies": {
"amplify-cli-core": "2.5.0",
"amplify-data-interfaces": "1.0.0",
"@aws-amplify/graphql-auth-transformer": "0.7.10",
"@aws-amplify/graphql-default-value-transformer": "0.5.18",
"@aws-amplify/graphql-function-transformer": "0.7.12",
"@aws-amplify/graphql-http-transformer": "0.8.12",
"@aws-amplify/graphql-index-transformer": "0.11.3",
"@aws-amplify/graphql-maps-to-transformer": "1.1.10",
"@aws-amplify/graphql-model-transformer": "0.13.3",
"@aws-amplify/graphql-predictions-transformer": "0.6.12",
"@aws-amplify/graphql-relational-transformer": "0.7.10",
"@aws-amplify/graphql-searchable-transformer": "0.13.5",
"@aws-amplify/graphql-transformer-core": "0.16.3",
"@aws-amplify/graphql-transformer-interfaces": "1.13.0",
"immer": "^9.0.12",
"fs-extra": "^10.0.1",
"graphql": "^16.3.0"
}
}
40 changes: 40 additions & 0 deletions packages/amplify-data-core/src/amplify-data-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as fs from 'fs-extra';
import { DocumentNode, parse } from 'graphql';
import path from 'path';

import { AmplifyDataApiProvider } from '../../amplify-data-interfaces';
import { GraphQLTransform } from '@aws-amplify/graphql-transformer-core';

export class AmplifyDataApi implements AmplifyDataApiProvider {
private schema?: DocumentNode;

public constructor() {
this.schema = undefined;
}

public async readSchema(schemaPath: string): Promise<DocumentNode> {
if (this.schema) {
return this.schema;
}

const fileContentsList = new Array<Promise<Buffer>>();

const stats = fs.statSync(schemaPath);
if (stats.isDirectory()) {
fs.readdirSync(schemaPath).forEach(fileName => {
fileContentsList.push(fs.readFile(path.join(schemaPath, fileName)));
});
} else {
fileContentsList.push(fs.readFile(schemaPath));
}

const bufferList = await Promise.all(fileContentsList);
const fullSchema = bufferList.map(buff => buff.toString()).join('\n');
const parsedSchema = parse(fullSchema);/*
const transformer = {} as GraphQLTransform; // TODO: implement getTransformer();
const processedSchema = transformer.preProcess(parsedSchema);
this.schema = processedSchema;
return processedSchema;*/
return parsedSchema;
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {
BelongsToTransformer,
HasManyTransformer,
HasOneTransformer,
ManyToManyTransformer,
} from '@aws-amplify/graphql-relational-transformer';
import {
getAppSyncServiceExtraDirectives,
GraphQLTransform,
} from '@aws-amplify/graphql-transformer-core';
10 changes: 10 additions & 0 deletions packages/amplify-data-core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./lib",
"rootDir": "./src",
},
"references": [
{ "path": "../amplify-data-interfaces" },
]
}
17 changes: 17 additions & 0 deletions packages/amplify-data-interfaces/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# 1.1.0 (2020-12-07)


### Bug Fixes

* fix winston error with 0 transports ([#5631](https://github.com/aws-amplify/amplify-cli/issues/5631)) ([4326ec6](https://github.com/aws-amplify/amplify-cli/commit/4326ec6cf2a62580cd2646241463d20d7b7fb062))
* fixed core references ([#6069](https://github.com/aws-amplify/amplify-cli/issues/6069)) ([32446ac](https://github.com/aws-amplify/amplify-cli/commit/32446ac77a5064bee928544861b8a70fba556d51))


### Features

* Cloudformation logging ([#5195](https://github.com/aws-amplify/amplify-cli/issues/5195)) ([19b2165](https://github.com/aws-amplify/amplify-cli/commit/19b21651375848c0858328952852201da47b17bb))
58 changes: 58 additions & 0 deletions packages/amplify-data-interfaces/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "amplify-data-interfaces",
"version": "1.0.0",
"description": "Amplify CLI Data Interfaces",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"test": "jest",
"build": "tsc",
"watch": "tsc -w",
"clean": "rimraf lib tsconfig.tsbuildinfo"
},
"repository": {
"type": "git",
"url": "git+https://github.com/aws-amplify/amplify-cli.git"
},
"author": "",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/aws-amplify/amplify-cli/issues"
},
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/src/__tests__/.*|(\\.|/)test)\\.tsx?$",
"coveragePathIgnorePatterns": [
"/node_modules/",
"/templates/"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"collectCoverage": true
},
"homepage": "https://github.com/aws-amplify/amplify-cli#readme",
"dependencies": {
"amplify-cli-core": "2.5.0",
"@aws-amplify/graphql-auth-transformer": "0.7.10",
"@aws-amplify/graphql-default-value-transformer": "0.5.18",
"@aws-amplify/graphql-function-transformer": "0.7.12",
"@aws-amplify/graphql-http-transformer": "0.8.12",
"@aws-amplify/graphql-index-transformer": "0.11.3",
"@aws-amplify/graphql-maps-to-transformer": "1.1.10",
"@aws-amplify/graphql-model-transformer": "0.13.3",
"@aws-amplify/graphql-predictions-transformer": "0.6.12",
"@aws-amplify/graphql-relational-transformer": "0.7.10",
"@aws-amplify/graphql-searchable-transformer": "0.13.5",
"@aws-amplify/graphql-transformer-core": "0.16.3",
"@aws-amplify/graphql-transformer-interfaces": "1.13.0",
"graphql": "^16.3.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { DocumentNode } from 'graphql';

/**
* The AmplifyDataApiProvider serves as a definition for the API used for external interactions with the data layer
*/
export interface AmplifyDataApiProvider {
readSchema: (path: string) => Promise<DocumentNode>;
// getRemovedTables: (referencedTables: string[]) => string[];
}
1 change: 1 addition & 0 deletions packages/amplify-data-interfaces/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { AmplifyDataApiProvider } from './amplify-data-api-provider';
7 changes: 7 additions & 0 deletions packages/amplify-data-interfaces/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./lib",
"rootDir": "./src",
}
}
3 changes: 2 additions & 1 deletion packages/amplify-graphql-relational-transformer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"@aws-cdk/core": "~1.124.0",
"graphql": "^14.5.8",
"graphql-mapping-template": "4.20.3",
"graphql-transformer-common": "4.23.0"
"graphql-transformer-common": "4.23.0",
"immer": "^9.0.12"
},
"jest": {
"transform": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrimaryKeyTransformer } from '@aws-amplify/graphql-index-transformer';
import { ModelTransformer } from '@aws-amplify/graphql-model-transformer';
import { ConflictHandlerType, GraphQLTransform, validateModelSchema } from '@aws-amplify/graphql-transformer-core';
import { Kind, parse } from 'graphql';
import { DocumentNode, Kind, parse } from 'graphql';
import { HasManyTransformer, HasOneTransformer } from '..';

test('fails if @hasOne was used on an object that is not a model type', () => {
Expand Down Expand Up @@ -466,3 +466,123 @@ test('recursive @hasOne relationships are supported if DataStore is enabled', ()
const schema = parse(out.schema);
validateModelSchema(schema);
});

describe('Pre Processing Tests', () => {
let transformer: GraphQLTransform;
const hasGeneratedField = (doc: DocumentNode, objectType: string, fieldName: string): boolean => {
let hasField = false;
doc?.definitions?.forEach(def => {
if ((def.kind === 'ObjectTypeDefinition' || def.kind === 'ObjectTypeExtension') && def.name.value === objectType) {
def?.fields?.forEach(field => {
if (field.name.value === fieldName) {
hasField = true;
}
});
}
});
return hasField;
};

const hasGeneratedFieldArgument = (doc: DocumentNode, objectType: string, fieldName: string, generatedFieldName: string): boolean => {
let hasFieldArgument = false;
doc?.definitions?.forEach(def => {
if ((def.kind === 'ObjectTypeDefinition' || def.kind === 'ObjectTypeExtension') && def.name.value === objectType) {
def?.fields?.forEach(field => {
if (field.name.value === fieldName) {
field?.directives?.forEach(dir => {
if (dir.name.value === 'hasOne') {
dir?.arguments?.forEach(arg => {
if (arg.name.value === 'fields') {
if (arg.value.kind === 'ListValue' && arg.value.values[0].kind === 'StringValue' && arg.value.values[0].value === generatedFieldName) {
hasFieldArgument = true;
}
else if (arg.value.kind === 'StringValue' && arg.value.value === generatedFieldName) {
hasFieldArgument = true;
}
}
});
}
});
}
});
}
});
return hasFieldArgument;
};

beforeEach(() => {
transformer = new GraphQLTransform({
transformers: [new ModelTransformer(), new HasOneTransformer()],
});
});

test('Should generate connecting field when one is not provided', () => {
const schema = `
type Blog @model {
id: ID!
blogName: BlogName @hasOne
}
type BlogName @model {
id: ID!
}
`;

const updatedSchemaDoc = transformer.preProcessSchema(parse(schema));
expect(hasGeneratedField(updatedSchemaDoc, 'Blog', 'blogBlogNameId')).toBeTruthy();
expect(hasGeneratedFieldArgument(updatedSchemaDoc, 'Blog', 'blogName', 'blogBlogNameId')).toBeTruthy();
});

test('Should generate connecting field when empty fields are provided', () => {
const schema = `
type Blog @model {
id: ID!
blogName: BlogName @hasOne(fields: [])
}
type BlogName @model {
id: ID!
}
`;

const updatedSchemaDoc = transformer.preProcessSchema(parse(schema));
expect(hasGeneratedField(updatedSchemaDoc, 'Blog', 'blogBlogNameId')).toBeTruthy();
expect(hasGeneratedFieldArgument(updatedSchemaDoc, 'Blog', 'blogName', 'blogBlogNameId')).toBeTruthy();
});

test('Should not generate connecting field when one is provided', () => {
const schema = `
type Blog @model {
id: ID!
connectionField: ID
blogName: BlogName @hasOne(fields: "connectionField")
}
type BlogName @model {
id: ID!
}
`;

const updatedSchemaDoc = transformer.preProcessSchema(parse(schema));
expect(hasGeneratedField(updatedSchemaDoc, 'Blog', 'blogBlogNameId')).toBeFalsy();
expect(hasGeneratedFieldArgument(updatedSchemaDoc, 'Blog', 'blogName', 'blogBlogNameId')).toBeFalsy();
});

test('Should not generate connecting field when a list one is provided', () => {
const schema = `
type Blog @model {
id: ID!
connectionField: ID
blogName: BlogName @hasOne(fields: ["connectionField"])
}
type BlogName @model {
id: ID!
}
`;

const updatedSchemaDoc = transformer.preProcessSchema(parse(schema));
expect(hasGeneratedField(updatedSchemaDoc, 'Blog', 'blogBlogNameId')).toBeFalsy();
expect(hasGeneratedFieldArgument(updatedSchemaDoc, 'Blog', 'blogName', 'blogBlogNameId')).toBeFalsy();
});
});
Loading

0 comments on commit 8f9fc88

Please sign in to comment.