question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GraphQLError request failed with status code 401

See original GitHub issue

Which Category is your question related to? Auth / GraphQL

What AWS Services are you utilizing? AWS AppSync

Provide additional details e.g. code snippets

In our schema.graphql, we added @function directive to the field inside the Query type, in order to invoke our recoverUsername lambda function:

type Query {
  recoverUsername(email: String): String
  @function(name: "recoverUsername-${env}")
  @aws_iam
}

It works when I execute this query in the AWS AppSync console by selecting IAM auth provider, but when I try to call it from the app with:

API.graphql({
  query: recoverUsername,
  variables: {
    email,
  },
  authMode: 'AWS_IAM',
});
aws-exports.js

const awsmobile = {
  aws_appsync_authenticationType: 'AWS_IAM',
  ...
}

I get GraphQLError request failed with status code 401

We are using older version of aws-amplify (1.2.4), but I also tried with latest version, same error.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
attilahcommented, Feb 26, 2020

@cc-bojan when you’re logged into the AWS Console you’re probably using an administrator role credentials that does not need to have a specific policy to invoke AppSync mutations, that’s the probable reason why it works.

If you use the AppSync service supported native directives like @aws_iam the Amplify CLI will not generate any policies for you for the Auth and Unauth roles, so when from the client application you call the Amplify service and specifying IAM as authentication mode, it will use the AWS credentials of the currently logged in user, and that user’s role (Auth or Unauth) has no attached policies defined that would grant access for AppSync service to invoke that Lambda function.

Could you please confirm that the above is correct? And if it is correct create a policy and verify that it is working?

1reaction
cc-bojancommented, Feb 29, 2020

@attilah You were right. I attached: arn:aws:appsync:eu-central-1:<account-id>:apis/<api-id>/types/Query/fields/recoverUsername - Resource to the existing UnauthRolePolicy, and now it’s working. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

GraphQLError: Request failed with status code 401 #2783
I am currently deploying through the AWS Amplify console through bitbucket. I noticed that aws-exports.js file is automatically ignored by .
Read more >
Resolve unauth errors for GraphQL requests in AWS AppSync
401 Unauthorized: The request is denied by either AWS AppSync or the authorization mode because the credentials are missing or invalid. 200 OK ......
Read more >
AWS Amplify AppSync IAM 401 - Stack Overflow
I'm getting GraphQLError: Request failed with status code 401. I followed the automatic configuration instructions from:.
Read more >
AWS AppSync Error: Request failed with status code 401
I need help with this 401 error status, how do I go about fixing it, as I can't run my query this way....
Read more >
GraphQL Error (Code: 401): {"response":{"data":[{"id":"
The error happens client-side where it seems that the client does not have access to NEXT_ENV_DATOCMS_API_TOKEN. That's why it only works when ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found