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

Gql preprocess #20

Merged
merged 16 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 .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 {
marcvberg marked this conversation as resolved.
Show resolved Hide resolved
private schema?: DocumentNode;
marcvberg marked this conversation as resolved.
Show resolved Hide resolved

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",
}
}
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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line is a dup of 41

"immer": "^9.0.12"
},
"jest": {
"transform": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IndexTransformer, 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 { BelongsToTransformer, HasManyTransformer, HasOneTransformer } from '..';

test('fails if used as a has one relation', () => {
Expand Down Expand Up @@ -778,3 +778,43 @@ test('has many with queries null generate correct filter input objects for enum
expect(statusField).toBeDefined();
expect(statusField.type.name.value).toMatch('ModelIssueStatusInput');
});

describe('Pre Processing Has Many 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;
};

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

test('Should generate connecting field when one is not provided', () => {
const schema = `
type Blog @model {
id: ID!
postsField: [Post] @hasMany
}

type Post @model {
id: ID!
}
`;

const updatedSchemaDoc = transformer.preProcessSchema(parse(schema));
expect(hasGeneratedField(updatedSchemaDoc, 'Post', 'blogPostsFieldId')).toBeTruthy();
});
});

Loading