70 %

Toast is in Beta!Learn more →

Chris Biscardi

Amplify JS graphqlOperation does nothing, don't use it

graphqlOperation is mentioned quite a bit in the amplify docs. It comes up for mutations, queries, and subscriptions in various places.

javascript
import { API } from 'aws-amplify';
import { createTodo, updateTodo, deleteTodo } from './graphql/mutations';
const todo = { name: "My first todo", description: "Hello world!" };
/* create a todo */
await API.graphql(graphqlOperation(createTodo, {input: todo}));
/* update a todo */
await API.graphql(graphqlOperation(updateTodo, { input: { id: todoId, name: "Updated todo info" }}));
/* delete a todo */
await API.graphql(graphqlOperation(deleteTodo, { input: { id: todoId }}));

but if we look at the source code, it doesn't do anything but obscure the object that's being passed in to people who are unfamiliar with it.

My suggestion: don't use it unless you're 100% bought into amplify codegen. It doesn't make sense if you're writing your own queries.

The alternative is

JS
API.graphql({
query: `
query GetWorkspace($id: String!) {
workspace(id: $id) {
id
name
}
}
`,
variables: { id: "someid" },
});