Skip to content

Commit

Permalink
Merge pull request #886 from aws-amplify/palpatim.chore.prep-release
Browse files Browse the repository at this point in the history
chore: prep release
  • Loading branch information
palpatim authored Sep 26, 2024
2 parents 359533d + 703307a commit bc1cefb
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 28 deletions.
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ List any codegen parameters changed or added.

- [ ] PR description included
- [ ] `yarn test` passes
- [ ] E2E test run linked
- [ ] Tests are [changed or added](https://github.com/aws-amplify/amplify-codegen/blob/main/CONTRIBUTING.md#tests)
- [ ] Relevant documentation is changed or added (and PR referenced)
- [ ] Breaking changes to existing customers are released behind a feature flag or major version update
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@
"parse-url": "^8.1.0",
"graphql": "15.8.0",
"xml2js": "0.5.0",
"axios": "^1.7.4"
"axios": "^1.7.4",
"**/@aws-amplify/amplify-codegen-e2e-tests/**/fast-xml-parser": "^4.4.1"
},
"config": {
"commitizen": {
Expand Down
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]) => {
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
21 changes: 7 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9995,9 +9995,9 @@ dotenv@~10.0.0:
integrity sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==

dset@^3.1.2:
version "3.1.3"
resolved "https://registry.npmjs.org/dset/-/dset-3.1.3.tgz#c194147f159841148e8e34ca41f638556d9542d2"
integrity sha512-20TuZZHCEZ2O71q9/+8BwKwZ0QtD9D8ObhrihJPr+vLLYlSuAU3/zL4cSlgbfeoGHTjCSJBa7NGcrF9/Bx/WJQ==
version "3.1.4"
resolved "https://registry.yarnpkg.com/dset/-/dset-3.1.4.tgz#f8eaf5f023f068a036d08cd07dc9ffb7d0065248"
integrity sha512-2QF/g9/zTaPDc3BjNcVTGoBbXBgYfMTTceLaYcFJ/W9kggFUkhxD/hMEeuLKbugyef9SqAx8cpgwlIP/jinUTA==

duplexer@^0.1.1:
version "0.1.2"
Expand Down Expand Up @@ -10666,17 +10666,10 @@ fast-url-parser@^1.1.3:
dependencies:
punycode "^1.3.2"

[email protected]:
version "4.2.5"
resolved "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-4.2.5.tgz#a6747a09296a6cb34f2ae634019bf1738f3b421f"
integrity sha512-B9/wizE4WngqQftFPmdaMYlXoJlJOYxGQOanC77fq9k8+Z0v5dDSVh+3glErdIROP//s/jgb7ZuxKfB8nVyo0g==
dependencies:
strnum "^1.0.5"

fast-xml-parser@^4.2.5:
version "4.3.2"
resolved "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-4.3.2.tgz#761e641260706d6e13251c4ef8e3f5694d4b0d79"
integrity sha512-rmrXUXwbJedoXkStenj1kkljNF7ugn5ZjR9FJcwmCfcCbtOMDghPajbc+Tck6vE6F5XsDmx+Pr2le9fw8+pXBg==
[email protected], fast-xml-parser@^4.2.5, fast-xml-parser@^4.4.1:
version "4.5.0"
resolved "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-4.5.0.tgz#2882b7d01a6825dfdf909638f2de0256351def37"
integrity sha512-/PlTQCI96+fZMAOLMZK4CWG1ItCbfZ/0jx7UIJFChPNrx7tcEgerUgWbeieCM9MfHInUDyK8DWYZ+YrywDJuTg==
dependencies:
strnum "^1.0.5"

Expand Down

0 comments on commit bc1cefb

Please sign in to comment.