You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
GraphQL API works when called from my localhost or Postman. However, when I deploy the website to production and make requests to the API, all requests fail with the following error:
GraphQLError: A network error has occurred.
at s (https://mipey.mx/_next/static/chunks/pages/_app-07b4d7124b689df8.js:3:12449)
at async https://mipey.mx/_next/static/chunks/pages/_app-07b4d7124b689df8.js:3:14606
at async https://mipey.mx/_next/static/chunks/pages/_app-07b4d7124b689df8.js:3:15357
at async nH (https://mipey.mx/_next/static/chunks/855-ce77091a8814e86c.js:10:2788)
at async s (https://mipey.mx/_next/static/chunks/855-ce77091a8814e86c.js:10:3299)
at async rR._graphql (https://mipey.mx/_next/static/chunks/855-ce77091a8814e86c.js:14:3320)
at async Object.getStoresByUser (https://mipey.mx/_next/static/chunks/pages/app-b5f7944cef2af94b.js:1:6061)
at async f (https://mipey.mx/_next/static/chunks/pages/app-b5f7944cef2af94b.js:1:3699)
Reading through #7025 I thought my problem might be in the Amplify configuration, but this seems to cause no issue:
I tried to check for any CORS requirements on AppSync or something that could be blocking my requests, but haven't found anything. My AppSync service is created through CDK and this is the code for the construct:
import*ascdkfrom'aws-cdk-lib';import*asappsyncfrom'aws-cdk-lib/aws-appsync';import*asdynamodbfrom'aws-cdk-lib/aws-dynamodb';import{Construct}from'constructs';import*asfsfrom'fs';import*aspathfrom'path';import{mergeFiles}from'../../../shared/src/lib/file-merge';interfaceTableInfo{tableName: string;table: dynamodb.Table;}exportinterfaceAppSyncConstructProps{environment: string;apiName: string;pathGraphQL: string;dynamoDBTables: TableInfo[];}exportclassAppSyncConstructextendsConstruct{publicreadonlyapi: appsync.GraphqlApi;constructor(scope: Construct,id: string,props: AppSyncConstructProps){super(scope,id);// Merge schemas to be used for the GraphQL API.constrootPath=path.resolve(__dirname,'..','..','..','graphql');constschemaString=mergeFiles({
rootPath,fileExtension: 'graphql'});// Save merged schema to a fileconstschemaPath=path.join(__dirname,'merged-schema.graphql');fs.writeFileSync(schemaPath,schemaString);this.api=newappsync.GraphqlApi(this,props.apiName,{name: props.apiName,definition: appsync.Definition.fromFile(schemaPath)});newcdk.CfnOutput(this,'GraphQLAPIURL',{value: this.api.graphqlUrl,});newcdk.CfnOutput(this,'GraphQLAPIKey',{value: this.api.apiKey||'',});// Create data sources using the table name from the TableInfo objectconstdataSources=props.dynamoDBTables.reduce((dataSourceMap,tableInfo)=>{dataSourceMap[tableInfo.tableName]=this.api.addDynamoDbDataSource(tableInfo.tableName,// Use table name as the logical IDtableInfo.table);returndataSourceMap;},{}as{[key: string]: appsync.DynamoDbDataSource});constnoneDataSource=this.api.addNoneDataSource('NoneDataSource');// Helper function to create AppsyncFunctionconstcreateAppsyncFunction=(name: string,dataSource: appsync.BaseDataSource,filePath: string)=>{returnnewappsync.AppsyncFunction(this,name,{name: `CdkFunction${name}`,api: this.api,dataSource: dataSource,code: appsync.Code.fromAsset(path.join(props.pathGraphQL,'functions',filePath)),runtime: appsync.FunctionRuntime.JS_1_0_0,});};// Helper function to create ResolverconstcreateResolver=(typeName: string,fieldName: string,filePath: string,functions: appsync.AppsyncFunction[])=>{returnnewappsync.Resolver(this,`PipelineResolver${fieldName}`,{api: this.api,typeName: typeName,fieldName: fieldName,code: appsync.Code.fromAsset(path.join(props.pathGraphQL,'resolvers',filePath)),runtime: appsync.FunctionRuntime.JS_1_0_0,pipelineConfig: functions,});};// Docs for AppSync resolvers with DynamoDB: https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-dynamodb.html// StoresconstgetStoresByUserFunction=createAppsyncFunction('GetStoresByUser',dataSources[`${props.environment}-Stores`],'read/storesByUser.js');createResolver('Query','getStoresByUser','pipelineResolver.js',[getStoresByUserFunction]);constgetStoreFunction=createAppsyncFunction('GetStore',dataSources[`${props.environment}-Stores`],'read/store.js');createResolver('Query','getStore','pipelineResolver.js',[getStoreFunction]);constcreateStoreFunction=createAppsyncFunction('CreateStore',dataSources[`${props.environment}-Stores`],'create/store.js');createResolver('Mutation','createStore','pipelineResolver.js',[createStoreFunction]);// UsersconstvalidateEmailFunction=createAppsyncFunction('ValidateEmail',noneDataSource,'create/signUp/validateEmail.js');constsaveUserFunction=createAppsyncFunction('SaveUser',dataSources[`${props.environment}-Users`],'create/signUp/saveUser.js');createResolver('Mutation','signUp','signUpResolver.js',[validateEmailFunction,saveUserFunction]);constgetUserFunction=createAppsyncFunction('GetUser',dataSources[`${props.environment}-Users`],'read/user.js');createResolver('Query','getUser','pipelineResolver.js',[getUserFunction]);}}
I don't know what might cause the issue, but I think it must be a simple config error. Any help is greatly appreciated.
Expected behavior
I was expecting my API request to work on the production website as well.
Reproduction steps
Build a AppSync service with the CDK command previously given.
Deploy a website and try to connect with the service.
Code Snippet
I have the following file from which I make all calls to AppSync GraphQL API.
import{generateClient}from'aws-amplify/api';import{getCurrentUser}from'aws-amplify/auth';// Initialize the API clientconstclient=generateClient();// Define types for your API responsesinterfaceCreateStoreInput{name: string;description?: string;}interfaceUserStores{limit?: number;nextToken?: string;}interfaceUser{userId: string;}// Define your GraphQL queries and mutationsconstQUERIES={GET_STORE_BY_USER: ` query GetStoresByUser($userId: ID!, $limit: Int, $nextToken: String) { getStoresByUser(userId: $userId, limit: $limit, nextToken: $nextToken) { nextToken scannedCount startedAt items { id name logo banner } } } `,};constMUTATIONS={CREATE_STORE: ` mutation CreateStore($input: CreateStoreInput!) { createStore(input: $input) { id name } } `,};// API functionsexportconstapi={getCurrentUser: async(): Promise<User>=>{try{returnawaitgetCurrentUser();}catch(error){console.error('Error getting current user:',error);throwerror;}},createStore: async(storeData: CreateStoreInput)=>{try{constcurrentUser=awaitapi.getCurrentUser();constresponse=awaitclient.graphql({query: MUTATIONS.CREATE_STORE,variables: {input: {userId: currentUser.userId,
...storeData}}});returnresponse;}catch(error){console.error('Error creating store:',error);throwerror;}},getStoresByUser: async(userStores: UserStores)=>{try{constcurrentUser=awaitapi.getCurrentUser();constresponse=awaitclient.graphql({query: QUERIES.GET_STORE_BY_USER,variables: {userId: currentUser.userId,
...userStores}});returnresponse;}catch(error){console.error('Error getting stores:',error);throwerror}}};
I want to clarify that this code works because when used from localhost requests work just fine.
Log output
// Put your logs below this line
aws-exports.js
No response
Manual configuration
Already pasted this code in the issue description.
Really dumb problem, the issue was in my Amplify Hosting. For some reason, changes in the environmental variables didn't propagate, and had to manually redeploy my website. Closing the issue.
Before opening, please confirm:
JavaScript Framework
Next.js
Amplify APIs
GraphQL API
Amplify Version
v6
Amplify Categories
api
Backend
CDK
Environment information
Describe the bug
GraphQL API works when called from my localhost or Postman. However, when I deploy the website to production and make requests to the API, all requests fail with the following error:
Reading through #7025 I thought my problem might be in the Amplify configuration, but this seems to cause no issue:
I tried to check for any CORS requirements on AppSync or something that could be blocking my requests, but haven't found anything. My AppSync service is created through CDK and this is the code for the construct:
I don't know what might cause the issue, but I think it must be a simple config error. Any help is greatly appreciated.
Expected behavior
I was expecting my API request to work on the production website as well.
Reproduction steps
Code Snippet
I have the following file from which I make all calls to AppSync GraphQL API.
I want to clarify that this code works because when used from localhost requests work just fine.
Log output
aws-exports.js
No response
Manual configuration
Already pasted this code in the issue description.
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
The text was updated successfully, but these errors were encountered: