"Not Authorized to access " error on create mutation + GraphQL

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"
    }
  ]
}

Upvotes: 7

Views: 8690

Answers (3)

Anant Tiwari
Anant Tiwari

Reputation: 1

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"

Upvotes: 0

C RICH
C RICH

Reputation: 513

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

Upvotes: 0

It solved with authMode

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

Upvotes: 7

Related Questions