Skip to content

Commit

Permalink
test: add generate codegen commands
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronZyLee committed May 3, 2024
1 parent 124f66d commit 51fefab
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Schema } from '../resource'

export const handler: Schema["echoMutation"]["functionHandler"] = async (event, context) => {
return {
id: 'todo1',
content: `Echoing content: ${event.arguments.requiredContent}`,
status: 'COMPLETED',
createdAt: performance.now().toString(),
updatedAt: performance.now().toString(),
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Schema } from '../resource'

export const handler: Schema["echoQuery"]["functionHandler"] = async (event, context) => {
const start = performance.now();
return {
content: `Echoing content: ${event.arguments.content}`,
executionDuration: performance.now() - start
};
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { type ClientSchema, a, defineData } from '@aws-amplify/backend';
import { type ClientSchema, a, defineData, defineFunction } from '@aws-amplify/backend';

const echoQueryHandler = defineFunction({
entry: './handlers/query.ts'
});
const echoMutationHandler = defineFunction({
entry: './handlers/mutation.ts'
});

const schema = a.schema({
// Model type
Expand All @@ -13,6 +20,10 @@ const schema = a.schema({
'PROGRESS',
'COMPLETED',
]),
EchoQueryStatus: a.enum([
'PROGRESS',
'COMPLETED',
]),
// Non model type
EchoResponse: a.customType({
content: a.string(),
Expand All @@ -26,14 +37,16 @@ const schema = a.schema({
status: a.enum(['PROGRESS', 'COMPLETED']),
})
.returns(a.ref('EchoResponse'))
.authorization(allow => [allow.authenticated()]),
.authorization(allow => [allow.authenticated()])
.handler(a.handler.function(echoQueryHandler)),
echoMutation: a
.mutation()
.arguments({
requiredContent: a.string().required()
})
.returns(a.ref('Todo').required())
.authorization(allow => [allow.authenticated()]),
.authorization(allow => [allow.authenticated()])
.handler(a.handler.function(echoMutationHandler)),
});

export type Schema = ClientSchema<typeof schema>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ describe('GraphQL generator for Gen2 e2e tests', () => {
});

it('should not throw error when generating GraphQL client code', async () => {
await expect(generateGraphqlClientCode(projRoot)).resolves.not.toThrow();
await expect(generateGraphqlClientCode(projRoot, { outDir: 'codegen', format: 'introspection'})).resolves.not.toThrow();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,46 @@ export const deleteSandbox = async (cwd: string): Promise<void> => {
.runAsync();
}

export const generateGraphqlClientCode = async (cwd: string, props: any = {}): Promise<void> => {
/**
* Commands for ampx generate
*/
type ClientCodegenConfigBase = {
format: string
outDir: string
}

type IntrospectionCodegenConfig = ClientCodegenConfigBase & {
format: 'introspection'
}
type ModelgenConfig = ClientCodegenConfigBase & {
format: 'modelgen'
modelTarget: string
}
type GraphqlCodegenConfig = ClientCodegenConfigBase & {
format: 'graphql-codegen'
typeTarget: string
statementTarget: string
}
type ClientCodegenConfig = IntrospectionCodegenConfig | ModelgenConfig | GraphqlCodegenConfig

const getClientCodegenParams = (props: ClientCodegenConfig): string[] => {
const params = [ '--out', props.outDir, '--format', props.format ]
switch (props.format) {
case 'modelgen':
return [ ...params, '--model-target', props.modelTarget];
case 'graphql-codegen':
return [ ...params, '--type-target', props.typeTarget, '--statement-target', props.statementTarget]
case 'introspection':
default:
return params;
}
}

export const generateGraphqlClientCode = async (cwd: string, props: ClientCodegenConfig): Promise<void> => {
await
spawn(
getNpxPath(),
['ampx', 'generate', 'graphql-client-code'],
['ampx', 'generate', 'graphql-client-code', ...getClientCodegenParams(props)],
{ cwd, stripColors: true },
).runAsync();
}
Expand Down

0 comments on commit 51fefab

Please sign in to comment.