Create Custom GraphQL Queries in AWS Amplify
AWS Amplify uses code generation to create GraphQL queries for you when you add an API. This is super handy, but say you want to add a custom query. What do you do?
The trick here is that when you change your schema, the GraphQL files are re-created for you, overwritting what is already in there -- so you can't add the query in the generated files without having to re-add them whenever you update your schema.
There are two options for how you can add your own queries.
First, if it's not a query you want to reuse much, you can write it inline:
const gifs = await API.graphql(graphqlOperation(`
query GetGifs {
listGifs {
items {
altText
id
url
}
}
}
`))
If you want the query to be more reusable, you can create another file in the graphql
directory -- for example custom-queries.js
and mirror the formatting of the queries in the queries.js file.