-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Conversation
@@ -563,7 +563,7 @@ describe('generateGraphQLDocument()', () => { | |||
modelOperation | |||
); | |||
|
|||
expect(document.includes(expectedArgs)).toBe(true); | |||
expect(document).toEqual(expect.stringContaining(expectedArgs)) |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
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; | ||
}, | ||
{}, | ||
); | ||
}; |
There was a problem hiding this comment.
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...)
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`, | |
]; | |
}), | |
); |
There was a problem hiding this comment.
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.
There was a problem hiding this 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
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
yarn test
passesBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.