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

generate client code command #265

Merged
merged 11 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
5 changes: 5 additions & 0 deletions .changeset/old-pumpkins-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-cli': minor
---

Add generate graphql-client-code command with mocked implementation
1 change: 1 addition & 0 deletions .eslint_dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default [
'codegen',
'cognito',
'ctor',
'datastore',
'debounce',
'declarator',
'deployer',
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"dependencies": {
"@aws-amplify/client-config": "^0.2.0-alpha.5",
"@aws-amplify/deployed-backend-client": "^0.1.0",
"@aws-amplify/model-generator": "^0.2.0-alpha.1",
"@aws-amplify/sandbox": "^0.1.1-alpha.5",
"@aws-sdk/credential-providers": "^3.360.0",
"@inquirer/prompts": "^3.0.0",
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/src/commands/generate/generate_command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Argv, CommandModule } from 'yargs';
import { GenerateConfigCommand } from './config/generate_config_command.js';
import { GenerateGraphqlClientCodeCommand } from './graphql-client-code/generate_graphql_client_code_command.js';

/**
* An entry point for generate command.
Expand All @@ -18,7 +19,10 @@ export class GenerateCommand implements CommandModule {
/**
* Creates top level entry point for generate command.
*/
constructor(private readonly generateConfigCommand: GenerateConfigCommand) {
constructor(
private readonly generateConfigCommand: GenerateConfigCommand,
private readonly generateGraphqlClientCodeCommand: GenerateGraphqlClientCodeCommand
) {
this.command = 'generate';
this.describe = 'Generates post deployment artifacts';
}
Expand All @@ -38,6 +42,9 @@ export class GenerateCommand implements CommandModule {
yargs
// Cast to erase options types used in internal sub command implementation. Otherwise, compiler fails here.
.command(this.generateConfigCommand as unknown as CommandModule)
.command(
this.generateGraphqlClientCodeCommand as unknown as CommandModule
)
.demandCommand()
.strictCommands()
.recommendCommands()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ describe('top level generate command', () => {
const parser = yargs().command(generateCommand);
const commandRunner = new TestCommandRunner(parser);

it('includes generate config in help output', async () => {
it('includes generate subcommands in help output', async () => {
const output = await commandRunner.runCommand('generate --help');
assert.match(output, /Commands:/);
assert.match(output, /generate config {2}Generates client config/);
assert.match(output, /generate config\W*Generates client config/);
assert.match(
output,
/generate graphql-client-code\W*Generates graphql API code/
);
});

it('fails if subcommand is not provided', async () => {
Expand Down
14 changes: 13 additions & 1 deletion packages/cli/src/commands/generate/generate_command_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { GenerateConfigCommand } from './config/generate_config_command.js';
import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
import { ClientConfigGeneratorAdapter } from './config/client_config_generator_adapter.js';
import { CwdPackageJsonLoader } from '../../cwd_package_json_loader.js';
import { GenerateGraphqlClientCodeCommand } from './graphql-client-code/generate_graphql_client_code_command.js';
import { LocalAppNameResolver } from '../../backend-identifier/local_app_name_resolver.js';
import { BackendIdentifierResolver } from '../../backend-identifier/backend_identifier_resolver.js';
import { GenerateApiCodeAdapter } from './graphql-client-code/generate_api_code_adapter.js';

/**
* Creates wired generate command.
Expand All @@ -28,5 +30,15 @@ export const createGenerateCommand = (): CommandModule => {
backendIdentifierResolver
);

return new GenerateCommand(generateConfigCommand);
const generateApiCodeAdapter = new GenerateApiCodeAdapter(credentialProvider);

const generateGraphqlClientCodeCommand = new GenerateGraphqlClientCodeCommand(
generateApiCodeAdapter,
backendIdentifierResolver
);

return new GenerateCommand(
generateConfigCommand,
generateGraphqlClientCodeCommand
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { BackendIdentifier } from '@aws-amplify/deployed-backend-client';
import {
GenerateOptions,
GenerationResult,
generateApiCode,
} from '@aws-amplify/model-generator';
import { AwsCredentialIdentityProvider } from '@aws-sdk/types';

// For some reason using `omit` is causing type errors, so reconstructing without the credentialProvider.
export type InvokeGenerateApiCodeProps = GenerateOptions & BackendIdentifier;

/**
* Class to wrap static generateApiCode method to facilitate testing.
*/
export class GenerateApiCodeAdapter {
/**
* Creates graphql api code adapter.
*/
constructor(
private readonly credentialProvider: AwsCredentialIdentityProvider
) {}

/**
* Invoke the generateApiCode method, using the constructor injected credentialProvider, and remaining props.
*/
invokeGenerateApiCode = (
props: InvokeGenerateApiCodeProps
): Promise<GenerationResult> =>
generateApiCode({
...props,
credentialProvider: this.credentialProvider,
});
}
Loading