"Not Authorized to access " error on create mutation + GraphQL
Asked Answered
T

3

7

I'm running the below GraphQL query to create data on dynamodb table, but gets the response with "Not Authorized to access createClient on type Client"

Why this is happening at.

GraphQL query:

`mutation addClient{
  createClient(input:{
    fullName: "Jhon Smith"
    title: "Software Engineer"
    organization: "AWS"
    contactNo: "+341289655524"
    email: "[email protected]"
    country: "Canada"
    address: "AN/458, Norton place, Down Town"    
  }){
    fullName
  }
}`

Response :

{
  "data": {
    "createClient": null
  },
  "errors": [
    {
      "path": [
        "createClient"
      ],
      "data": null,
      "errorType": "Unauthorized",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "Not Authorized to access createClient on type Client"
    }
  ]
}
Twopenny answered 5/2, 2022 at 7:59 Comment(2)
Is it possible that the HTTP client that sends the GraphQL query to the server need to also send an Authorization header with an authentication token? (or some other form of authentication)Hysterectomize
Seems yes. it needs some autorization. Do you have any samples for that?Twopenny
T
7

It solved with authMode

const newTodo = await API.graphql({ query: mutations.createClient, variables: {input: inputData}, authMode: "AMAZON_COGNITO_USER_POOLS" });

Twopenny answered 9/2, 2022 at 11:44 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Gnarly
This answer worked for me, some more info in the amplify docsBartizan
I
0

Make sure your model @auth directive has ... operation: [create, ...] included. This is what I was running into.

Idolum answered 8/2 at 22:16 Comment(0)
P
0

I had similar issue with the error - error fetching {"data": {"createTodo": null}, "errors": [{"data": null, "errorInfo": null, "errorType": "Unauthorized", "locations": [Array], "message": "Not Authorized to access createTodo on type Mutation", "path": [Array]}]}

Turn out the amplifyconfiguration.json file had "aws_appsync_authenticationType": "API_KEY" where "API_KEY" shall be replaced with "AMAZON_COGNITO_USER_POOLS"

Prodigy answered 20/3 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.