简体   繁体   中英

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

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

使用 graphqlOperation 的原因之一是如果您的方法 listMessages 有任何参数,您可以在 graphqlOperation(listMessages, {...params}) 的调用中传递这些参数

Docs don't mention it, but you can simply do this. Works for me.

      await API.graphql({
        ...graphqlOperation(createTodo, { input: todo }),
        authMode: "AMAZON_COGNITO_USER_POOLS",
      });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM