Skip to content

Commit 4148c5d

Browse files
committed
refactor: centralize DynamoDB GSI accessor into a shared helper
Dedupe the aws-cdk-lib 2.252/2.260 _globalSecondaryIndexes fallback across 3 packages; no behavior change [follow-up to #3505] Extracts the dual-path accessor `table['_globalSecondaryIndexes'] ?? table.globalSecondaryIndexes` into a single exported helper `getGlobalSecondaryIndexes` in @aws-amplify/graphql-transformer-core and points all three call sites at it (relational-transformer resolvers, transformer-core schema-utils getKeySchema, conversation-transformer resolver generator). Identical dual-path semantics preserved.
1 parent ebaf1e0 commit 4148c5d

6 files changed

Lines changed: 29 additions & 24 deletions

File tree

packages/amplify-graphql-conversation-transformer/src/transformer-steps/conversation-resolver-generator.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { conversation } from '@aws-amplify/ai-constructs';
22
import { overrideIndexAtCfnLevel } from '@aws-amplify/graphql-index-transformer';
3-
import { getModelDataSourceNameForTypeName, getTable, TransformerResolver } from '@aws-amplify/graphql-transformer-core';
3+
import {
4+
getModelDataSourceNameForTypeName,
5+
getGlobalSecondaryIndexes,
6+
getTable,
7+
TransformerResolver,
8+
} from '@aws-amplify/graphql-transformer-core';
49
import { DataSourceProvider, TransformerContextProvider } from '@aws-amplify/graphql-transformer-interfaces';
510
import { BackendOutputEntry, BackendOutputStorageStrategy } from '@aws-amplify/plugin-types';
611
import * as cdk from 'aws-cdk-lib';
@@ -331,10 +336,7 @@ export class ConversationResolverGenerator {
331336
writeCapacity: cdk.Fn.ref(ResourceConstants.PARAMETERS.DynamoDBModelTableWriteIOPS),
332337
});
333338

334-
// aws-cdk-lib 2.260 renamed the private `globalSecondaryIndexes` array on the DynamoDB L2 `Table` to
335-
// `_globalSecondaryIndexes` (an `ArrayBox` exposing the same `find` surface and `{ indexName, keySchema }`
336-
// element shape); Amplify's managed-table construct keeps the public `globalSecondaryIndexes` array.
337-
const globalSecondaryIndexes = table['_globalSecondaryIndexes'] ?? table.globalSecondaryIndexes;
339+
const globalSecondaryIndexes = getGlobalSecondaryIndexes(table);
338340
const gsi = globalSecondaryIndexes.find((g: any) => g.indexName === indexName);
339341

