amplify-codegen
amplify-codegen copied to clipboard
Types not generated for non null return types for mutations
Before opening, please confirm:
- [X] I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
- [X] I have searched for duplicate or closed issues.
- [X] I have read the guide for submitting bug reports.
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
How did you install the Amplify CLI?
npm
If applicable, what version of Node.js are you using?
v18.16.1
Amplify CLI Version
12.10.1
What operating system are you using?
Mac
Amplify Codegen Command
codegen
Describe the bug
Having a non null return type for a mutation doesn't generate the appropriate return type in API.service.ts. If I have a mutation like
type Mutation {
updateFoo(bar: String!): String!
@function(name: "ResolverFn")
@auth(rules: [{ allow: groups, groups: ["User"] }])
}
The generated code in API.service.ts looks like
async UpdateFoo(bar: string): Promise<UpdateFooMutation> {
const statement = `mutation UpdateFoo($bar: String!) {
updateFoo(bar: $bar)
}`;
const gqlAPIServiceArguments: any = {
bar
};
const response = (await API.graphql(
graphqlOperation(statement, gqlAPIServiceArguments)
)) as any;
return <UpdateFooMutation>response.data.updateFoo;
}
But theres no UpdateFooMutation
created
Expected behavior
I would expect codegen to generate the NonNullReturnTypeMutation type in API.service.ts. In the context of the bug description above it would be UpdateFooMutation
Reproduction steps
- Add the following to schema.graphql
updateFoo(bar: String!): String!
@function(name: "ResolverFn")
@auth(rules: [{ allow: groups, groups: ["User"] }])
- Run amplify api gql-compile && amplify codegen
GraphQL schema(s)
# Put schemas below this line
type Mutation {
updateFoo(bar: String!): String!
@function(name: "ResolverFn")
@auth(rules: [{ allow: groups, groups: ["User"] }])
}
Log output
No response
Additional information
I get the following GeneratedMutation
in mutations.ts
export const updateFoo = /* GraphQL */ `mutation UpdateFoo($bar: String!) {
updateFoo(bar: $bar)
}
` as GeneratedMutation<
APITypes.UpdateFooMutationVariables,
APITypes.UpdateFooMutation
>;
This is the generated code in mutation.graphql
mutation UpdateFoo($bar: String!) {
updateFoo(bar: $bar)
}
This is the generated code in graphql-schema.ts
export type UpdateFooMutationVariables = {
bar: string,
};
export type UpdateFooMutation = {
updateFoo: string,
};
I was able to reproduce this issue. This will be resolved for customers using Amplify JS library V6 with https://github.com/aws-amplify/amplify-codegen/pull/799.
An additional fix is needed for V5 customers.