-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
353 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@aws-amplify/model-generator': minor | ||
--- | ||
|
||
Add wrapper to around types, documents, and model generation (`generateAPICode`). | ||
|
||
Change `createGraphqlDocumentGenerator` and `createGraphqlTypesGenerator` to use backendIdentifier and credentialProvider. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 16 additions & 2 deletions
18
packages/model-generator/src/create_graphql_document_generator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
import assert from 'node:assert'; | ||
import { describe, it } from 'node:test'; | ||
import { AwsCredentialIdentityProvider } from '@aws-sdk/types'; | ||
import { BackendIdentifier } from '@aws-amplify/deployed-backend-client'; | ||
import { createGraphqlDocumentGenerator } from './create_graphql_document_generator.js'; | ||
|
||
describe('model generator factory', () => { | ||
it('throws an error if a null apiId is passed in', async () => { | ||
it('throws an error if a null backendIdentifier is passed in', async () => { | ||
assert.throws(() => | ||
createGraphqlDocumentGenerator({ apiId: null as unknown as string }) | ||
createGraphqlDocumentGenerator({ | ||
backendIdentifier: null as unknown as BackendIdentifier, | ||
credentialProvider: null as unknown as AwsCredentialIdentityProvider, | ||
}) | ||
); | ||
}); | ||
|
||
it('throws an error if a null backendIdentifier is passed in', async () => { | ||
assert.throws(() => | ||
createGraphqlDocumentGenerator({ | ||
backendIdentifier: { stackName: 'foo' }, | ||
credentialProvider: null as unknown as AwsCredentialIdentityProvider, | ||
}) | ||
); | ||
}); | ||
}); |
38 changes: 32 additions & 6 deletions
38
packages/model-generator/src/create_graphql_document_generator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,51 @@ | ||
import { AppSyncClient } from '@aws-sdk/client-appsync'; | ||
import { | ||
BackendIdentifier, | ||
BackendOutputClient, | ||
} from '@aws-amplify/deployed-backend-client'; | ||
import { graphqlOutputKey } from '@aws-amplify/backend-output-schemas'; | ||
import { AwsCredentialIdentityProvider } from '@aws-sdk/types'; | ||
import { AppsyncGraphqlGenerationResult } from './appsync_graphql_generation_result.js'; | ||
import { AppSyncIntrospectionSchemaFetcher } from './appsync_schema_fetcher.js'; | ||
import { AppSyncGraphqlDocumentGenerator } from './graphql_document_generator.js'; | ||
import { GraphqlDocumentGenerator } from './model_generator.js'; | ||
|
||
export type GraphqlDocumentGeneratorFactoryParams = { | ||
apiId: string; | ||
backendIdentifier: BackendIdentifier; | ||
credentialProvider: AwsCredentialIdentityProvider; | ||
}; | ||
|
||
/** | ||
* Factory function to compose a model generator | ||
*/ | ||
export const createGraphqlDocumentGenerator = ({ | ||
apiId, | ||
backendIdentifier, | ||
credentialProvider, | ||
}: GraphqlDocumentGeneratorFactoryParams): GraphqlDocumentGenerator => { | ||
if (!apiId) { | ||
throw new Error('`apiId` must be defined'); | ||
if (!backendIdentifier) { | ||
throw new Error('`backendIdentifier` must be defined'); | ||
} | ||
if (!credentialProvider) { | ||
throw new Error('`credentialProvider` must be defined'); | ||
} | ||
|
||
const fetchSchema = async () => { | ||
const backendOutputClient = new BackendOutputClient( | ||
credentialProvider, | ||
backendIdentifier | ||
); | ||
const output = await backendOutputClient.getOutput(); | ||
const apiId = output[graphqlOutputKey]?.payload.awsAppsyncApiId; | ||
if (!apiId) { | ||
throw new Error(`Unable to determine AppSync API ID.`); | ||
} | ||
|
||
return new AppSyncIntrospectionSchemaFetcher(new AppSyncClient()).fetch( | ||
apiId | ||
); | ||
}; | ||
return new AppSyncGraphqlDocumentGenerator( | ||
() => | ||
new AppSyncIntrospectionSchemaFetcher(new AppSyncClient()).fetch(apiId), | ||
fetchSchema, | ||
(fileMap) => new AppsyncGraphqlGenerationResult(fileMap) | ||
); | ||
}; |
18 changes: 16 additions & 2 deletions
18
packages/model-generator/src/create_graphql_types_generator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
import assert from 'node:assert'; | ||
import { describe, it } from 'node:test'; | ||
import { AwsCredentialIdentityProvider } from '@aws-sdk/types'; | ||
import { BackendIdentifier } from '@aws-amplify/deployed-backend-client'; | ||
import { createGraphqlTypesGenerator } from './create_graphql_types_generator.js'; | ||
|
||
describe('types generator factory', () => { | ||
it('throws an error if a null apiId is passed in', async () => { | ||
it('throws an error if a null backendIdentifier is passed in', async () => { | ||
assert.throws(() => | ||
createGraphqlTypesGenerator({ apiId: null as unknown as string }) | ||
createGraphqlTypesGenerator({ | ||
backendIdentifier: null as unknown as BackendIdentifier, | ||
credentialProvider: null as unknown as AwsCredentialIdentityProvider, | ||
}) | ||
); | ||
}); | ||
|
||
it('throws an error if a null backendIdentifier is passed in', async () => { | ||
assert.throws(() => | ||
createGraphqlTypesGenerator({ | ||
backendIdentifier: { stackName: 'foo' }, | ||
credentialProvider: null as unknown as AwsCredentialIdentityProvider, | ||
}) | ||
); | ||
}); | ||
}); |
38 changes: 32 additions & 6 deletions
38
packages/model-generator/src/create_graphql_types_generator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,51 @@ | ||
import { AppSyncClient } from '@aws-sdk/client-appsync'; | ||
import { | ||
BackendIdentifier, | ||
BackendOutputClient, | ||
} from '@aws-amplify/deployed-backend-client'; | ||
import { graphqlOutputKey } from '@aws-amplify/backend-output-schemas'; | ||
import { AwsCredentialIdentityProvider } from '@aws-sdk/types'; | ||
import { AppsyncGraphqlGenerationResult } from './appsync_graphql_generation_result.js'; | ||
import { AppSyncIntrospectionSchemaFetcher } from './appsync_schema_fetcher.js'; | ||
import { AppSyncGraphqlTypesGenerator } from './graphql_types_generator.js'; | ||
import { GraphqlTypesGenerator } from './model_generator.js'; | ||
|
||
export type GraphqlTypesGeneratorFactoryParams = { | ||
apiId: string; | ||
backendIdentifier: BackendIdentifier; | ||
credentialProvider: AwsCredentialIdentityProvider; | ||
}; | ||
|
||
/** | ||
* Factory function to compose a model generator | ||
*/ | ||
export const createGraphqlTypesGenerator = ({ | ||
apiId, | ||
backendIdentifier, | ||
credentialProvider, | ||
}: GraphqlTypesGeneratorFactoryParams): GraphqlTypesGenerator => { | ||
if (!apiId) { | ||
throw new Error('`apiId` must be defined'); | ||
if (!backendIdentifier) { | ||
throw new Error('`backendIdentifier` must be defined'); | ||
} | ||
if (!credentialProvider) { | ||
throw new Error('`credentialProvider` must be defined'); | ||
} | ||
|
||
const fetchSchema = async () => { | ||
const backendOutputClient = new BackendOutputClient( | ||
credentialProvider, | ||
backendIdentifier | ||
); | ||
const output = await backendOutputClient.getOutput(); | ||
const apiId = output[graphqlOutputKey]?.payload.awsAppsyncApiId; | ||
if (!apiId) { | ||
throw new Error(`Unable to determine AppSync API ID.`); | ||
} | ||
|
||
return new AppSyncIntrospectionSchemaFetcher(new AppSyncClient()).fetch( | ||
apiId | ||
); | ||
}; | ||
return new AppSyncGraphqlTypesGenerator( | ||
() => | ||
new AppSyncIntrospectionSchemaFetcher(new AppSyncClient()).fetch(apiId), | ||
fetchSchema, | ||
(fileMap) => new AppsyncGraphqlGenerationResult(fileMap) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import assert from 'assert'; | ||
import { describe, it } from 'node:test'; | ||
import { GenerateApiCodeProps, generateApiCode } from './generate_api_code.js'; | ||
|
||
describe('generateAPICode', () => { | ||
describe('graphql-codegen', () => {}); | ||
describe('modelgen', () => {}); | ||
describe('introspection', () => {}); | ||
|
||
it('throws error on unknown format', async () => { | ||
const props = { | ||
format: 'unsupported', | ||
stackName: 'stack_name', | ||
} as unknown as GenerateApiCodeProps; | ||
await assert.rejects(() => generateApiCode(props)); | ||
}); | ||
}); |
Oops, something went wrong.