Skip to content

Commit

Permalink
feat: return GenerationResult from generateApiCode
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilch committed Sep 22, 2023
1 parent 144d40d commit 7452e45
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 62 deletions.
8 changes: 1 addition & 7 deletions packages/model-generator/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@ export type DocumentGenerationParameters = {
};

// @public
export const generateApiCode: (props: GenerateApiCodeProps) => Promise<GeneratedOutput>;
export const generateApiCode: (props: GenerateApiCodeProps) => Promise<GenerationResult>;

// @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';
Expand Down Expand Up @@ -68,7 +63,6 @@ export type GenerateOptions = GenerateGraphqlCodegenOptions | GenerateModelsOpti
// @public (undocumented)
export type GenerationResult = {
writeToDirectory: (directoryPath: string) => Promise<void>;
operations: Record<string, string>;
};

// @public (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class AppsyncGraphqlGenerationResult implements GenerationResult {
* @param operations A record of FileName to FileContent
* in the format of Record<string,string>
*/
constructor(public operations: ClientOperations) {}
constructor(private operations: ClientOperations) {}
private writeSchemaToFile = async (
basePath: string,
filePath: string,
Expand Down
91 changes: 41 additions & 50 deletions packages/model-generator/src/generate_api_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -56,68 +55,60 @@ export type GenerateApiCodeProps = GenerateOptions &
*/
export const generateApiCode = async (
props: GenerateApiCodeProps
): Promise<GeneratedOutput> => {
): Promise<GenerationResult> => {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down
1 change: 0 additions & 1 deletion packages/model-generator/src/model_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export type DocumentGenerationParameters = {
};
export type GenerationResult = {
writeToDirectory: (directoryPath: string) => Promise<void>;
operations: Record<string, string>;
};
export type GraphqlDocumentGenerator = {
generateModels: (
Expand Down

0 comments on commit 7452e45

Please sign in to comment.