-
Notifications
You must be signed in to change notification settings - Fork 23
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
feat(ui-collection): add a proto optimistic() method to collections #183
base: main
Are you sure you want to change the base?
Conversation
1712f7b
to
b28f7cc
Compare
*/ | ||
public optimistic(_id: ObjectId | string, document: T) { | ||
const stringId = _id instanceof ObjectId ? _id.toString() : _id; | ||
const operationName = ""; // ! how do we determine this here ? |
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.
based on this.getName(), we'll need to do it for Find FindOne and Count maybe? Or give some opts?
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.
The idea would be to have this behavior by default.
A developper will prefer to write :
collection.updateOne(_id, document)
and expect the cache to be automatically update (or disable that behavior with a optimisticReponse: false
option) than to have to manually add an additional for each update in his software :
collection.updateOne(_id,
document,
{ optimisticResponse: collection.optimistic(_id, document) }
)
We could add an additional optional argument with the queryName
, that would default to [Collection]UpdateOne
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.
Proposition commited : 2a6aac5
collectionName[collectionName.length - 1] === "s" | ||
? collectionName.slice(0, -1) | ||
: collectionName, // we could use https://www.npmjs.com/package/pluralize | ||
...document, // should we deepclone here just in case ? |
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 you need to transform the document into a graph-like
model of it.
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.
related to #87
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'm not sure of hat you mean.
What we need here is to provide the exact same values as in the input : https://www.apollographql.com/docs/react/performance/optimistic-ui/
d4f7ff2
to
4a5d139
Compare
[operationName]: { | ||
_id: stringId, | ||
__typename: | ||
collectionName[collectionName.length - 1] === "s" |
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.
This is not a reliable way to do this, we might need to add a getTypeName() { }
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.
Moved it to collection.getTypeName, keeping this dumb implementation. Probably that the best would be to have the generator to auto implement an override of getTypename
and jut keep this dumb implementation "just in case" ?
35d5888
to
469e726
Compare
@@ -360,6 +379,12 @@ export abstract class Collection<T = null> { | |||
return this.hybridFind(false, query, body); | |||
} | |||
|
|||
protected optimisticUpdates = true; | |||
|
|||
public enableOptimisticUpdates(option: boolean) { |
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 don't know if this is a good approach, at what step would you actually call this enableOptimisticUpdates? In every component? Rather have something: isOptimisticUpdateEnabled() { return true }
// `${this.getName()}UpdateOne` | ||
[operationName]: { | ||
_id: stringId, | ||
__typename: this.getTypeName(), |
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.
Yep, we should also add this to blueprint collection generation to return the entity name so we don't have situations like "Goose" "Geese"
TODO:
Related: