Skip to content

Commit

Permalink
Feat: Single Source Read (aws-amplify#573)
Browse files Browse the repository at this point in the history
* chore: no-op change to trigger pipelines (aws-amplify#545)

* chore(release): Publish [ci skip]

 - @aws-amplify/amplify-category-api@3.1.1
 - amplify-category-api-dynamodb-simulator@2.4.1
 - amplify-category-api-e2e-core@4.0.1
 - amplify-category-api-e2e-tests@3.10.1
 - @aws-amplify/graphql-auth-transformer@0.11.0
 - @aws-amplify/graphql-default-value-transformer@0.5.27
 - @aws-amplify/graphql-function-transformer@0.7.21
 - @aws-amplify/graphql-http-transformer@0.8.21
 - @aws-amplify/graphql-index-transformer@0.12.0
 - @aws-amplify/graphql-maps-to-transformer@1.1.19
 - amplify-category-api-graphql-migration-tests@2.3.1
 - @aws-amplify/graphql-model-transformer@0.14.5
 - @aws-amplify/graphql-predictions-transformer@0.6.21
 - @aws-amplify/graphql-relational-transformer@0.10.0
 - @aws-amplify/graphql-schema-test-library@1.1.0
 - @aws-amplify/graphql-searchable-transformer@0.14.5
 - @aws-amplify/graphql-transformer-core@0.17.5
 - @aws-amplify/graphql-transformer-interfaces@1.14.4
 - @aws-amplify/graphql-transformer-migrator@1.4.0
 - amplify-category-api-migration-tests@5.0.1
 - amplify-category-api-util-mock@5.1.0
 - graphql-auth-transformer@7.2.38
 - graphql-connection-transformer@5.2.38
 - graphql-dynamodb-transformer@7.2.38
 - graphql-elasticsearch-transformer@5.2.38
 - graphql-function-transformer@3.3.29
 - graphql-http-transformer@5.2.38
 - graphql-key-transformer@3.2.38
 - graphql-predictions-transformer@3.2.38
 - graphql-transformer-core@7.6.1
 - amplify-category-api-graphql-transformers-e2e-tests@8.1.0
 - graphql-versioned-transformer@5.2.38

* Added Windows compatible translation of "hoist-cli" script (aws-amplify#543)

* fix(graphql): handle begin and end vtl keywords in index name (aws-amplify#510)

* fix(graphql): handle begin and end vtl keywords in index name

* updated comments

* chore: cleanup stale s3 buckets, and remove references to amplify-app which we no longer leverage in our test suite (aws-amplify#556)

* feature: add support for default generation of query and gsi names for @index directive behind a feature flag

* fix(amplify-graphql-auth-transformer, amplify-graphql-relational-transformer, amplify-graphql-transformer-core): support custom primary key with relational directives (aws-amplify#462)

* fix(amplify-graphql-auth-transformer, amplify-graphql-relational-transformer, amplify-graphql-transformer-core): support custom primary key with relational directives

* fix: fixed refactor incorrect hanlding for filter vs connection input names

* fix: merge build errors

* fix: merge caused unit test failures

* fix: merge caused e2e failures

* fix: merge caused e2e failures

* addressing PR feedback

* fix: corrected allowed fields for relational fields with custom PK and restricted auth

* fix: fixed imports and e2e feature flag initialization

* Feat: Single Source Read

This adds the capability for but does not enable single source reading on GraphQL schemas
This is intended for GraphQL Transformer V2 without support for V1
This also required pulling apart some of the logic in schema transformation so I can actually instantiate the transformer elsewhere

* Nit: early return statement for ease of reading

* test(graphql): fix failing datastore modelgen tests (aws-amplify#575)

* test(graphql): fix failing datastore modelgen tests

* moved the amplify-app to e2e tests package

* chore(release): Publish [ci skip]

 - @aws-amplify/amplify-category-api@3.1.2
 - amplify-category-api-e2e-core@4.0.2
 - amplify-category-api-e2e-tests@3.10.2
 - @aws-amplify/graphql-auth-transformer@0.11.1
 - @aws-amplify/graphql-default-value-transformer@0.5.28
 - @aws-amplify/graphql-function-transformer@0.7.22
 - @aws-amplify/graphql-http-transformer@0.8.22
 - @aws-amplify/graphql-index-transformer@0.12.1
 - @aws-amplify/graphql-maps-to-transformer@1.1.20
 - amplify-category-api-graphql-migration-tests@2.3.2
 - @aws-amplify/graphql-model-transformer@0.14.6
 - @aws-amplify/graphql-predictions-transformer@0.6.22
 - @aws-amplify/graphql-relational-transformer@0.10.1
 - @aws-amplify/graphql-schema-test-library@1.1.1
 - @aws-amplify/graphql-searchable-transformer@0.14.6
 - @aws-amplify/graphql-transformer-core@0.17.6
 - @aws-amplify/graphql-transformer-migrator@1.4.1
 - amplify-category-api-migration-tests@5.0.2
 - amplify-category-api-util-mock@5.1.1
 - graphql-auth-transformer@7.2.39
 - graphql-connection-transformer@5.2.39
 - graphql-dynamodb-transformer@7.2.39
 - graphql-elasticsearch-transformer@5.2.39
 - graphql-function-transformer@3.3.30
 - graphql-http-transformer@5.2.39
 - graphql-key-transformer@3.2.39
 - graphql-predictions-transformer@3.2.39
 - graphql-relational-schema-transformer@2.21.9
 - graphql-transformer-common@4.23.3
 - graphql-transformer-core@7.6.2
 - amplify-category-api-graphql-transformers-e2e-tests@8.1.1
 - graphql-versioned-transformer@5.2.39

* fix: timestamp field sort in search queries (aws-amplify#581)

* fix: timestamp field sort in search queries

* address CRs

* chore: change flag name for PK properties (aws-amplify#593)

* chore: add build target to refresh lockfile, and refreshing lockfile

* chore: improve error messaging if an invalid boolean sort key is provided to @index

* feat(graphql): add runtime filtering support for subscriptions (aws-amplify#551)

* feat(graphql): subscription runtime filtering

* added dynamic groups support

* update unit and e2e tests

* added more test cases for auth precedence and different operators

* fix enum list type on subscription filter

* update model transformer screenshots

* add test for enum field type

* updated error message on a test

* chore: remove unused dependencies based on depcheck report

* chore: merge release to main (aws-amplify#616)

* chore: no-op change to trigger pipelines (aws-amplify#545)

* chore(release): Publish [ci skip]

 - @aws-amplify/amplify-category-api@3.1.1
 - amplify-category-api-dynamodb-simulator@2.4.1
 - amplify-category-api-e2e-core@4.0.1
 - amplify-category-api-e2e-tests@3.10.1
 - @aws-amplify/graphql-auth-transformer@0.11.0
 - @aws-amplify/graphql-default-value-transformer@0.5.27
 - @aws-amplify/graphql-function-transformer@0.7.21
 - @aws-amplify/graphql-http-transformer@0.8.21
 - @aws-amplify/graphql-index-transformer@0.12.0
 - @aws-amplify/graphql-maps-to-transformer@1.1.19
 - amplify-category-api-graphql-migration-tests@2.3.1
 - @aws-amplify/graphql-model-transformer@0.14.5
 - @aws-amplify/graphql-predictions-transformer@0.6.21
 - @aws-amplify/graphql-relational-transformer@0.10.0
 - @aws-amplify/graphql-schema-test-library@1.1.0
 - @aws-amplify/graphql-searchable-transformer@0.14.5
 - @aws-amplify/graphql-transformer-core@0.17.5
 - @aws-amplify/graphql-transformer-interfaces@1.14.4
 - @aws-amplify/graphql-transformer-migrator@1.4.0
 - amplify-category-api-migration-tests@5.0.1
 - amplify-category-api-util-mock@5.1.0
 - graphql-auth-transformer@7.2.38
 - graphql-connection-transformer@5.2.38
 - graphql-dynamodb-transformer@7.2.38
 - graphql-elasticsearch-transformer@5.2.38
 - graphql-function-transformer@3.3.29
 - graphql-http-transformer@5.2.38
 - graphql-key-transformer@3.2.38
 - graphql-predictions-transformer@3.2.38
 - graphql-transformer-core@7.6.1
 - amplify-category-api-graphql-transformers-e2e-tests@8.1.0
 - graphql-versioned-transformer@5.2.38

* API Category Release (aws-amplify#587)

* chore: no-op change to trigger pipelines (aws-amplify#545)

* chore(release): Publish [ci skip]

 - @aws-amplify/amplify-category-api@3.1.1
 - amplify-category-api-dynamodb-simulator@2.4.1
 - amplify-category-api-e2e-core@4.0.1
 - amplify-category-api-e2e-tests@3.10.1
 - @aws-amplify/graphql-auth-transformer@0.11.0
 - @aws-amplify/graphql-default-value-transformer@0.5.27
 - @aws-amplify/graphql-function-transformer@0.7.21
 - @aws-amplify/graphql-http-transformer@0.8.21
 - @aws-amplify/graphql-index-transformer@0.12.0
 - @aws-amplify/graphql-maps-to-transformer@1.1.19
 - amplify-category-api-graphql-migration-tests@2.3.1
 - @aws-amplify/graphql-model-transformer@0.14.5
 - @aws-amplify/graphql-predictions-transformer@0.6.21
 - @aws-amplify/graphql-relational-transformer@0.10.0
 - @aws-amplify/graphql-schema-test-library@1.1.0
 - @aws-amplify/graphql-searchable-transformer@0.14.5
 - @aws-amplify/graphql-transformer-core@0.17.5
 - @aws-amplify/graphql-transformer-interfaces@1.14.4
 - @aws-amplify/graphql-transformer-migrator@1.4.0
 - amplify-category-api-migration-tests@5.0.1
 - amplify-category-api-util-mock@5.1.0
 - graphql-auth-transformer@7.2.38
 - graphql-connection-transformer@5.2.38
 - graphql-dynamodb-transformer@7.2.38
 - graphql-elasticsearch-transformer@5.2.38
 - graphql-function-transformer@3.3.29
 - graphql-http-transformer@5.2.38
 - graphql-key-transformer@3.2.38
 - graphql-predictions-transformer@3.2.38
 - graphql-transformer-core@7.6.1
 - amplify-category-api-graphql-transformers-e2e-tests@8.1.0
 - graphql-versioned-transformer@5.2.38

* Added Windows compatible translation of "hoist-cli" script (aws-amplify#543)

* fix(graphql): handle begin and end vtl keywords in index name (aws-amplify#510)

* fix(graphql): handle begin and end vtl keywords in index name

* updated comments

* chore: cleanup stale s3 buckets, and remove references to amplify-app which we no longer leverage in our test suite (aws-amplify#556)

* feature: add support for default generation of query and gsi names for @index directive behind a feature flag

* fix(amplify-graphql-auth-transformer, amplify-graphql-relational-transformer, amplify-graphql-transformer-core): support custom primary key with relational directives (aws-amplify#462)

* fix(amplify-graphql-auth-transformer, amplify-graphql-relational-transformer, amplify-graphql-transformer-core): support custom primary key with relational directives

* fix: fixed refactor incorrect hanlding for filter vs connection input names

* fix: merge build errors

* fix: merge caused unit test failures

* fix: merge caused e2e failures

* fix: merge caused e2e failures

* addressing PR feedback

* fix: corrected allowed fields for relational fields with custom PK and restricted auth

* fix: fixed imports and e2e feature flag initialization

* test(graphql): fix failing datastore modelgen tests (aws-amplify#575)

* test(graphql): fix failing datastore modelgen tests

* moved the amplify-app to e2e tests package

Co-authored-by: Al Harris <91494052+alharris-at@users.noreply.github.com>
Co-authored-by: amplify-data-ci <amplify-data-dev+github@amazon.com>
Co-authored-by: naedx <1711099+naedx@users.noreply.github.com>
Co-authored-by: Alexander Harris <alharris@amazon.com>
Co-authored-by: Pavel Lazar <85319655+lazpavel@users.noreply.github.com>

* chore(release): Publish [ci skip]

 - @aws-amplify/amplify-category-api@3.1.2
 - amplify-category-api-e2e-core@4.0.2
 - amplify-category-api-e2e-tests@3.10.2
 - @aws-amplify/graphql-auth-transformer@0.11.1
 - @aws-amplify/graphql-default-value-transformer@0.5.28
 - @aws-amplify/graphql-function-transformer@0.7.22
 - @aws-amplify/graphql-http-transformer@0.8.22
 - @aws-amplify/graphql-index-transformer@0.12.1
 - @aws-amplify/graphql-maps-to-transformer@1.1.20
 - amplify-category-api-graphql-migration-tests@2.3.2
 - @aws-amplify/graphql-model-transformer@0.14.6
 - @aws-amplify/graphql-predictions-transformer@0.6.22
 - @aws-amplify/graphql-relational-transformer@0.10.1
 - @aws-amplify/graphql-schema-test-library@1.1.1
 - @aws-amplify/graphql-searchable-transformer@0.14.6
 - @aws-amplify/graphql-transformer-core@0.17.6
 - @aws-amplify/graphql-transformer-migrator@1.4.1
 - amplify-category-api-migration-tests@5.0.2
 - amplify-category-api-util-mock@5.1.1
 - graphql-auth-transformer@7.2.39
 - graphql-connection-transformer@5.2.39
 - graphql-dynamodb-transformer@7.2.39
 - graphql-elasticsearch-transformer@5.2.39
 - graphql-function-transformer@3.3.30
 - graphql-http-transformer@5.2.39
 - graphql-key-transformer@3.2.39
 - graphql-predictions-transformer@3.2.39
 - graphql-relational-schema-transformer@2.21.9
 - graphql-transformer-common@4.23.3
 - graphql-transformer-core@7.6.2
 - amplify-category-api-graphql-transformers-e2e-tests@8.1.1
 - graphql-versioned-transformer@5.2.39

* fix conflict

* fix conflict

Co-authored-by: Al Harris <91494052+alharris-at@users.noreply.github.com>
Co-authored-by: amplify-data-ci <amplify-data-dev+github@amazon.com>
Co-authored-by: Christopher Sundersingh <83315412+sundersc@users.noreply.github.com>
Co-authored-by: naedx <1711099+naedx@users.noreply.github.com>
Co-authored-by: Alexander Harris <alharris@amazon.com>
Co-authored-by: Pavel Lazar <85319655+lazpavel@users.noreply.github.com>

* Update linting rules

* Added Windows compatible translation of "hoist-cli" script (aws-amplify#543)

* chore: cleanup stale s3 buckets, and remove references to amplify-app which we no longer leverage in our test suite (aws-amplify#556)

* Feat: Single Source Read

This adds the capability for but does not enable single source reading on GraphQL schemas
This is intended for GraphQL Transformer V2 without support for V1
This also required pulling apart some of the logic in schema transformation so I can actually instantiate the transformer elsewhere

* Nit: early return statement for ease of reading

* Consolidate if statements

* chore: merge release to main (aws-amplify#616)

* chore: no-op change to trigger pipelines (aws-amplify#545)

* chore(release): Publish [ci skip]

 - @aws-amplify/amplify-category-api@3.1.1
 - amplify-category-api-dynamodb-simulator@2.4.1
 - amplify-category-api-e2e-core@4.0.1
 - amplify-category-api-e2e-tests@3.10.1
 - @aws-amplify/graphql-auth-transformer@0.11.0
 - @aws-amplify/graphql-default-value-transformer@0.5.27
 - @aws-amplify/graphql-function-transformer@0.7.21
 - @aws-amplify/graphql-http-transformer@0.8.21
 - @aws-amplify/graphql-index-transformer@0.12.0
 - @aws-amplify/graphql-maps-to-transformer@1.1.19
 - amplify-category-api-graphql-migration-tests@2.3.1
 - @aws-amplify/graphql-model-transformer@0.14.5
 - @aws-amplify/graphql-predictions-transformer@0.6.21
 - @aws-amplify/graphql-relational-transformer@0.10.0
 - @aws-amplify/graphql-schema-test-library@1.1.0
 - @aws-amplify/graphql-searchable-transformer@0.14.5
 - @aws-amplify/graphql-transformer-core@0.17.5
 - @aws-amplify/graphql-transformer-interfaces@1.14.4
 - @aws-amplify/graphql-transformer-migrator@1.4.0
 - amplify-category-api-migration-tests@5.0.1
 - amplify-category-api-util-mock@5.1.0
 - graphql-auth-transformer@7.2.38
 - graphql-connection-transformer@5.2.38
 - graphql-dynamodb-transformer@7.2.38
 - graphql-elasticsearch-transformer@5.2.38
 - graphql-function-transformer@3.3.29
 - graphql-http-transformer@5.2.38
 - graphql-key-transformer@3.2.38
 - graphql-predictions-transformer@3.2.38
 - graphql-transformer-core@7.6.1
 - amplify-category-api-graphql-transformers-e2e-tests@8.1.0
 - graphql-versioned-transformer@5.2.38

* API Category Release (aws-amplify#587)

* chore: no-op change to trigger pipelines (aws-amplify#545)

* chore(release): Publish [ci skip]

 - @aws-amplify/amplify-category-api@3.1.1
 - amplify-category-api-dynamodb-simulator@2.4.1
 - amplify-category-api-e2e-core@4.0.1
 - amplify-category-api-e2e-tests@3.10.1
 - @aws-amplify/graphql-auth-transformer@0.11.0
 - @aws-amplify/graphql-default-value-transformer@0.5.27
 - @aws-amplify/graphql-function-transformer@0.7.21
 - @aws-amplify/graphql-http-transformer@0.8.21
 - @aws-amplify/graphql-index-transformer@0.12.0
 - @aws-amplify/graphql-maps-to-transformer@1.1.19
 - amplify-category-api-graphql-migration-tests@2.3.1
 - @aws-amplify/graphql-model-transformer@0.14.5
 - @aws-amplify/graphql-predictions-transformer@0.6.21
 - @aws-amplify/graphql-relational-transformer@0.10.0
 - @aws-amplify/graphql-schema-test-library@1.1.0
 - @aws-amplify/graphql-searchable-transformer@0.14.5
 - @aws-amplify/graphql-transformer-core@0.17.5
 - @aws-amplify/graphql-transformer-interfaces@1.14.4
 - @aws-amplify/graphql-transformer-migrator@1.4.0
 - amplify-category-api-migration-tests@5.0.1
 - amplify-category-api-util-mock@5.1.0
 - graphql-auth-transformer@7.2.38
 - graphql-connection-transformer@5.2.38
 - graphql-dynamodb-transformer@7.2.38
 - graphql-elasticsearch-transformer@5.2.38
 - graphql-function-transformer@3.3.29
 - graphql-http-transformer@5.2.38
 - graphql-key-transformer@3.2.38
 - graphql-predictions-transformer@3.2.38
 - graphql-transformer-core@7.6.1
 - amplify-category-api-graphql-transformers-e2e-tests@8.1.0
 - graphql-versioned-transformer@5.2.38

* Added Windows compatible translation of "hoist-cli" script (aws-amplify#543)

* fix(graphql): handle begin and end vtl keywords in index name (aws-amplify#510)

* fix(graphql): handle begin and end vtl keywords in index name

* updated comments

* chore: cleanup stale s3 buckets, and remove references to amplify-app which we no longer leverage in our test suite (aws-amplify#556)

* feature: add support for default generation of query and gsi names for @index directive behind a feature flag

* fix(amplify-graphql-auth-transformer, amplify-graphql-relational-transformer, amplify-graphql-transformer-core): support custom primary key with relational directives (aws-amplify#462)

* fix(amplify-graphql-auth-transformer, amplify-graphql-relational-transformer, amplify-graphql-transformer-core): support custom primary key with relational directives

* fix: fixed refactor incorrect hanlding for filter vs connection input names

* fix: merge build errors

* fix: merge caused unit test failures

* fix: merge caused e2e failures

* fix: merge caused e2e failures

* addressing PR feedback

* fix: corrected allowed fields for relational fields with custom PK and restricted auth

* fix: fixed imports and e2e feature flag initialization

* test(graphql): fix failing datastore modelgen tests (aws-amplify#575)

* test(graphql): fix failing datastore modelgen tests

* moved the amplify-app to e2e tests package

Co-authored-by: Al Harris <91494052+alharris-at@users.noreply.github.com>
Co-authored-by: amplify-data-ci <amplify-data-dev+github@amazon.com>
Co-authored-by: naedx <1711099+naedx@users.noreply.github.com>
Co-authored-by: Alexander Harris <alharris@amazon.com>
Co-authored-by: Pavel Lazar <85319655+lazpavel@users.noreply.github.com>

* chore(release): Publish [ci skip]

 - @aws-amplify/amplify-category-api@3.1.2
 - amplify-category-api-e2e-core@4.0.2
 - amplify-category-api-e2e-tests@3.10.2
 - @aws-amplify/graphql-auth-transformer@0.11.1
 - @aws-amplify/graphql-default-value-transformer@0.5.28
 - @aws-amplify/graphql-function-transformer@0.7.22
 - @aws-amplify/graphql-http-transformer@0.8.22
 - @aws-amplify/graphql-index-transformer@0.12.1
 - @aws-amplify/graphql-maps-to-transformer@1.1.20
 - amplify-category-api-graphql-migration-tests@2.3.2
 - @aws-amplify/graphql-model-transformer@0.14.6
 - @aws-amplify/graphql-predictions-transformer@0.6.22
 - @aws-amplify/graphql-relational-transformer@0.10.1
 - @aws-amplify/graphql-schema-test-library@1.1.1
 - @aws-amplify/graphql-searchable-transformer@0.14.6
 - @aws-amplify/graphql-transformer-core@0.17.6
 - @aws-amplify/graphql-transformer-migrator@1.4.1
 - amplify-category-api-migration-tests@5.0.2
 - amplify-category-api-util-mock@5.1.1
 - graphql-auth-transformer@7.2.39
 - graphql-connection-transformer@5.2.39
 - graphql-dynamodb-transformer@7.2.39
 - graphql-elasticsearch-transformer@5.2.39
 - graphql-function-transformer@3.3.30
 - graphql-http-transformer@5.2.39
 - graphql-key-transformer@3.2.39
 - graphql-predictions-transformer@3.2.39
 - graphql-relational-schema-transformer@2.21.9
 - graphql-transformer-common@4.23.3
 - graphql-transformer-core@7.6.2
 - amplify-category-api-graphql-transformers-e2e-tests@8.1.1
 - graphql-versioned-transformer@5.2.39

* fix conflict

* fix conflict

Co-authored-by: Al Harris <91494052+alharris-at@users.noreply.github.com>
Co-authored-by: amplify-data-ci <amplify-data-dev+github@amazon.com>
Co-authored-by: Christopher Sundersingh <83315412+sundersc@users.noreply.github.com>
Co-authored-by: naedx <1711099+naedx@users.noreply.github.com>
Co-authored-by: Alexander Harris <alharris@amazon.com>
Co-authored-by: Pavel Lazar <85319655+lazpavel@users.noreply.github.com>

* Added Windows compatible translation of "hoist-cli" script (aws-amplify#543)

* chore: cleanup stale s3 buckets, and remove references to amplify-app which we no longer leverage in our test suite (aws-amplify#556)

* Feat: Single Source Read

This adds the capability for but does not enable single source reading on GraphQL schemas
This is intended for GraphQL Transformer V2 without support for V1
This also required pulling apart some of the logic in schema transformation so I can actually instantiate the transformer elsewhere

* Nit: early return statement for ease of reading

* Consolidate if statements

* Nits: fixing some code quality requests

* Add explicit error for no schema found

Co-authored-by: Al Harris <91494052+alharris-at@users.noreply.github.com>
Co-authored-by: amplify-data-ci <amplify-data-dev+github@amazon.com>
Co-authored-by: naedx <1711099+naedx@users.noreply.github.com>
Co-authored-by: Christopher Sundersingh <83315412+sundersc@users.noreply.github.com>
Co-authored-by: Alexander Harris <alharris@amazon.com>
Co-authored-by: Pavel Lazar <85319655+lazpavel@users.noreply.github.com>
Co-authored-by: Zeyu Li <lizeyu@amazon.com>
  • Loading branch information
8 people committed Aug 16, 2022
1 parent 276331c commit d9388ab
Showing 8 changed files with 590 additions and 237 deletions.
56 changes: 56 additions & 0 deletions packages/amplify-category-api/src/category-utils/context-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
$TSAny,
$TSContext,
AmplifyCategories,
pathManager,
} from 'amplify-cli-core';
import path from 'path';
import { PROVIDER_NAME } from '../graphql-transformer/constants';

/**
* ContextUtil
* Some values are calculated on the basis of the context and options that come
* from the Amplify CLI, this class/singleton help calculate and cache those values
* for reference
*/
export class ContextUtil {
private resourceDir: string;

/**
* Get the resource directory as used by the API category for GraphQL
* @param context the context from the CLI
* @param options the options from the CLI
*/
getResourceDir = async (
context: $TSContext,
options: $TSAny,
): Promise<string> => {
if (this.resourceDir) {
return this.resourceDir;
}
let { resourceDir } = options;
const backEndDir = pathManager.getBackendDirPath();
const { resourcesToBeCreated, resourcesToBeUpdated } = await context.amplify.getResourceStatus(AmplifyCategories.API);
const resources = resourcesToBeCreated.concat(resourcesToBeUpdated);
if (!resourceDir) {
// There can only be one appsync resource
if (!resources.length) {
// No appsync resource to update/add
return undefined;
}
if (resources.length > 0) {
const resource = resources[0];
if (resource.providerPlugin !== PROVIDER_NAME) {
return undefined;
}
const { category } = resource;
const { resourceName } = resource;
resourceDir = path.normalize(path.join(backEndDir, category, resourceName));
}
}
this.resourceDir = resourceDir;
return resourceDir;
}
}

export const contextUtil = new ContextUtil();
91 changes: 91 additions & 0 deletions packages/amplify-category-api/src/category-utils/schema-reader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import * as fs from 'fs-extra';
import path from 'path';
import {
DocumentNode,
parse,
} from 'graphql';
import {
$TSAny,
$TSContext,
ApiCategoryFacade,
} from 'amplify-cli-core';
import { buildGraphQLTransformV2 } from '../graphql-transformer/transformer-factory';
import {
SCHEMA_DIR_NAME,
SCHEMA_FILENAME,
} from '../graphql-transformer/constants';
import { generateTransformerOptions } from '../graphql-transformer/transformer-options-v2';
import { contextUtil } from './context-util';

/**
* SchemaReader is a utility point to consolidate and abstract GraphQL Schema reading
* The readSchema method provides a flag to read the un-processed (original) schema
* if desired, but by default the intent of the SchemaReader is to use the preProcess
* utility of the V2 transformer
*/
export class SchemaReader {
private schemaPath: string;
private schemaDocument: DocumentNode;
private preProcessedSchemaDocument: DocumentNode;

getSchemaPath = async (
resourceDir: string,
): Promise<string> => {
if (this.schemaPath) {
return this.schemaPath;
}
const schemaFilePath = path.normalize(path.join(resourceDir, SCHEMA_FILENAME));
const schemaDirPath = path.normalize(path.join(resourceDir, SCHEMA_DIR_NAME));

if (fs.pathExistsSync(schemaFilePath)) {
this.schemaPath = schemaFilePath;
} else if (fs.pathExistsSync(schemaDirPath)) {
this.schemaPath = schemaDirPath;
} else {
throw new Error(`No schema found, your graphql schema should be in either ${schemaFilePath} or ${schemaDirPath}`);
}
return this.schemaPath;
};

invalidateCachedSchema = (): void => {
this.schemaPath = null;
this.schemaDocument = null;
this.preProcessedSchemaDocument = null;
};

readSchema = async (
context: $TSContext,
options: $TSAny,
usePreProcessing = true,
): Promise<DocumentNode> => {
const preProcessSchema = usePreProcessing && (await ApiCategoryFacade.getTransformerVersion(context) === 2);
if (!this.schemaDocument) {
const fileContentsList = new Array<Promise<Buffer>>();
const resourceDir = await contextUtil.getResourceDir(context, options);
const schemaPath = await this.getSchemaPath(resourceDir);

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');
this.schemaDocument = parse(fullSchema);
}

if (preProcessSchema && !this.preProcessedSchemaDocument) {
const transformerOptions = await generateTransformerOptions(context, options);
const transform = await buildGraphQLTransformV2(transformerOptions);
this.preProcessedSchemaDocument = transform.preProcessSchema(this.schemaDocument);
}

return preProcessSchema ? this.preProcessedSchemaDocument : this.schemaDocument;
};
}

export const schemaReader = new SchemaReader();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const DESTRUCTIVE_UPDATES_FLAG = 'allow-destructive-graphql-schema-updates';
export const PROVIDER_NAME = 'awscloudformation';
export const PARAMETERS_FILENAME = 'parameters.json';
export const ROOT_APPSYNC_S3_KEY = 'amplify-appsync-files';
export const SCHEMA_FILENAME = 'schema.graphql';
export const SCHEMA_DIR_NAME = 'schema';
Loading

0 comments on commit d9388ab

Please sign in to comment.