-
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 form cli --------- Co-authored-by: Edward Foyle <[email protected]> Co-authored-by: Amplifiyer <[email protected]>
- Loading branch information
1 parent
d5aba32
commit 6dc935f
Showing
15 changed files
with
11,571 additions
and
13,165 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 form generation interface |
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 |
---|---|---|
|
@@ -24,3 +24,6 @@ yarn-error.log* | |
|
||
cdk.out | ||
src/amplifyconfiguration.js | ||
src/ui-components/ | ||
src/graphql/ | ||
|
Large diffs are not rendered by default.
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
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
43 changes: 43 additions & 0 deletions
43
packages/cli/src/commands/generate/forms/form_generation_handler.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,43 @@ | ||
import path from 'path'; | ||
import { createLocalGraphqlFormGenerator } from '@aws-amplify/form-generator'; | ||
import { createGraphqlDocumentGenerator } from '@aws-amplify/model-generator'; | ||
import { BackendIdentifier } from '@aws-amplify/deployed-backend-client'; | ||
import { AwsCredentialIdentityProvider } from '@aws-sdk/types'; | ||
|
||
type FormGenerationParams = { | ||
modelsOutDir: string; | ||
uiOutDir: string; | ||
apiUrl: string; | ||
backendIdentifier: BackendIdentifier; | ||
}; | ||
type FormGenerationInstanceOptions = { | ||
credentialProvider: AwsCredentialIdentityProvider; | ||
}; | ||
/** | ||
* Creates a handler for FormGeneration | ||
*/ | ||
export class FormGenerationHandler { | ||
/** | ||
* Instantiates the handler | ||
*/ | ||
constructor(private readonly formGenParams: FormGenerationInstanceOptions) {} | ||
generate = async (params: FormGenerationParams) => { | ||
const { backendIdentifier, modelsOutDir, uiOutDir, apiUrl } = params; | ||
const { credentialProvider } = this.formGenParams; | ||
const graphqlClientGenerator = createGraphqlDocumentGenerator({ | ||
backendIdentifier, | ||
credentialProvider, | ||
}); | ||
const modelsResult = await graphqlClientGenerator.generateModels({ | ||
language: 'typescript', | ||
}); | ||
await modelsResult.writeToDirectory(modelsOutDir); | ||
const relativePath = path.relative(uiOutDir, modelsOutDir); | ||
const localFormGenerator = createLocalGraphqlFormGenerator({ | ||
introspectionSchemaUrl: apiUrl, | ||
graphqlModelDirectoryPath: relativePath, | ||
}); | ||
const result = await localFormGenerator.generateForms(); | ||
await result.writeToDirectory(uiOutDir); | ||
}; | ||
} |
199 changes: 199 additions & 0 deletions
199
packages/cli/src/commands/generate/forms/generate_forms_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,199 @@ | ||
import { graphqlOutputKey } from '@aws-amplify/backend-output-schemas'; | ||
import { BackendOutputClient } from '@aws-amplify/deployed-backend-client'; | ||
import { fromNodeProviderChain } from '@aws-sdk/credential-providers'; | ||
import assert from 'node:assert'; | ||
import { describe, it, mock } from 'node:test'; | ||
import yargs, { CommandModule } from 'yargs'; | ||
import { BackendIdentifierResolver } from '../../../backend-identifier/backend_identifier_resolver.js'; | ||
import { TestCommandRunner } from '../../../test-utils/command_runner.js'; | ||
import { FormGenerationHandler } from './form_generation_handler.js'; | ||
import { GenerateFormsCommand } from './generate_forms_command.js'; | ||
|
||
void describe('generate forms command', () => { | ||
void describe('form generation validation', () => { | ||
void it('modelsOutDir path can be customized', async () => { | ||
const credentialProvider = fromNodeProviderChain(); | ||
|
||
const backendIdResolver = new BackendIdentifierResolver({ | ||
resolve: () => Promise.resolve('testAppName'), | ||
}); | ||
const formGenerationHandler = new FormGenerationHandler({ | ||
credentialProvider, | ||
}); | ||
|
||
const fakedBackendOutputClient = new BackendOutputClient( | ||
credentialProvider, | ||
{ stackName: 'test_stack' } | ||
); | ||
|
||
const generateFormsCommand = new GenerateFormsCommand( | ||
backendIdResolver, | ||
() => fakedBackendOutputClient, | ||
formGenerationHandler | ||
); | ||
|
||
const generationMock = mock.method(formGenerationHandler, 'generate'); | ||
generationMock.mock.mockImplementation(async () => undefined); | ||
mock | ||
.method(fakedBackendOutputClient, 'getOutput') | ||
.mock.mockImplementation(async () => ({ | ||
[graphqlOutputKey]: { | ||
payload: { | ||
awsAppsyncApiId: 'test_api_id', | ||
amplifyApiModelSchemaS3Uri: 'test_schema', | ||
awsAppsyncApiEndpoint: 'test_endpoint', | ||
}, | ||
}, | ||
})); | ||
const parser = yargs().command( | ||
generateFormsCommand as unknown as CommandModule | ||
); | ||
|
||
const modelsOutPath = './my-fake-models-path'; | ||
const commandRunner = new TestCommandRunner(parser); | ||
await commandRunner.runCommand( | ||
`forms --stack my_stack --modelsOutDir ${modelsOutPath}` | ||
); | ||
assert.equal( | ||
generationMock.mock.calls[0].arguments[0].modelsOutDir, | ||
modelsOutPath | ||
); | ||
}); | ||
void it('uiOutDir path can be customized', async () => { | ||
const credentialProvider = fromNodeProviderChain(); | ||
|
||
const backendIdResolver = new BackendIdentifierResolver({ | ||
resolve: () => Promise.resolve('testAppName'), | ||
}); | ||
const formGenerationHandler = new FormGenerationHandler({ | ||
credentialProvider, | ||
}); | ||
|
||
const fakedBackendOutputClient = new BackendOutputClient( | ||
credentialProvider, | ||
{ stackName: 'test_stack' } | ||
); | ||
|
||
const generateFormsCommand = new GenerateFormsCommand( | ||
backendIdResolver, | ||
() => fakedBackendOutputClient, | ||
formGenerationHandler | ||
); | ||
|
||
const generationMock = mock.method(formGenerationHandler, 'generate'); | ||
generationMock.mock.mockImplementation(async () => undefined); | ||
mock | ||
.method(fakedBackendOutputClient, 'getOutput') | ||
.mock.mockImplementation(async () => ({ | ||
[graphqlOutputKey]: { | ||
payload: { | ||
awsAppsyncApiId: 'test_api_id', | ||
amplifyApiModelSchemaS3Uri: 'test_schema', | ||
awsAppsyncApiEndpoint: 'test_endpoint', | ||
}, | ||
}, | ||
})); | ||
const parser = yargs().command( | ||
generateFormsCommand as unknown as CommandModule | ||
); | ||
|
||
const uiOutPath = './my-fake-ui-path'; | ||
const commandRunner = new TestCommandRunner(parser); | ||
await commandRunner.runCommand( | ||
`forms --stack my_stack --uiOutDir ${uiOutPath}` | ||
); | ||
assert.equal( | ||
generationMock.mock.calls[0].arguments[0].uiOutDir, | ||
uiOutPath | ||
); | ||
}); | ||
void it('./src/ui-components is the default graphql model generation path', async () => { | ||
const credentialProvider = fromNodeProviderChain(); | ||
|
||
const backendIdResolver = new BackendIdentifierResolver({ | ||
resolve: () => Promise.resolve('testAppName'), | ||
}); | ||
const formGenerationHandler = new FormGenerationHandler({ | ||
credentialProvider, | ||
}); | ||
|
||
const fakedBackendOutputClient = new BackendOutputClient( | ||
credentialProvider, | ||
{ stackName: 'test_stack' } | ||
); | ||
|
||
const generateFormsCommand = new GenerateFormsCommand( | ||
backendIdResolver, | ||
() => fakedBackendOutputClient, | ||
formGenerationHandler | ||
); | ||
|
||
const generationMock = mock.method(formGenerationHandler, 'generate'); | ||
generationMock.mock.mockImplementation(async () => undefined); | ||
mock | ||
.method(fakedBackendOutputClient, 'getOutput') | ||
.mock.mockImplementation(async () => ({ | ||
[graphqlOutputKey]: { | ||
payload: { | ||
awsAppsyncApiId: 'test_api_id', | ||
amplifyApiModelSchemaS3Uri: 'test_schema', | ||
awsAppsyncApiEndpoint: 'test_endpoint', | ||
}, | ||
}, | ||
})); | ||
const parser = yargs().command( | ||
generateFormsCommand as unknown as CommandModule | ||
); | ||
const commandRunner = new TestCommandRunner(parser); | ||
await commandRunner.runCommand('forms --stack my_stack'); | ||
assert.equal( | ||
generationMock.mock.calls[0].arguments[0].uiOutDir, | ||
'./src/ui-components' | ||
); | ||
}); | ||
void it('./src/graphql is the default graphql model generation path', async () => { | ||
const credentialProvider = fromNodeProviderChain(); | ||
|
||
const backendIdResolver = new BackendIdentifierResolver({ | ||
resolve: () => Promise.resolve('testAppName'), | ||
}); | ||
const formGenerationHandler = new FormGenerationHandler({ | ||
credentialProvider, | ||
}); | ||
|
||
const fakedBackendOutputClient = new BackendOutputClient( | ||
credentialProvider, | ||
{ stackName: 'test_stack' } | ||
); | ||
|
||
const generateFormsCommand = new GenerateFormsCommand( | ||
backendIdResolver, | ||
() => fakedBackendOutputClient, | ||
formGenerationHandler | ||
); | ||
|
||
const generationMock = mock.method(formGenerationHandler, 'generate'); | ||
generationMock.mock.mockImplementation(async () => undefined); | ||
mock | ||
.method(fakedBackendOutputClient, 'getOutput') | ||
.mock.mockImplementation(async () => ({ | ||
[graphqlOutputKey]: { | ||
payload: { | ||
awsAppsyncApiId: 'test_api_id', | ||
amplifyApiModelSchemaS3Uri: 'test_schema', | ||
awsAppsyncApiEndpoint: 'test_endpoint', | ||
}, | ||
}, | ||
})); | ||
const parser = yargs().command( | ||
generateFormsCommand as unknown as CommandModule | ||
); | ||
const commandRunner = new TestCommandRunner(parser); | ||
await commandRunner.runCommand('forms --stack my_stack'); | ||
assert.equal( | ||
generationMock.mock.calls[0].arguments[0].modelsOutDir, | ||
'./src/graphql' | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.