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 UI when adding categories to articles #575

Merged
merged 2 commits into from
Jul 25, 2024

Conversation

jiru
Copy link
Contributor

@jiru jiru commented Jul 23, 2024

Closes #481.

The reason the UI was not refreshed was a regression introduced by cf550a0. Adding the argument "(statuses: $articleCategoryStatuses)" changes the key used by InMemoryCache from "articleCategories" to "articleCategories({"statuses":["NORMAL"]})". This results in cache.readFragment() (called in components/ArticleCategories/CategoryOption.js throwing this error:

Invariant Violation: Can't find field articleCategories on object {
  "id": ...,
  "text": ...
  ...
  "articleCategories({\"statuses\":[\"NORMAL\"]})": [
    {
      "type": "id",
      "generated": false,
      "id": "ArticleCategory:...__...",
      "typename": "ArticleCategory"
    }
  ],
  "stats": [
    ...
  ],
  "user": {
    ...
  }
}

This commits solves this issue by forcing the key to be "articleCategories".

The technique of using an @connection directive is documented here: https://www.apollographql.com/docs/react/data/directives/#connection

jiru added 2 commits July 23, 2024 14:54
Closes cofacts#481.

The reason the UI was not refreshed was a regression introduced by cf550a0.
Adding the argument "(statuses: $articleCategoryStatuses)" changes the key used by InMemoryCache
from "articleCategories" to "articleCategories({"statuses":["NORMAL"]})".
This results in cache.readFragment() (called in components/ArticleCategories/CategoryOption.js throwing this error:
```
Invariant Violation: Can't find field articleCategories on object {
  "id": ...,
  "text": ...
  ...
  "articleCategories({\"statuses\":[\"NORMAL\"]})": [
    {
      "type": "id",
      "generated": false,
      "id": "ArticleCategory:...__...",
      "typename": "ArticleCategory"
    }
  ],
  "stats": [
    ...
  ],
  "user": {
    ...
  }
}
```

This commits solves this issue by forcing the key to be "articleCategories".

The technique of using an @connection directive is documented here:
https://www.apollographql.com/docs/react/data/directives/#connection
@coveralls
Copy link

Coverage Status

coverage: 71.353%. remained the same
when pulling 478d157 on jiru:fix_add_categories
into 91dcbfb on cofacts:master.

Copy link
Member

@MrOrz MrOrz left a comment

Choose a reason for hiding this comment

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

Your solution is super elegant! It's great to learn about the cache mechanism of Apollo client and the affect that argument filters have on the cache. Thanks for your contribution!

@MrOrz MrOrz merged commit 5eddb4f into cofacts:master Jul 25, 2024
2 checks passed
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.

Category label is not updated after adding one category
3 participants