-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add configuration option for batch keys that are a Set rather than $ReadOnlyArray #265
Conversation
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.
testing in swapi looks good!
schema.json
Outdated
@@ -93,6 +93,10 @@ | |||
"isResponseDictionary": { | |||
"type": "boolean", | |||
"description": "(Optional) Set to true if the batch resource returns the results as a dictionary with key mapped to values (instead of a list of items). If this option is supplied `reorderResultsByKey` should not be. Default: false" | |||
}, | |||
"uniqueBatchKeys": { |
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.
uniqueBatchKeys
confused me a little bit. (uniqueBatchKeys
makes me feel I should input what's the unique batch keys, not whether batchkey is unique.) would something like isBatchKeyASet
be better?
and I like the description of commaSeparatedBatchKey
a lot, so maybe we could follow that and do
"description": "(Optional) Set to true if the interface of the resource takes the batch key as a set (rather than an array as is more common). Default: false"
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.
Great suggestion. I was really struggling with what to call this option haha. I like yours better for sure.
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.
LGTM, cc @ATRAN2
src/genTypeFlow.ts
Outdated
let newKeyType = `${resourceConfig.newKey}: $ElementType<$PropertyType<${resourceArgs}, '${resourceConfig.batchKey}'>, 0>`; | ||
|
||
if (resourceConfig.isBatchKeyASet) { | ||
newKeyType = `${resourceConfig.newKey}: ${getNewKeyTypeFromBatchKeySetType( | ||
resourceConfig.batchKey, | ||
resourceArgs, | ||
)}`; | ||
} |
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.
may be worth a comment explaining "batchKey could be part of an array or a set" or something to explain what's going on here in the default / branching case?
return `\ | ||
$Call< | ||
ExtractArg, | ||
[$PropertyType<$PropertyType<${resourceArgs}, '${batchKey}'>, 'has'>] |
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.
doesn't has
return a boolean rather than the underlying value?
slightly confused here sorry!
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.
Yeah, finding a solution here wasn't easy/obvious, even when I was pairing on it with @ATRAN2. This is extracting the arg type of has()
(T
) similarly to how the code extracts the batchKey arg type to the batch resource function (ie Set<T>
in this case) itself.
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.
ohhhh this is the type of the newKey, sorry i'm being dumb
looks good, nice approach :) 👍
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.
looking good!
Could you build the swapi examples and commit the newly generated files?
(should add this step to the contribution guide)
https://github.com/Yelp/dataloader-codegen/tree/master/examples/swapi
would like to see the generated output of this
schema.json
Outdated
}, | ||
"isBatchKeyASet": { | ||
"type": "boolean", | ||
"description": "(Optional) Set to true if the interface of the resource takes the batch key as a set (rather than an array). Default: false" |
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.
Might help to also note in the description what swagger flag can cause this to need to be True, so maybe a
when uniqueItems: true in the swagger spec
It already is, no? Or is there more I needed to check-in? |
ah missed this, thanks! We don't really have tests for the flow types here I don't think - but we can use the swapi example. Could we add a resolver that fetches something using this new option, and verify that flow doesn't blow up? thanks!! |
Done! also
|
Oh also output from
|
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.
Tidy work!
Love the links to tryflow and flowdefs in the PR description, helped me a lot 👍
Could you update the docs too? https://github.com/Yelp/dataloader-codegen/blob/HEAD/API_DOCS.md |
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.
lg2m!
Problem
There are cases where a batch key might not be an
Array
collection type, but rather aSet
type. In these cases, flow needs to define the type ofnewKey
differently. Currently, a function that defines a batch endpoint with aSet
batch parameter will fail.Solution
Allow users to set
isBatchKeyASet: true
to change the way the generated dataloader determines the type fornewKey
to work with aSet
.Here is a simple example of this typing working in the flow repl. This solution relies on
Set<T>.has()
, which accepts a parameter of typeT
.Verification
make test
passes. I've added some unit tests to verify it is behaving properly.I also added a contrived example to
examples/swapi
.