aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.12k forks source link

Error: No credentials at GraphQLAPIClass.<anonymous> (GraphQLAPI.ts:154:1) #13699

Closed ashwinikumar07 closed 1 month ago

ashwinikumar07 commented 1 month ago

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

GraphQL API

Amplify Version

Older than v5

Amplify Categories

api

Backend

CDK

Environment information

``` # Put output below this line System: OS: Windows 11 10.0.22631 CPU: (16) x64 11th Gen Intel(R) Core(TM) i9-11950H @ 2.60GHz Memory: 5.84 GB / 31.73 GB Binaries: Node: 20.15.1 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD npm: 10.7.0 - C:\Program Files\nodejs\npm.CMD Browsers: Edge: Chromium (127.0.2651.74) Internet Explorer: 11.0.22621.3527 npmPackages: @aws-sdk/protocol-http: ^3.374.0 => 3.374.0 (3.6.1, 3.186.0) @aws-sdk/signature-v4: ^3.374.0 => 3.374.0 (3.186.0, 3.6.1) @testing-library/jest-dom: ^5.17.0 => 5.17.0 @testing-library/react: ^13.4.0 => 13.4.0 @testing-library/user-event: ^13.5.0 => 13.5.0 aws-amplify: ^4.3.46 => 4.3.46 node-fetch: ^3.3.2 => 3.3.2 (2.7.0) react: ^18.3.1 => 18.3.1 (18.2.0) react-dom: ^18.3.1 => 18.3.1 react-scripts: 5.0.1 => 5.0.1 web-vitals: ^2.1.4 => 2.1.4 npmGlobalPackages: @aws-amplify/cli: 12.12.4 create-react-app: 5.0.1 nodemon: 3.1.4 yarn: 1.22.10 ```

Describe the bug

image

I am using Api key as an auth to connect to appsync.

aws-amplify version 4.3.46 (I can't use the higher version above this due to some restriction)

Expected behavior

Graphql call should return the data successfully.

Reproduction steps

Install aws-amplify@4.3.46 Make the graphql query. (See the code Snippet) See the exception in console

Code Snippet

// Put your code below this line.
const fetchData = async () => {
    try {
        const result = await API.graphql(graphqlOperation(getData));
        console.log(result);

        return result.data.getData;

    } catch (error) {
      console.error('Error fetching votes:', error);
    }
  };

Log output

``` // Put your logs below this line ```

aws-exports.js

// WARNING: DO NOT EDIT. This file is automatically generated by AWS Amplify. It will be overwritten.

const config = { API: { GraphQL: { endpoint: 'graphql_endpoint', region: 'us-east-1', defaultAuthMode: 'apiKey', apiKey: 'da2-***' } }, };

export default config;

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

chrisbonifacio commented 1 month ago

Hi @ashwinikumar07 It looks like you have API Key as the default auth mode on your graphql client. Was the api key working before and only recently you started seeing this error?

This error, from what I recall, can occur if Amplify is trying to provide credentials to the request such as IAM signing or adding a logged in user's access token to the request headers. Is there a network request that is being sent out at all?

Lastly, could you share your schema so that we can try to identify any potential causes there?

ashwinikumar07 commented 1 month ago

The issue is resolved. The fix is that I have updated the aws-exports.js file as below:

const config = { aws_appsync_graphqlEndpoint: 'https://XXXXXXXXXXXX.appsync-api.us-east-1.amazonaws.com/graphql', aws_appsync_region: 'us-east-1', aws_appsync_authenticationType: 'API_KEY', aws_appsync_apiKey: 'da2-**', };

export default config;