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 * as cdk from 'aws-cdk-lib';
import * as appsync from 'aws-cdk-lib/aws-appsync';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import { Construct } from 'constructs';
import * as fs from 'fs';
import * as path from 'path';
import { mergeFiles } from '../../../shared/src/lib/file-merge';
interface TableInfo {
tableName: string;
table: dynamodb.Table;
}
export interface AppSyncConstructProps {
environment: string;
apiName: string;
pathGraphQL: string;
dynamoDBTables: TableInfo[];
}
export class AppSyncConstruct extends Construct {
public readonly api: appsync.GraphqlApi;
constructor(scope: Construct, id: string, props: AppSyncConstructProps) {
super(scope, id);
// Merge schemas to be used for the GraphQL API.
const rootPath = path.resolve(__dirname, '..', '..', '..', 'graphql');
const schemaString = mergeFiles({
rootPath,
fileExtension: 'graphql'
});
// Save merged schema to a file
const schemaPath = path.join(__dirname, 'merged-schema.graphql');
fs.writeFileSync(schemaPath, schemaString);
this.api = new appsync.GraphqlApi(this, props.apiName, {
name: props.apiName,
definition: appsync.Definition.fromFile(schemaPath)
});
new cdk.CfnOutput(this, 'GraphQLAPIURL', {
value: this.api.graphqlUrl,
});
new cdk.CfnOutput(this, 'GraphQLAPIKey', {
value: this.api.apiKey || '',
});
// Create data sources using the table name from the TableInfo object
const dataSources = props.dynamoDBTables.reduce((dataSourceMap, tableInfo) => {
dataSourceMap[tableInfo.tableName] = this.api.addDynamoDbDataSource(
tableInfo.tableName, // Use table name as the logical ID
tableInfo.table
);
return dataSourceMap;
}, {} as { [key: string]: appsync.DynamoDbDataSource });
const noneDataSource = this.api.addNoneDataSource('NoneDataSource');
// Helper function to create AppsyncFunction
const createAppsyncFunction = (name: string, dataSource: appsync.BaseDataSource, filePath: string) => {
return new appsync.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 Resolver
const createResolver = (typeName: string, fieldName: string, filePath: string, functions: appsync.AppsyncFunction[]) => {
return new appsync.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
// Stores
const getStoresByUserFunction = createAppsyncFunction('GetStoresByUser', dataSources[`${props.environment}-Stores`], 'read/storesByUser.js');
createResolver('Query', 'getStoresByUser', 'pipelineResolver.js', [getStoresByUserFunction]);
const getStoreFunction = createAppsyncFunction('GetStore', dataSources[`${props.environment}-Stores`], 'read/store.js');
createResolver('Query', 'getStore', 'pipelineResolver.js', [getStoreFunction]);
const createStoreFunction = createAppsyncFunction('CreateStore', dataSources[`${props.environment}-Stores`], 'create/store.js');
createResolver('Mutation', 'createStore', 'pipelineResolver.js', [createStoreFunction]);
// Users
const validateEmailFunction = createAppsyncFunction('ValidateEmail', noneDataSource, 'create/signUp/validateEmail.js');
const saveUserFunction = createAppsyncFunction('SaveUser', dataSources[`${props.environment}-Users`], 'create/signUp/saveUser.js');
createResolver('Mutation', 'signUp', 'signUpResolver.js', [validateEmailFunction, saveUserFunction]);
const getUserFunction = 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.
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