amplify-codegen
amplify-codegen copied to clipboard
How do I generate exportable graphql input types using `amplify codegen`?
** Which Category is your question related to? **
codegen
** What AWS Services are you utilizing? **
appsync, dynamodb
** Provide additional details e.g. code snippets **
I'm trying to follow the documentation here https://aws-amplify.github.io/docs/js/api#aws-appsync-sdk and there is an example for using buildMutation given:
import { listTodos } from './graphql/queries';
import { createTodo, CreateTodoInput } from './graphql/mutations';
(async () => {
const result = await client.mutate(buildMutation(client,
gql(createTodo),
{
inputType: gql(CreateTodoInput),
variables: {
input: {
name: 'Use AppSync',
description: 'Realtime and Offline',
}
}
},
(_variables) => [ gql(listTodos) ],
'Todo'));
console.log(result);
})();
Specifically, import { createTodo, CreateTodoInput } from './graphql/mutations';
When I run amplify codegen, the generated src/graphql/mutations.js does not include input types (e.g. CreateTodoInput in the case of the example given). How do I generate these, so I can use buildMutation in a similar manner?
Just noticed I used aws instead of amplify everywhere. My bad :) aws mindshare is strong
codegen does not generate input types, but its needed for offline support. We will have to update the codegen to generate input types to make it easier to support offline support
@yuth could the input types generated by AppSync be downloaded with something like amplify api pull --types?
@jkeys-ecg-nmsu the compiled schema should already be available to you inside <proj-root>/amplify/backedn/api/<api-name>/build/schema.json and should have the input type.
I have added this issue to our backlog to generate input types. I will try to get to this as soon as this gets prioritized.
It seams that you can pass the entire schema into the "inputType":
import schemaGraphQL from "../../../../../amplify/backend/api/[project name]/build/schema.graphql"
then:
makePost() {
return (async () => {
const result = await this.$apollo.provider.defaultClient.mutate(
buildMutation(
this.$apollo.provider.defaultClient,
gql(require("@/graphql/mutations.js").createPost),
{
inputType: schemaGraphQL,
variables: {
input: {
title: "My post",
content: "This is the post",
}
}
},
_variables => [gql(require("@/graphql/queries.js").listPosts)],
"listPosts",
"CreatePostInput",
"id",
"AUTO"
)
);
console.log(result);
})();
},
And Im using the graphql-tag/loader: https://github.com/apollographql/graphql-tag
It seams not to work with the schema.json file...
I'm not sure if this is a good idea or not? But the goal must be to define everything once in schema.graphql and reuse everything from there on.
Set input type manually as follows. It works.
inputType: gql(`input CreateTodoInput {
id: ID
name: String!
}`),
Hi @timrchavez .The input types are generated for typescript and flow targets during amplify codegen add workflow. Would be awesome if you could take advantage of those in the meantime.