340342
const newIndex = {

packages/amplify-graphql-relational-transformer/src/resolvers.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { attributeTypeFromType, overrideIndexAtCfnLevel } from '@aws-amplify/graphql-index-transformer';
22
import { generateApplyDefaultsToInputTemplate } from '@aws-amplify/graphql-model-transformer';
3-
import { InvalidDirectiveError, MappingTemplate, getTable } from '@aws-amplify/graphql-transformer-core';
3+
import { InvalidDirectiveError, MappingTemplate, getGlobalSecondaryIndexes, getTable } from '@aws-amplify/graphql-transformer-core';
44
import {
55
TransformerContextProvider,
66
TransformerPrepareStepContextProvider,
@@ -29,18 +29,6 @@ import { getSortKeyFields } from './schema';
2929
import { HasManyDirectiveConfiguration, HasOneDirectiveConfiguration } from './types';
3030
import { getConnectionAttributeName, getObjectPrimaryKey } from './utils';
3131

32-
/**
33-
* Reads the list of global secondary indexes tracked on a DynamoDB L2 `Table` construct.
34-
*
35-
* `aws-cdk-lib` 2.260 renamed the (private) `globalSecondaryIndexes` array to `_globalSecondaryIndexes`
36-
* and now backs it with an `ArrayBox`, which still exposes the array-like `some`/`find`/`length` surface
37-
* and the same element shape (`{ indexName, keySchema }`). Amplify's own managed-table construct
38-
* (`AmplifyDynamoDBTable`) instead keeps its indexes on a public `globalSecondaryIndexes` array. Reading
39-
* `_globalSecondaryIndexes` first and falling back to `globalSecondaryIndexes` works for both table types
40-
* and preserves the duplicate-index-name detection rather than silently disabling it.
41-
*/
42-
const getGlobalSecondaryIndexes = (table: any): any => table['_globalSecondaryIndexes'] ?? table.globalSecondaryIndexes;
43-
4432
/**
4533
* Creates a GSI on the table of the `relatedType` based on the config's `references` / `referenceNodes`
4634
*

packages/amplify-graphql-transformer-core/API.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ export const getFieldNameFor: (op: Operation, typeName: string) => string;
250250
// @public (undocumented)
251251
export const getFilterInputName: (modelName: string) => string;
252252

253+
// @public (undocumented)
254+
export const getGlobalSecondaryIndexes: (table: any) => any;
255+
253256
// @public (undocumented)
254257
export const getImportedRDSTypeFromStrategyDbType: (dbType: ModelDataSourceStrategyDbType) => ImportedRDSType;
255258

packages/amplify-graphql-transformer-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export {
3939
getDefaultStrategyNameForDbType,
4040
getField,
4141
getFilterInputName,
42+
getGlobalSecondaryIndexes,
4243
getImportedRDSTypeFromStrategyDbType,
4344
getKeySchema,
4445
getModelDataSourceNameForTypeName,

packages/amplify-graphql-transformer-core/src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export {
99
export { DirectiveWrapper, GetArgumentsOptions, generateGetArgumentsInput } from './directive-wrapper';
1010
export { collectDirectives, collectDirectivesByTypeNames } from './type-map-utils';
1111
export { stripDirectives } from './strip-directives';
12-
export { getTable, getKeySchema, getSortKeyFieldNames, getStrategyDbTypeFromTypeNode } from './schema-utils';
12+
export { getTable, getKeySchema, getGlobalSecondaryIndexes, getSortKeyFieldNames, getStrategyDbTypeFromTypeNode } from './schema-utils';
1313
export { DEFAULT_SCHEMA_DEFINITION } from './defaultSchema';
1414
export {
1515
constructArrayFieldsStatement,

packages/amplify-graphql-transformer-core/src/utils/schema-utils.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,26 @@ import { ListValueNode, ObjectTypeDefinitionNode, StringValueNode, TypeNode } fr
66
import { ModelResourceIDs, getBaseType } from 'graphql-transformer-common';
77
import { getModelDataSourceStrategy } from './model-datasource-strategy-utils';
88

9+
/**
10+
* Reads the list of global secondary indexes tracked on a DynamoDB L2 `Table` construct.
11+
*
12+
* aws-cdk-lib 2.260 renamed the private `globalSecondaryIndexes` array on the DynamoDB L2 `Table` to
13+
* `_globalSecondaryIndexes` (now an `ArrayBox` exposing the same `find`/`some`/`length` surface and the
14+
* same `{ indexName, keySchema }` element shape); Amplify's managed-table construct keeps the public
15+
* `globalSecondaryIndexes` array. This reads whichever exists (covers standard `Table` + Amplify managed
16+
* table). Centralized so a future CDK rename is a one-line fix.
17+
*/
18+
export const getGlobalSecondaryIndexes = (table: any): any => table['_globalSecondaryIndexes'] ?? table.globalSecondaryIndexes;
19+
920
/**
1021
* getKeySchema
1122
*/
1223
export const getKeySchema = (table: any, indexName?: string): any => {
13-
// aws-cdk-lib 2.260 renamed the private `globalSecondaryIndexes`/`localSecondaryIndexes` arrays on the
14-
// DynamoDB L2 `Table` to `_globalSecondaryIndexes`/`_localSecondaryIndexes` (now `ArrayBox`es exposing the
15-
// same `find` surface and element shape). Amplify's managed-table construct keeps the public array names.
16-
// Read the renamed fields first, falling back to the public ones so both table types resolve correctly.
17-
const globalSecondaryIndexes = table['_globalSecondaryIndexes'] ?? table.globalSecondaryIndexes;
24+
// aws-cdk-lib 2.260 renamed the private `localSecondaryIndexes` array on the DynamoDB L2 `Table` to
25+
// `_localSecondaryIndexes` (now an `ArrayBox` exposing the same `find` surface and element shape).
26+
// Amplify's managed-table construct keeps the public array name. Read the renamed field first, falling
27+
// back to the public one so both table types resolve correctly.
28+
const globalSecondaryIndexes = getGlobalSecondaryIndexes(table);
1829
const localSecondaryIndexes = table['_localSecondaryIndexes'] ?? table.localSecondaryIndexes;
1930
return (
2031
(

0 commit comments

Comments
 (0)