amplify-js: Error Graphql Error running query [query]. Authmode Cognito. Error: Unauthorized
Before opening, please confirm:
- I have searched for duplicate or closed issues and discussions.
- I have read the guide for submitting bug reports.
- I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
JavaScript Framework
Angular
Amplify APIs
GraphQL API
Amplify Version
v6
Amplify Categories
auth, api
Backend
None
Environment information
# Put output below this line
System:
OS: macOS 14.4.1
CPU: (10) arm64 Apple M1 Max
Memory: 2.21 GB / 64.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.14.0 - /usr/local/bin/node
Yarn: 1.22.18 - ~/.npm-global/bin/yarn
npm: 8.19.1 - ~/.npm-global/bin/npm
pnpm: 9.0.0 - ~/.npm-global/bin/pnpm
Browsers:
Chrome: 127.0.6533.100
Safari: 17.4.1
npmGlobalPackages:
@angular/cli: 17.0.3
angular-http-server: 1.10.0
aws-cdk: 2.146.0
aws: 0.0.3-2
envinfo: 7.13.0
firebase-tools: 11.16.1
nativescript: 8.2.3
node-gyp: 8.4.1
npm: 8.19.1
pnpm: 9.0.0
yarn: 1.22.18
Describe the bug
I am seeing the clientside JS error Error: Graphql Error running query getMusicStream. Authmode Cognito. Error: Unauthorized occur many times in our client logging system.
It is caught by the custom Angular Error handler that will redirect to an Oops page which is okay for a real error, but not this one.
The cause is an expired accessToken, I am sure of it.
The error was reported at 8:25 AM
When I look at the accessToken for this request (I logged that too), I see the expiry was at 8:25:
There is a valid Refresh token present.
The user did an action which resulted in a graphql Api call which caused the above issue and then resulted in an Oops page, instead of being issues a new accessToken by the graphql Api client code.
When does it issue not happen: I was logged into our production system, last time used yesterday, so my accesstoken is expired. I got to the same URL with my dev console open. The application works as expected, new token is retrieved which is valid for 15min and the page displays. No error. So the mechanism works fine this way.
The issue seems to be a live session where the accessToken expires and needs to be renewed; a graphql error slips through. Question: is that by design or a bug? Question: if I tell my custom error handler to ignore this, will the graphql Api attempt another call with a new accessToken, or will the JS Error mean the end of the execution and the application is dead as of that moment until a customer does a reload?
Expected behavior
I expect when an accessToken is expiring at the same moment a API call is made, I do not receive an UNAUTHORISED error. Maybe the grahpql client code should consider each token that is about to expire within 15 seconds as expired and retrieve a new accessToken so that the API call that follows will not receive the UNAUTHORISED error.
Maybe that is already how the system works, not sure.
Reproduction steps
-Login to cognito via Amplify client in an Angular app -Have an Graphql API call ready that requires you to be logged in to Cognito -wait for the accessToken to expire and run a couple of API calls to try to trigger this error. Sometimes it happens, sometimes it doesnt.
Code Snippet
// THIS IS MY API IMPLEMENTATION USING RXJS
export class ApiService {
private client = generateClient();
graphql<V>({ statement: query, variables, type, iam = false, familyApi = false }: queryInput) {
if (familyApi) {
Amplify.configure(awsConfig.familyApp);
} else {
Amplify.configure(awsConfig.adminApp);
}
const payload: any = {
query,
authMode: iam ? 'iam' : 'userPool',
}
if (variables) {
payload.variables = variables;
}
return from(this.client.graphql(payload) as Promise<GraphQLResult<object>>)
.pipe(
retry({ count: 3, delay: 1000, resetOnSuccess: true}),
map(res => res.data[type as keyof object]),
catchError((error: GraphQLResult<object> | Error) => {
// graphql error
if ('errors' in error) {
const gqError = error.errors![0];
throw new Error(`Graphql Error running query ${type}. Authmode ${iam ? 'IAM' : 'Cognito'}. Error: ${gqError?.message} ${gqError?.path ? gqError.path.toString() : ''}`);
// normal error
} else if ('stack' in error) {
throw new Error(`Runtime error running query ${type}. Authmode ${iam ? 'IAM' : 'Cognito'}. Error: ${error.stack}}`);
// fallback for something else
} else {
throw new Error(`Unknown Error running query ${type}. Authmode ${iam ? 'IAM' : 'Cognito'}. Error: ${error}}`);
}
})
) as Observable<V>
}
}
Log output
// Put your logs below this line
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 46 (16 by maintainers)
Hi @mattiLeBlanc! Appreciate your patience on this. We’ve just released a tagged release that should mitigate some of the challenges you’re encountering here. You can install this tagged version with
Please keep in mind the implementation is experimental and not for production, but let us know if this improves the situation on your end! We will keep testing as well to ensure everything is stable prior to a formal release.
@cwomack hi, yes, so far so good. These token renewal issue on the cusp of expiry seem to have been resolved. Please thank the team for their effort!
Hey @mattiLeBlanc, thanks for your patience! This has been merged to main and released to the
unstabletag. We’ll be prepping a formal release for later this week or early next week.@jjarvisp I am testing the release you mentioned now. I did 2 rounds of testing, the token got renewed fine. Since I am currently in Australia (and not Bali where I reported the issue) I am making sure I can reproduce the error in 6.9.0:
I can!
Now I testing again with the new experimental version.

I haven’t been able to reproduce with the new version.
Hi @mattiLeBlanc yes we are looking into this. Thank your for your patience.
Hi @thedevopsguyblog sorry for about the delayed response. Could you provide a minimal next.js app that can reproduce the issue you encountered?
Hi @mattiLeBlanc 👋 thanks for opening this issue. At the moment your workaround of checking and force refreshing the tokens makes sense but there might be a race condition because even though you provide a token the graphql client internally performs its own fetchAuthSession call so we are discussing this internally as something that could use improvement.
That being said, I’ve marked this as a bug for us to track