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

feat(ui-collection): add a proto optimistic() method to collections #183

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Fen747
Copy link
Collaborator

@Fen747 Fen747 commented Feb 4, 2022

TODO:

  • We need to know how to determine the operatioName

Related:

*/
public optimistic(_id: ObjectId | string, document: T) {
const stringId = _id instanceof ObjectId ? _id.toString() : _id;
const operationName = ""; // ! how do we determine this here ?
Copy link
Contributor

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?

Copy link
Collaborator Author

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

Copy link
Collaborator Author

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 ?
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

related to #87

Copy link
Collaborator Author

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/

@Fen747 Fen747 force-pushed the feat/optimistic-updates branch 3 times, most recently from d4f7ff2 to 4a5d139 Compare February 9, 2022 14:59
[operationName]: {
_id: stringId,
__typename:
collectionName[collectionName.length - 1] === "s"
Copy link
Contributor

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() { }

Copy link
Collaborator Author

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" ?

@@ -360,6 +379,12 @@ export abstract class Collection<T = null> {
return this.hybridFind(false, query, body);
}

protected optimisticUpdates = true;

public enableOptimisticUpdates(option: boolean) {
Copy link
Contributor

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(),
Copy link
Contributor

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"

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

2 participants