Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: overriden mock resource outputs are respected in amplifyconfiguration #13274

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions packages/amplify-frontend-android/lib/amplify-config-helper.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function generateConfig(context, newAWSConfig) {
function generateConfig(context, newAWSConfig, amplifyResources) {
const metadata = context.amplify.getProjectMeta();
const amplifyConfig = {
UserAgent: 'aws-amplify-cli/2.0',
Version: '1.0',
};
constructAnalytics(metadata, amplifyConfig);
constructNotifications(metadata, amplifyConfig);
constructApi(metadata, amplifyConfig);
constructApi(metadata, amplifyConfig, amplifyResources);
// Auth plugin with entire awsconfiguration contained required for Native GA release
constructAuth(metadata, amplifyConfig, newAWSConfig);
constructPredictions(metadata, amplifyConfig);
Expand Down Expand Up @@ -85,7 +85,7 @@ function constructAnalytics(metadata, amplifyConfig) {
}
}

function constructApi(metadata, amplifyConfig) {
function constructApi(metadata, amplifyConfig, amplifyResources) {
const categoryName = 'api';
const pluginName = 'awsAPIPlugin';
const region = metadata.providers.awscloudformation.Region;
Expand All @@ -104,13 +104,17 @@ function constructApi(metadata, amplifyConfig) {
} else if (resourceMeta.output.securityType) {
authorizationType = resourceMeta.output.securityType;
}
const apiKey = getAppSyncResourceOutput(amplifyResources, 'GraphQLAPIKeyOutput') || resourceMeta.output.GraphQLAPIKeyOutput;
amplifyConfig[categoryName].plugins[pluginName][r] = {
endpointType: 'GraphQL',
endpoint: resourceMeta.output.GraphQLAPIEndpointOutput,
endpoint:
getAppSyncResourceOutput(amplifyResources, 'GraphQLAPIEndpointOutput') || resourceMeta.output.GraphQLAPIEndpointOutput,
region,
authorizationType,
apiKey: resourceMeta.output.GraphQLAPIKeyOutput,
};
if (apiKey) {
amplifyConfig[categoryName].plugins[pluginName][r]['apiKey'] = apiKey;
}
} else if (resourceMeta.service === 'API Gateway') {
amplifyConfig[categoryName].plugins[pluginName][r] = {
endpointType: 'REST',
Expand Down Expand Up @@ -282,6 +286,13 @@ function constructGeo(metadata, amplifyConfig) {
}
}

function getAppSyncResourceOutput(amplifyResources, outputName) {
const appSyncResourceMapping = amplifyResources?.serviceResourceMapping?.AppSync;
if (appSyncResourceMapping && appSyncResourceMapping[0]) {
return appSyncResourceMapping[0]?.output?.[outputName];
}
}

module.exports = {
generateConfig,
constructNotifications,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function writeToFile(filePath, fileName, configObject) {

function getAmplifyConfig(context, amplifyResources, cloudAmplifyResources) {
const newAWSConfig = getNewAWSConfigObject(context, amplifyResources, cloudAmplifyResources);
return amplifyConfigHelper.generateConfig(context, newAWSConfig);
return amplifyConfigHelper.generateConfig(context, newAWSConfig, amplifyResources);
}

function getNewAWSConfigObject(context, amplifyResources, cloudAmplifyResources) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const configHelper = require('../../amplify-frontend-android/lib/amplify-config-helper');
const configHelper = require('../lib/amplify-config-helper');
jest.mock('@aws-amplify/amplify-cli-core');

const mapServiceName = 'Map';
Expand Down Expand Up @@ -126,3 +126,113 @@ describe('customer pinpoint configuration', () => {
expect(amplifyConfiguration).toMatchObject(expectedAmplifyConfiguration);
});
});

describe('AppSync configuration', () => {
const mockContext = {
amplify: {
getProjectMeta: jest.fn(),
},
};
let amplifyMeta = {};
const expectedAmplifyConfiguration = {
UserAgent: 'aws-amplify-cli/2.0',
Version: '1.0',
api: {
plugins: {
awsAPIPlugin: {
testapi: {
apiKey: 'expectedApiKey',
authorizationType: undefined,
endpoint: 'expectedEndpoint',
endpointType: 'GraphQL',
region: 'us-east-1',
},
},
},
},
};

beforeEach(() => {
jest.clearAllMocks();
amplifyMeta = {
providers: {
awscloudformation: {
Region: 'us-east-1',
},
},
api: {
testapi: {
service: 'AppSync',
output: {
GraphQLAPIEndpointOutput: 'expectedEndpoint',
GraphQLAPIKeyOutput: 'expectedApiKey',
},
},
},
};
});
it('generates correct endpoint and apiKey based on outputs in Amplify meta', () => {
mockContext.amplify.getProjectMeta = jest.fn().mockReturnValue(amplifyMeta);
const amplifyConfiguration = configHelper.generateConfig(mockContext, {}, {});

const expectedAmplifyConfiguration = {
UserAgent: 'aws-amplify-cli/2.0',
Version: '1.0',
api: {
plugins: {
awsAPIPlugin: {
testapi: {
apiKey: 'expectedApiKey',
authorizationType: undefined,
endpoint: 'expectedEndpoint',
endpointType: 'GraphQL',
region: 'us-east-1',
},
},
},
},
};
expect(amplifyConfiguration).toMatchObject(expectedAmplifyConfiguration);
});

it('generates correct endpoint and apiKey based on overriden resource outputs', () => {
amplifyMeta.api.testapi.output.GraphQLAPIEndpointOutput = 'notExpectedEndpoint';
amplifyMeta.api.testapi.output.GraphQLAPIKeyOutput = 'notExpectedEndpoint';
mockContext.amplify.getProjectMeta = jest.fn().mockReturnValue(amplifyMeta);
const amplifyResources = {
serviceResourceMapping: {
AppSync: [
{
output: {
GraphQLAPIEndpointOutput: 'expectedEndpoint',
GraphQLAPIKeyOutput: 'expectedApiKey',
},
},
],
},
};
const amplifyConfiguration = configHelper.generateConfig(mockContext, {}, amplifyResources);
expect(amplifyConfiguration).toMatchObject(expectedAmplifyConfiguration);
});

it('does not add apiKey if its not available', () => {
amplifyMeta.api.testapi.output.GraphQLAPIEndpointOutput = 'notExpectedEndpoint';
delete amplifyMeta.api.testapi.output.GraphQLAPIKeyOutput;
mockContext.amplify.getProjectMeta = jest.fn().mockReturnValue(amplifyMeta);
const amplifyResources = {
serviceResourceMapping: {
AppSync: [
{
output: {
GraphQLAPIEndpointOutput: 'expectedEndpoint',
},
},
],
},
};
const expected = { ...expectedAmplifyConfiguration };
delete expected.api.plugins.awsAPIPlugin.testapi.apiKey;
const amplifyConfiguration = configHelper.generateConfig(mockContext, {}, amplifyResources);
expect(amplifyConfiguration).toMatchObject(expected);
});
});
21 changes: 16 additions & 5 deletions packages/amplify-frontend-ios/lib/amplify-config-helper.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function generateConfig(context, newAWSConfig) {
function generateConfig(context, newAWSConfig, amplifyResources) {
const metadata = context.amplify.getProjectMeta();
const amplifyConfig = {
UserAgent: 'aws-amplify-cli/2.0',
Version: '1.0',
};
constructAnalytics(metadata, amplifyConfig);
constructNotifications(metadata, amplifyConfig);
constructApi(metadata, amplifyConfig);
constructApi(metadata, amplifyConfig, amplifyResources);
// Auth plugin with entire awsconfiguration contained required for Native GA release
constructAuth(metadata, amplifyConfig, newAWSConfig);
constructPredictions(metadata, amplifyConfig);
Expand Down Expand Up @@ -85,7 +85,7 @@ function constructAnalytics(metadata, amplifyConfig) {
}
}

function constructApi(metadata, amplifyConfig) {
function constructApi(metadata, amplifyConfig, amplifyResources) {
const categoryName = 'api';
const pluginName = 'awsAPIPlugin';
const region = metadata.providers.awscloudformation.Region;
Expand All @@ -104,13 +104,17 @@ function constructApi(metadata, amplifyConfig) {
} else if (resourceMeta.output.securityType) {
authorizationType = resourceMeta.output.securityType;
}
const apiKey = getAppSyncResourceOutput(amplifyResources, 'GraphQLAPIKeyOutput') || resourceMeta.output.GraphQLAPIKeyOutput;
amplifyConfig[categoryName].plugins[pluginName][r] = {
endpointType: 'GraphQL',
endpoint: resourceMeta.output.GraphQLAPIEndpointOutput,
endpoint:
getAppSyncResourceOutput(amplifyResources, 'GraphQLAPIEndpointOutput') || resourceMeta.output.GraphQLAPIEndpointOutput,
region,
authorizationType,
apiKey: resourceMeta.output.GraphQLAPIKeyOutput,
};
if (apiKey) {
amplifyConfig[categoryName].plugins[pluginName][r]['apiKey'] = apiKey;
}
} else if (resourceMeta.service === 'API Gateway') {
amplifyConfig[categoryName].plugins[pluginName][r] = {
endpointType: 'REST',
Expand Down Expand Up @@ -283,6 +287,13 @@ function constructGeo(metadata, amplifyConfig) {
}
}

function getAppSyncResourceOutput(amplifyResources, outputName) {
const appSyncResourceMapping = amplifyResources?.serviceResourceMapping?.AppSync;
if (appSyncResourceMapping && appSyncResourceMapping[0]) {
return appSyncResourceMapping[0]?.output?.[outputName];
}
}

module.exports = {
generateConfig,
};
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function writeToFile(filePath, fileName, configObject) {

function getAmplifyConfig(context, amplifyResources, cloudAmplifyResources) {
const newAWSConfig = getNewAWSConfigObject(context, amplifyResources, cloudAmplifyResources);
const amplifyConfig = amplifyConfigHelper.generateConfig(context, newAWSConfig);
const amplifyConfig = amplifyConfigHelper.generateConfig(context, newAWSConfig, amplifyResources);
return amplifyConfig;
}

Expand Down
Loading