From 7452e451681c31ea478741fb363f85b97738b0be Mon Sep 17 00:00:00 2001 From: Dane Pilcher Date: Fri, 22 Sep 2023 16:06:37 -0600 Subject: [PATCH] feat: return GenerationResult from generateApiCode --- packages/model-generator/API.md | 8 +- .../src/appsync_graphql_generation_result.ts | 2 +- .../model-generator/src/generate_api_code.ts | 91 +++++++++---------- .../src/graphql_document_generator.test.ts | 2 +- .../src/graphql_models_generator.test.ts | 2 +- .../src/graphql_types_generator.test.ts | 2 +- .../model-generator/src/model_generator.ts | 1 - 7 files changed, 46 insertions(+), 62 deletions(-) diff --git a/packages/model-generator/API.md b/packages/model-generator/API.md index c2fd55f8a5..97087da629 100644 --- a/packages/model-generator/API.md +++ b/packages/model-generator/API.md @@ -21,18 +21,13 @@ export type DocumentGenerationParameters = { }; // @public -export const generateApiCode: (props: GenerateApiCodeProps) => Promise; +export const generateApiCode: (props: GenerateApiCodeProps) => Promise; // @public (undocumented) export type GenerateApiCodeProps = GenerateOptions & BackendIdentifier & { credentialProvider: AwsCredentialIdentityProvider; }; -// @public (undocumented) -export type GeneratedOutput = { - [filename: string]: string; -}; - // @public (undocumented) export type GenerateGraphqlCodegenOptions = { format: 'graphql-codegen'; @@ -68,7 +63,6 @@ export type GenerateOptions = GenerateGraphqlCodegenOptions | GenerateModelsOpti // @public (undocumented) export type GenerationResult = { writeToDirectory: (directoryPath: string) => Promise; - operations: Record; }; // @public (undocumented) diff --git a/packages/model-generator/src/appsync_graphql_generation_result.ts b/packages/model-generator/src/appsync_graphql_generation_result.ts index 291969c7d0..f9d6fe0f1d 100644 --- a/packages/model-generator/src/appsync_graphql_generation_result.ts +++ b/packages/model-generator/src/appsync_graphql_generation_result.ts @@ -12,7 +12,7 @@ export class AppsyncGraphqlGenerationResult implements GenerationResult { * @param operations A record of FileName to FileContent * in the format of Record */ - constructor(public operations: ClientOperations) {} + constructor(private operations: ClientOperations) {} private writeSchemaToFile = async ( basePath: string, filePath: string, diff --git a/packages/model-generator/src/generate_api_code.ts b/packages/model-generator/src/generate_api_code.ts index 6ec7a6384d..4ad44f68f4 100644 --- a/packages/model-generator/src/generate_api_code.ts +++ b/packages/model-generator/src/generate_api_code.ts @@ -2,12 +2,11 @@ import { BackendIdentifier } from '@aws-amplify/deployed-backend-client'; import { fromNodeProviderChain } from '@aws-sdk/credential-providers'; import { AwsCredentialIdentityProvider } from '@aws-sdk/types'; +import { GenerationResult } from './model_generator.js'; import { createGraphqlModelsGenerator } from './create_graphql_models_generator.js'; import { createGraphqlTypesGenerator } from './create_graphql_types_generator.js'; import { createGraphqlDocumentGenerator } from './create_graphql_document_generator.js'; -export type GeneratedOutput = { [filename: string]: string }; - export type GenerateModelsOptions = { format: 'modelgen'; modelTarget: 'java' | 'swift' | 'javascript' | 'typescript' | 'dart'; @@ -56,68 +55,60 @@ export type GenerateApiCodeProps = GenerateOptions & */ export const generateApiCode = async ( props: GenerateApiCodeProps -): Promise => { +): Promise => { const { credentialProvider = fromNodeProviderChain() } = props; switch (props.format) { case 'graphql-codegen': { - const documents = ( - await createGraphqlDocumentGenerator({ - backendIdentifier: props as BackendIdentifier, - credentialProvider, - }).generateModels({ - language: props.statementTarget, - maxDepth: props.maxDepth, - typenameIntrospection: props.typeNameIntrospection, - }) - ).operations; + const documents = await createGraphqlDocumentGenerator({ + backendIdentifier: props as BackendIdentifier, + credentialProvider, + }).generateModels({ + language: props.statementTarget, + maxDepth: props.maxDepth, + typenameIntrospection: props.typeNameIntrospection, + }); if (props.typeTarget) { - const types = ( - await createGraphqlTypesGenerator({ - backendIdentifier: props as BackendIdentifier, - credentialProvider, - }).generateTypes({ - target: props.typeTarget, - multipleSwiftFiles: props.multipleSwiftFiles, - }) - ).operations; + const types = await createGraphqlTypesGenerator({ + backendIdentifier: props as BackendIdentifier, + credentialProvider, + }).generateTypes({ + target: props.typeTarget, + multipleSwiftFiles: props.multipleSwiftFiles, + }); return { ...documents, ...types }; } return documents; } case 'modelgen': { - return ( - await createGraphqlModelsGenerator({ - backendIdentifier: props as BackendIdentifier, - credentialProvider, - }).generateModels({ - target: props.modelTarget, - generateIndexRules: props.generateIndexRules, - emitAuthProvider: props.emitAuthProvider, - useExperimentalPipelinedTransformer: - props.useExperimentalPipelinedTransformer, - transformerVersion: props.transformerVersion, - respectPrimaryKeyAttributesOnConnectionField: - props.respectPrimaryKeyAttributesOnConnectionField, - generateModelsForLazyLoadAndCustomSelectionSet: - props.generateModelsForLazyLoadAndCustomSelectionSet, - addTimestampFields: props.addTimestampFields, - handleListNullabilityTransparently: - props.handleListNullabilityTransparently, - }) - ).operations; + return createGraphqlModelsGenerator({ + backendIdentifier: props as BackendIdentifier, + credentialProvider, + }).generateModels({ + target: props.modelTarget, + generateIndexRules: props.generateIndexRules, + emitAuthProvider: props.emitAuthProvider, + useExperimentalPipelinedTransformer: + props.useExperimentalPipelinedTransformer, + transformerVersion: props.transformerVersion, + respectPrimaryKeyAttributesOnConnectionField: + props.respectPrimaryKeyAttributesOnConnectionField, + generateModelsForLazyLoadAndCustomSelectionSet: + props.generateModelsForLazyLoadAndCustomSelectionSet, + addTimestampFields: props.addTimestampFields, + handleListNullabilityTransparently: + props.handleListNullabilityTransparently, + }); } case 'introspection': { - return ( - await createGraphqlModelsGenerator({ - backendIdentifier: props as BackendIdentifier, - credentialProvider, - }).generateModels({ - target: 'introspection', - }) - ).operations; + return createGraphqlModelsGenerator({ + backendIdentifier: props as BackendIdentifier, + credentialProvider, + }).generateModels({ + target: 'introspection', + }); } default: throw new Error( diff --git a/packages/model-generator/src/graphql_document_generator.test.ts b/packages/model-generator/src/graphql_document_generator.test.ts index 1a78740bc7..7f74351f37 100644 --- a/packages/model-generator/src/graphql_document_generator.test.ts +++ b/packages/model-generator/src/graphql_document_generator.test.ts @@ -6,7 +6,7 @@ describe('client generator', () => { it('if `fetchSchema` returns null, it should throw an error', async () => { const generator = new AppSyncGraphqlDocumentGenerator( async () => null as unknown as string, - () => ({ writeToDirectory: () => Promise.resolve(), operations: {} }) + () => ({ writeToDirectory: () => Promise.resolve() }) ); await assert.rejects(() => generator.generateModels({ language: 'typescript' }) diff --git a/packages/model-generator/src/graphql_models_generator.test.ts b/packages/model-generator/src/graphql_models_generator.test.ts index d1c26e754c..cfd9fcc7ed 100644 --- a/packages/model-generator/src/graphql_models_generator.test.ts +++ b/packages/model-generator/src/graphql_models_generator.test.ts @@ -6,7 +6,7 @@ describe('models generator', () => { it('if `fetchSchema` returns null, it should throw an error', async () => { const generator = new StackMetadataGraphqlModelsGenerator( async () => null as unknown as string, - () => ({ writeToDirectory: () => Promise.resolve(), operations: {} }) + () => ({ writeToDirectory: () => Promise.resolve() }) ); await assert.rejects(() => generator.generateModels({ target: 'typescript' }) diff --git a/packages/model-generator/src/graphql_types_generator.test.ts b/packages/model-generator/src/graphql_types_generator.test.ts index c6f13a799b..8153f79473 100644 --- a/packages/model-generator/src/graphql_types_generator.test.ts +++ b/packages/model-generator/src/graphql_types_generator.test.ts @@ -6,7 +6,7 @@ describe('types generator', () => { it('if `fetchSchema` returns null, it should throw an error', async () => { const generator = new AppSyncGraphqlTypesGenerator( async () => null as unknown as string, - () => ({ writeToDirectory: () => Promise.resolve(), operations: {} }) + () => ({ writeToDirectory: () => Promise.resolve() }) ); await assert.rejects(() => generator.generateTypes({ target: 'typescript' }) diff --git a/packages/model-generator/src/model_generator.ts b/packages/model-generator/src/model_generator.ts index d5dfa84bc1..22c359df4d 100644 --- a/packages/model-generator/src/model_generator.ts +++ b/packages/model-generator/src/model_generator.ts @@ -12,7 +12,6 @@ export type DocumentGenerationParameters = { }; export type GenerationResult = { writeToDirectory: (directoryPath: string) => Promise; - operations: Record; }; export type GraphqlDocumentGenerator = { generateModels: (