'AWS Amplify (GraphQL) - Using "graphqlOperation" vs Normal Query?

Might be a simple question, but I initially have been using the graphqlOperation for API queries in AWS Amplify. Recently I started utilizing Cognito User Pools. For the life of me, I could not pass authMode into it and ended up using a normal query.

## normal query - works
await API.graphql({query: listForms, authMode: 'AMAZON_COGNITO_USER_POOLS'});

## using graphqlOperation - does not work (Unauthorized)
await API.graphql(graphqlOperation(listForms,  { authMode: 'AMAZON_COGNITO_USER_POOLS' }));

I'm ok with using the normal query but I'm wondering why you'd use the graphqlOperation class at all if the normal query seems to suffice? They both seem to return the same payload. Most documentation seems to reference using graphqlOperation. I'm just not sure what benefit it brings.

Thanks! T



Solution 1:[1]

You can't use graphqlOperation with authMode. Use just API.graphql() instead.

"Previous examples uses graphqlOperation function. That function only creates an object with two attributes query and variables. In order to use authMode you need to pass this object as is mentioned on the previous example."

See Configure authorization modes

Solution 2:[2]

One of the reasons to use graphqlOperation is in the case your method listMessages has any parameters you could pass those in the call of graphqlOperation(listMessages, {...params})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anja
Solution 2 alexandervonwl