-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add mocked generate graphql-client-code to cli * Update packages/cli/src/commands/generate/generate_command_factory.test.ts fix: update test name Co-authored-by: Kamil Sobol <[email protected]> * chore: add missing descriptions for targets, use new shared backendIdentifierResolver * chore: remove adapter, we may need to reintroduce in the future, but simplifying until we have an impl in place * chore: add feature flags * chore: flip to use real generator, not mocked api --------- Co-authored-by: Kamil Sobol <[email protected]>
- Loading branch information
1 parent
2fd970e
commit 5523bc9
Showing
12 changed files
with
712 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,5 @@ | ||
--- | ||
'@aws-amplify/backend-cli': minor | ||
--- | ||
|
||
Add generate graphql-client-code command with mocked implementation |
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ export default [ | |
'codegen', | ||
'cognito', | ||
'ctor', | ||
'datastore', | ||
'debounce', | ||
'declarator', | ||
'deployer', | ||
|
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
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
33 changes: 33 additions & 0 deletions
33
packages/cli/src/commands/generate/graphql-client-code/generate_api_code_adapter.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 |
---|---|---|
@@ -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, | ||
}); | ||
} |
255 changes: 255 additions & 0 deletions
255
...li/src/commands/generate/graphql-client-code/generate_graphql_client_code_command.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 |
---|---|---|
@@ -0,0 +1,255 @@ | ||
import { beforeEach, describe, it, mock } from 'node:test'; | ||
import { fromNodeProviderChain } from '@aws-sdk/credential-providers'; | ||
import { GenerateGraphqlClientCodeCommand } from './generate_graphql_client_code_command.js'; | ||
import yargs, { CommandModule } from 'yargs'; | ||
import { | ||
TestCommandError, | ||
TestCommandRunner, | ||
} from '../../../test-utils/command_runner.js'; | ||
import assert from 'node:assert'; | ||
import path from 'path'; | ||
import { BackendIdentifierResolver } from '../../../backend-identifier/backend_identifier_resolver.js'; | ||
import { GenerateApiCodeAdapter } from './generate_api_code_adapter.js'; | ||
import { | ||
GenerateApiCodeFormat, | ||
GenerateApiCodeModelTarget, | ||
GenerateApiCodeStatementTarget, | ||
} from '@aws-amplify/model-generator'; | ||
|
||
describe('generate graphql-client-code command', () => { | ||
const generateApiCodeAdapter = new GenerateApiCodeAdapter( | ||
fromNodeProviderChain() | ||
); | ||
|
||
const writeToDirectoryMock = mock.fn(); | ||
const invokeGenerateApiCodeMock = mock.method( | ||
generateApiCodeAdapter, | ||
'invokeGenerateApiCode', | ||
() => | ||
Promise.resolve({ | ||
writeToDirectory: writeToDirectoryMock, | ||
}) | ||
); | ||
|
||
const backendIdentifierResolver = new BackendIdentifierResolver({ | ||
resolve: () => Promise.resolve('testAppName'), | ||
}); | ||
const generateGraphqlClientCodeCommand = new GenerateGraphqlClientCodeCommand( | ||
generateApiCodeAdapter, | ||
backendIdentifierResolver | ||
); | ||
const parser = yargs().command( | ||
generateGraphqlClientCodeCommand as unknown as CommandModule | ||
); | ||
const commandRunner = new TestCommandRunner(parser); | ||
|
||
beforeEach(() => { | ||
invokeGenerateApiCodeMock.mock.resetCalls(); | ||
writeToDirectoryMock.mock.resetCalls(); | ||
}); | ||
|
||
it('generates and writes graphql client code for stack', async () => { | ||
await commandRunner.runCommand('graphql-client-code --stack stack_name'); | ||
assert.equal(invokeGenerateApiCodeMock.mock.callCount(), 1); | ||
assert.deepEqual(invokeGenerateApiCodeMock.mock.calls[0].arguments[0], { | ||
stackName: 'stack_name', | ||
format: GenerateApiCodeFormat.GRAPHQL_CODEGEN, | ||
statementTarget: GenerateApiCodeStatementTarget.JAVASCRIPT, | ||
}); | ||
assert.equal(writeToDirectoryMock.mock.callCount(), 1); | ||
assert.equal( | ||
writeToDirectoryMock.mock.calls[0].arguments[0], | ||
process.cwd() | ||
); | ||
}); | ||
|
||
it('generates and writes graphql client code for branch', async () => { | ||
await commandRunner.runCommand('graphql-client-code --branch branch_name'); | ||
assert.equal(invokeGenerateApiCodeMock.mock.callCount(), 1); | ||
assert.deepEqual(invokeGenerateApiCodeMock.mock.calls[0].arguments[0], { | ||
appName: 'testAppName', | ||
branchName: 'branch_name', | ||
format: GenerateApiCodeFormat.GRAPHQL_CODEGEN, | ||
statementTarget: GenerateApiCodeStatementTarget.JAVASCRIPT, | ||
}); | ||
assert.equal(writeToDirectoryMock.mock.callCount(), 1); | ||
assert.equal( | ||
writeToDirectoryMock.mock.calls[0].arguments[0], | ||
process.cwd() | ||
); | ||
}); | ||
|
||
it('generates and writes graphql client code for appID and branch', async () => { | ||
await commandRunner.runCommand( | ||
'graphql-client-code --branch branch_name --appId app_id' | ||
); | ||
assert.equal(invokeGenerateApiCodeMock.mock.callCount(), 1); | ||
assert.deepEqual(invokeGenerateApiCodeMock.mock.calls[0].arguments[0], { | ||
backendId: 'app_id', | ||
branchName: 'branch_name', | ||
format: GenerateApiCodeFormat.GRAPHQL_CODEGEN, | ||
statementTarget: GenerateApiCodeStatementTarget.JAVASCRIPT, | ||
}); | ||
assert.equal(writeToDirectoryMock.mock.callCount(), 1); | ||
assert.equal( | ||
writeToDirectoryMock.mock.calls[0].arguments[0], | ||
process.cwd() | ||
); | ||
}); | ||
|
||
it('can generate to custom relative path', async () => { | ||
await commandRunner.runCommand( | ||
'graphql-client-code --stack stack_name --out foo/bar' | ||
); | ||
assert.equal(invokeGenerateApiCodeMock.mock.callCount(), 1); | ||
assert.deepEqual(invokeGenerateApiCodeMock.mock.calls[0].arguments[0], { | ||
stackName: 'stack_name', | ||
format: GenerateApiCodeFormat.GRAPHQL_CODEGEN, | ||
statementTarget: GenerateApiCodeStatementTarget.JAVASCRIPT, | ||
}); | ||
assert.equal(writeToDirectoryMock.mock.callCount(), 1); | ||
assert.equal( | ||
writeToDirectoryMock.mock.calls[0].arguments[0], | ||
path.join(process.cwd(), 'foo', 'bar') | ||
); | ||
}); | ||
|
||
it('shows available options in help output', async () => { | ||
const output = await commandRunner.runCommand('graphql-client-code --help'); | ||
assert.match(output, /--stack/); | ||
assert.match(output, /--appId/); | ||
assert.match(output, /--branch/); | ||
assert.match(output, /--format/); | ||
assert.match(output, /--statementTarget/); | ||
assert.match(output, /--typeTarget/); | ||
assert.match(output, /--modelTarget/); | ||
assert.match(output, /--out/); | ||
assert.match(output, /--all/); | ||
}); | ||
|
||
it('shows all available options in help output', async () => { | ||
const output = await commandRunner.runCommand( | ||
'graphql-client-code --help --all' | ||
); | ||
assert.match(output, /--stack/); | ||
assert.match(output, /--appId/); | ||
assert.match(output, /--branch/); | ||
assert.match(output, /--format/); | ||
assert.match(output, /--statementTarget/); | ||
assert.match(output, /--typeTarget/); | ||
assert.match(output, /--modelTarget/); | ||
assert.match(output, /--out/); | ||
assert.match(output, /--all/); | ||
assert.match(output, /--modelGenerateIndexRules/); | ||
assert.match(output, /--modelEmitAuthProvider/); | ||
assert.match(output, /--modelAddTimestampFields/); | ||
assert.match(output, /--statementMaxDepth/); | ||
assert.match(output, /--statementTypenameIntrospection/); | ||
assert.match(output, /--typeMultipleSwiftFiles/); | ||
}); | ||
|
||
it('can be invoked explicitly with graphql-codegen format', async () => { | ||
await commandRunner.runCommand( | ||
'graphql-client-code --stack stack_name --format graphql-codegen' | ||
); | ||
assert.equal(invokeGenerateApiCodeMock.mock.callCount(), 1); | ||
assert.deepEqual(invokeGenerateApiCodeMock.mock.calls[0].arguments[0], { | ||
stackName: 'stack_name', | ||
format: GenerateApiCodeFormat.GRAPHQL_CODEGEN, | ||
statementTarget: GenerateApiCodeStatementTarget.JAVASCRIPT, | ||
}); | ||
assert.equal(writeToDirectoryMock.mock.callCount(), 1); | ||
assert.equal( | ||
writeToDirectoryMock.mock.calls[0].arguments[0], | ||
process.cwd() | ||
); | ||
}); | ||
|
||
it('can be invoked explicitly with modelgen format', async () => { | ||
await commandRunner.runCommand( | ||
'graphql-client-code --stack stack_name --format modelgen' | ||
); | ||
assert.equal(invokeGenerateApiCodeMock.mock.callCount(), 1); | ||
assert.deepEqual(invokeGenerateApiCodeMock.mock.calls[0].arguments[0], { | ||
stackName: 'stack_name', | ||
format: GenerateApiCodeFormat.MODELGEN, | ||
modelTarget: GenerateApiCodeModelTarget.JAVASCRIPT, | ||
}); | ||
assert.equal(writeToDirectoryMock.mock.callCount(), 1); | ||
assert.equal( | ||
writeToDirectoryMock.mock.calls[0].arguments[0], | ||
process.cwd() | ||
); | ||
}); | ||
|
||
it('can be invoked explicitly with introspection format', async () => { | ||
await commandRunner.runCommand( | ||
'graphql-client-code --stack stack_name --format introspection' | ||
); | ||
assert.equal(invokeGenerateApiCodeMock.mock.callCount(), 1); | ||
assert.deepEqual(invokeGenerateApiCodeMock.mock.calls[0].arguments[0], { | ||
stackName: 'stack_name', | ||
format: GenerateApiCodeFormat.INTROSPECTION, | ||
}); | ||
assert.equal(writeToDirectoryMock.mock.callCount(), 1); | ||
assert.equal( | ||
writeToDirectoryMock.mock.calls[0].arguments[0], | ||
process.cwd() | ||
); | ||
}); | ||
|
||
it('passes in feature flags on modelgen', async () => { | ||
await commandRunner.runCommand( | ||
'graphql-client-code --stack stack_name --format modelgen --modelGenerateIndexRules true --modelEmitAuthProvider true --modelGenerateModelsForLazyLoadAndCustomSelectionSet false' | ||
); | ||
assert.equal(invokeGenerateApiCodeMock.mock.callCount(), 1); | ||
assert.deepEqual(invokeGenerateApiCodeMock.mock.calls[0].arguments[0], { | ||
stackName: 'stack_name', | ||
format: GenerateApiCodeFormat.MODELGEN, | ||
modelTarget: GenerateApiCodeModelTarget.JAVASCRIPT, | ||
generateIndexRules: true, | ||
emitAuthProvider: true, | ||
generateModelsForLazyLoadAndCustomSelectionSet: false, | ||
}); | ||
assert.equal(writeToDirectoryMock.mock.callCount(), 1); | ||
assert.equal( | ||
writeToDirectoryMock.mock.calls[0].arguments[0], | ||
process.cwd() | ||
); | ||
}); | ||
|
||
it('passes in feature flags on graphql-codegen', async () => { | ||
await commandRunner.runCommand( | ||
'graphql-client-code --stack stack_name --format graphql-codegen --statementTarget typescript --statementMaxDepth 3 --statementTypenameIntrospection true' | ||
); | ||
assert.equal(invokeGenerateApiCodeMock.mock.callCount(), 1); | ||
assert.deepEqual(invokeGenerateApiCodeMock.mock.calls[0].arguments[0], { | ||
stackName: 'stack_name', | ||
format: GenerateApiCodeFormat.GRAPHQL_CODEGEN, | ||
statementTarget: GenerateApiCodeStatementTarget.TYPESCRIPT, | ||
maxDepth: 3, | ||
typenameIntrospection: true, | ||
}); | ||
assert.equal(writeToDirectoryMock.mock.callCount(), 1); | ||
assert.equal( | ||
writeToDirectoryMock.mock.calls[0].arguments[0], | ||
process.cwd() | ||
); | ||
}); | ||
|
||
// Note: after this test, future tests seem to be in a weird state, leaving this at the | ||
it('fails if both stack and branch are present', async () => { | ||
await assert.rejects( | ||
() => | ||
commandRunner.runCommand( | ||
'graphql-client-code --stack foo --branch baz' | ||
), | ||
(err: TestCommandError) => { | ||
assert.equal(err.error.name, 'YError'); | ||
assert.match(err.error.message, /Arguments .* mutually exclusive/); | ||
assert.match(err.output, /Arguments .* are mutually exclusive/); | ||
return true; | ||
} | ||
); | ||
}); | ||
}); |
Oops, something went wrong.