Skip to content

Commit

Permalink
fix(graphql-model-transformer): fix open search instance check for v1…
Browse files Browse the repository at this point in the history
… and v2 transformers (#8354)
  • Loading branch information
lazpavel authored Oct 5, 2021
1 parent c936570 commit d7d9476
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { stateManager } from 'amplify-cli-core';
import { printer } from 'amplify-prompts';
import { searchablePushChecks } from '../transform-graphql-schema';

jest.mock('amplify-cli-core');
jest.mock('amplify-prompts');

const printerMock = printer as jest.Mocked<typeof printer>;
const stateManagerMock = stateManager as jest.Mocked<typeof stateManager>;

describe('graphql schema checks', () => {
const contextMock = {
amplify: {
getEnvInfo: jest.fn(),
},
};

beforeEach(() => {
jest.clearAllMocks();
});

it('should warn users if they use not recommended open search instance without overrides', async () => {
printerMock.warn.mockImplementation(jest.fn());
stateManagerMock.getTeamProviderInfo.mockReturnValue({});
contextMock.amplify.getEnvInfo.mockReturnValue({ envName: 'test' });
const map = { Post: ['model', 'searchable'] };
searchablePushChecks(contextMock, map, 'test_api_name');
expect(printerMock.warn).lastCalledWith(
'Your instance type for OpenSearch is t2.small.elasticsearch, you may experience performance issues or data loss. Consider reconfiguring with the instructions here https://docs.amplify.aws/cli/graphql-transformer/searchable/',
);
});

it('should warn users if they use not recommended open search instance with overrides', async () => {
printerMock.warn.mockImplementation(jest.fn());
stateManagerMock.getTeamProviderInfo.mockReturnValue({
test: {
categories: {
api: {
test_api_name: {
ElasticSearchInstanceType: 't2.small.elasticsearch',
},
},
},
},
});
contextMock.amplify.getEnvInfo.mockReturnValue({ envName: 'test' });
const map = { Post: ['model', 'searchable'] };
searchablePushChecks(contextMock, map, 'test_api_name');
expect(printerMock.warn).lastCalledWith(
'Your instance type for OpenSearch is t2.small.elasticsearch, you may experience performance issues or data loss. Consider reconfiguring with the instructions here https://docs.amplify.aws/cli/graphql-transformer/searchable/',
);
});

it('should NOT warn users if they use recommended open search instance', async () => {
printerMock.warn.mockImplementation(jest.fn());
stateManagerMock.getTeamProviderInfo.mockReturnValue({
test: {
categories: {
api: {
test_api_name: {
ElasticSearchInstanceType: 't2.medium.elasticsearch',
},
},
},
},
});
contextMock.amplify.getEnvInfo.mockReturnValue({ envName: 'test' });
const map = { Post: ['model', 'searchable'] };
searchablePushChecks(contextMock, map, 'test_api_name');
expect(printerMock.warn).not.toBeCalled();
});

it('should NOT warn users if they use recommended open search instance on the environment', async () => {
printerMock.warn.mockImplementation(jest.fn());
stateManagerMock.getTeamProviderInfo.mockReturnValue({
dev: {
categories: {
api: {
test_api_name: {
ElasticSearchInstanceType: 't2.small.elasticsearch',
},
},
},
},
prod: {
categories: {
api: {
test_api_name: {
ElasticSearchInstanceType: 't2.medium.elasticsearch',
},
},
},
},
});
contextMock.amplify.getEnvInfo.mockReturnValue({ envName: 'prod' });
const map = { Post: ['model', 'searchable'] };
searchablePushChecks(contextMock, map, 'test_api_name');
expect(printerMock.warn).not.toBeCalled();
});

it('should NOT warn users if they do NOT use searchable', async () => {
printerMock.warn.mockImplementation(jest.fn());
stateManagerMock.getTeamProviderInfo.mockReturnValue({});
contextMock.amplify.getEnvInfo.mockReturnValue({ envName: 'test' });
const map = { Post: ['model'] };
searchablePushChecks(contextMock, map, 'test_api_name');
expect(printerMock.warn).not.toBeCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import { AppSyncAuthConfiguration } from '@aws-amplify/graphql-transformer-core'
import { Template } from '@aws-amplify/graphql-transformer-core/lib/config/project-config';
import { AmplifyCLIFeatureFlagAdapter } from '../utils/amplify-cli-feature-flag-adapter';
import { JSONUtilities } from 'amplify-cli-core';
import { searchablePushChecks } from '../transform-graphql-schema';
import { ResourceConstants } from 'graphql-transformer-common';

const API_CATEGORY = 'api';
const STORAGE_CATEGORY = 'storage';
Expand Down Expand Up @@ -275,6 +277,7 @@ export async function transformGraphQLSchema(context, options) {
// Check for common errors
const directiveMap = collectDirectivesByTypeNames(project.schema);
warnOnAuth(context, directiveMap.types);
searchablePushChecks(context, directiveMap.types, parameters[ResourceConstants.PARAMETERS.AppSyncApiName]);

const transformerListFactory = getTransformerFactory(context, resourceDir);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,19 @@ const schemaDirName = 'schema';
const ROOT_APPSYNC_S3_KEY = 'amplify-appsync-files';
const s3ServiceName = 'S3';

export function searchablePushChecks(context, map): void {
export function searchablePushChecks(context, map, apiName): void {
const searchableModelTypes = Object.keys(map).filter(type => map[type].includes('searchable') && map[type].includes('model'));
if (searchableModelTypes.length) {
const currEnv = context.amplify.getEnvInfo().envName;
const teamProviderInfo = stateManager.getTeamProviderInfo();
const apiCategory = teamProviderInfo[currEnv]?.categories?.api;
const instanceType = apiCategory ? apiCategory[ResourceConstants.PARAMETERS.ElasticsearchInstanceType] : null;
if (!instanceType || instanceType === 't2.small.elasticsearch') {
const apiCategory = teamProviderInfo[currEnv]?.categories?.api[apiName];
const instanceType =
apiCategory && apiCategory[ResourceConstants.PARAMETERS.ElasticsearchInstanceType]
? apiCategory[ResourceConstants.PARAMETERS.ElasticsearchInstanceType]
: 't2.small.elasticsearch';
if (instanceType === 't2.small.elasticsearch' || instanceType === 't3.small.elasticsearch') {
printer.warn(
'Your instance type for OpenSearch is t2.small, you may experience performance issues or data loss. Consider reconfiguring with the instructions here https://docs.amplify.aws/cli/graphql-transformer/searchable/',
`Your instance type for OpenSearch is ${instanceType}, you may experience performance issues or data loss. Consider reconfiguring with the instructions here https://docs.amplify.aws/cli/graphql-transformer/searchable/`,
);
}
}
Expand Down Expand Up @@ -481,7 +484,7 @@ export async function transformGraphQLSchema(context, options) {
// Check for common errors
const directiveMap = collectDirectivesByTypeNames(project.schema);
warnOnAuth(context, directiveMap.types);
searchablePushChecks(context, directiveMap.types);
searchablePushChecks(context, directiveMap.types, parameters[ResourceConstants.PARAMETERS.AppSyncApiName]);

await transformerVersionCheck(context, resourceDir, previouslyDeployedBackendDir, resourcesToBeUpdated, directiveMap.directives);

Expand Down

0 comments on commit d7d9476

Please sign in to comment.