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(api-graphql): incorrect list sk arg type #13249

Merged
merged 3 commits into from Apr 15, 2024
Merged

fix(api-graphql): incorrect list sk arg type #13249

merged 3 commits into from Apr 15, 2024

Conversation

iartemiev
Copy link
Contributor

@iartemiev iartemiev commented Apr 14, 2024

Description of changes

We were generating list query arguments for sort keys using the underlying field's primitive type as the argument type.

List queries on models with composite primary keys expect a condition input argument type for the sort key(s).

See this diff for current vs expected behavior: https://github.com/aws-amplify/amplify-js/compare/data-fix-sk-args?expand=1#diff-fa24101f776e07b54f8416746bbed348b96dd7e855c0eed2e5609bcb501a9b46L4095-R4095

Description of how you validated changes

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@iartemiev iartemiev requested a review from a team as a code owner April 14, 2024 16:54
@iartemiev iartemiev requested a review from a team as a code owner April 14, 2024 17:00
@@ -563,7 +563,7 @@ describe('generateGraphQLDocument()', () => {
modelOperation
);

expect(document.includes(expectedArgs)).toBe(true);
expect(document).toEqual(expect.stringContaining(expectedArgs))
Copy link
Contributor Author

@iartemiev iartemiev Apr 15, 2024

Choose a reason for hiding this comment

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

Same effect, but this outputs a helpful diff if the string doesn't match, whereas previously it would only tell us got false expected true.

@@ -4092,12 +4092,12 @@ exports[`generateClient basic model operations can list() with sortDirection (AS
"abortController": AbortController {},
"options": {
"body": {
"query": "query ($filter: ModelThingWithCustomPkFilterInput, $sortDirection: ModelSortDirection, $cpk_cluster_key: String, $cpk_sort_key: String, $limit: Int, $nextToken: String) {
"query": "query ($cpk_cluster_key: String, $cpk_sort_key: ModelStringKeyConditionInput, $sortDirection: ModelSortDirection, $filter: ModelThingWithCustomPkFilterInput, $limit: Int, $nextToken: String) {
Copy link
Contributor Author

@iartemiev iartemiev Apr 15, 2024

Choose a reason for hiding this comment

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

Model${fieldType}KeyConditionInput is the expected GraphQL type for SKs in list operations

Comment on lines +731 to +746
const generateSkArgs = (op: 'get' | 'list') => {
return sortKeyFieldNames.reduce(
(acc: Record<string, any>, fieldName: string) => {
const fieldType = fields[fieldName].type;

if (op === 'get') {
acc[fieldName] = `${fieldType}!`;
} else if (op === 'list') {
acc[fieldName] = `Model${fieldType}KeyConditionInput`;
}

return acc;
},
{},
);
};
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can use Object.fromEntries to save a few bytes :D (still over the limit a bit though...)

Suggested change
const generateSkArgs = (op: 'get' | 'list') => {
return sortKeyFieldNames.reduce(
(acc: Record<string, any>, fieldName: string) => {
const fieldType = fields[fieldName].type;
if (op === 'get') {
acc[fieldName] = `${fieldType}!`;
} else if (op === 'list') {
acc[fieldName] = `Model${fieldType}KeyConditionInput`;
}
return acc;
},
{},
);
};
const generateSkArgs = (op: 'get' | 'list') =>
Object.fromEntries(
sortKeyFieldNames.map(fieldName => {
const fieldType = fields[fieldName].type;
return [
fieldName,
op === 'get' ? `${fieldType}!` : `Model${fieldType}KeyConditionInput`,
];
}),
);

Copy link
Contributor Author

@iartemiev iartemiev Apr 15, 2024

Choose a reason for hiding this comment

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

I think the suggested change makes this harder to read/grok. I'd rather keep it very simple since we've had lots of bugs in this code already.

packages/api-graphql/src/internals/APIClient.ts Outdated Show resolved Hide resolved
packages/api-graphql/src/internals/APIClient.ts Outdated Show resolved Hide resolved
@HuiSF HuiSF changed the title fix: incorrect list sk arg type fix(api-graphql): incorrect list sk arg type Apr 15, 2024
Copy link
Contributor

@jimblanc jimblanc left a comment

Choose a reason for hiding this comment

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

Approving umbrella package.json changes

@HuiSF HuiSF merged commit f37faeb into main Apr 15, 2024
31 checks passed
@HuiSF HuiSF deleted the data-fix-sk-args branch April 15, 2024 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants