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(amplify-codegen): support multiple indexes on the same field in introspection schema #879

Merged
merged 1 commit into from
Sep 20, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,78 @@ describe('processIndex', () => {
]);
});

it('support multiple @index directives on a field', () => {
const model: CodeGenModel = {
directives: [
{
name: 'model',
arguments: {},
},
],
name: 'testModel',
type: 'model',
fields: [
{
type: 'field',
isList: false,
isNullable: true,
name: 'connectionField',
directives: [
{
name: 'index',
arguments: {
name: 'byItemAndSortField',
sortKeyFields: ['sortField'],
},
},
{
name: 'index',
arguments: {
name: 'byItemAndAnotherSortField',
sortKeyFields: ['anotherSortField'],
},
},
{
name: 'index',
arguments: {
name: 'byItemAndSomeOtherSortField',
sortKeyFields: ['someOtherSortField'],
},
},
],
},
],
};
processIndex(model);
expect(model.directives).toEqual([
{
name: 'model',
arguments: {},
},
{
name: 'key',
arguments: {
name: 'byItemAndSortField',
fields: ['connectionField', 'sortField'],
},
},
{
name: 'key',
arguments: {
name: 'byItemAndAnotherSortField',
fields: ['connectionField', 'anotherSortField'],
},
},
{
name: 'key',
arguments: {
name: 'byItemAndSomeOtherSortField',
fields: ['connectionField', 'someOtherSortField'],
},
},
]);
});

it('adds simple @index directives as model key attributes', () => {
const model: CodeGenModel = {
directives: [
Expand Down
3 changes: 3 additions & 0 deletions packages/appsync-modelgen-plugin/src/utils/fieldUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export function removeFieldFromModel(model: CodeGenModel, fieldName: string): vo
export const getDirective = (fieldOrModel: CodeGenField | CodeGenModel) => (directiveName: string): CodeGenDirective | undefined =>
fieldOrModel.directives.find(d => d.name === directiveName);

export const getDirectives = (fieldOrModel: CodeGenField | CodeGenModel) => (directiveName: string): CodeGenDirective[] | undefined =>
fieldOrModel.directives.filter(d => d.name === directiveName);

// Function matching to GraphQL transformer so that the auto-generated field
export function toCamelCase(words: string[]): string {
const formatted = words.map((w, i) => (i === 0 ? w.charAt(0).toLowerCase() + w.slice(1) : w.charAt(0).toUpperCase() + w.slice(1)));
Expand Down
32 changes: 19 additions & 13 deletions packages/appsync-modelgen-plugin/src/utils/process-index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CodeGenDirective, CodeGenModel } from '../visitors/appsync-visitor';
import { getDirective } from './fieldUtils';
import { getDirectives } from './fieldUtils';
import pluralize from 'pluralize';
import { toLower, toUpper } from './stringUtils';

Expand All @@ -9,21 +9,27 @@ import { toLower, toUpper } from './stringUtils';
*/
export const processIndex = (model: CodeGenModel) => {
const indexMap = model.fields.reduce((acc, field) => {
const indexDirective = getDirective(field)('index');
if (!indexDirective) {
const indexDirectives = getDirectives(field)('index');
if (!indexDirectives) {
return acc;
}
return { ...acc, [field.name]: indexDirective };
}, {} as Record<string, CodeGenDirective>);
return { ...acc, [field.name]: indexDirectives };
}, {} as Record<string, CodeGenDirective[]>);

const keyList: CodeGenDirective[] = [];
Object.entries(indexMap).forEach(([fieldName, directives]) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Why change this from a map to a forEach?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.map returns a new array, whereas .forEach doesn't return anything. Since the refactored code doesn't rely on the returned array, .forEach is a better fit here.

directives.forEach(directive => {
keyList.push({
name: 'key',
arguments: {
name: directive.arguments.name ?? generateDefaultIndexName(model.name, [fieldName].concat((directive.arguments.sortKeyFields as string[]) ?? [])),
queryField: directive.arguments.queryField,
fields: [fieldName].concat((directive.arguments.sortKeyFields as string[]) ?? []),
},
});
});
});

const keyList: CodeGenDirective[] = Object.entries(indexMap).map(([fieldName, directive]) => ({
name: 'key',
arguments: {
name: directive.arguments.name ?? generateDefaultIndexName(model.name, [fieldName].concat((directive.arguments.sortKeyFields as string[]) ?? [])),
queryField: directive.arguments.queryField,
fields: [fieldName].concat((directive.arguments.sortKeyFields as string[]) ?? []),
},
}));
const existingIndexNames = model.directives
.filter(directive => directive.name === 'key' && !!directive.arguments.name)
.map(directive => directive.arguments.name);
Expand Down
Loading