Skip to content

Cache update and invalidation in same transaction voids the update #3618

Description

@BickelLukas

Describe the bug

I am executing a query that updates creates a calendar event. As a result I am getting back that event instance and adding it to the list via an updater. This far everything works great.
However, creating a event may also add other events in the background that are not immediately returned (think recurring events). So what I want to do is:

  • add the event that is returned to the calendar immediately
  • invalidate the cache to refetch the events in the background so other events are eventually added

Now I have the following updater:

createCalendarEvent(result, args, cache) {
  if (result.createCalendarEvent.calendarEventInstance) {
    cache
      .inspectFields("Query")
      .filter((field) => field.fieldName === "calendarEvents")
      .forEach((field) => {
        const items = cache.resolve("Query", field.fieldName, field.arguments);
        if (Array.isArray(items)) {
          cache.link("Query", field.fieldName, field.arguments, [...items, result.createCalendarEvent.calendarEventInstance]);
        }

        // cache.invalidate("Query", field.fieldName, field.arguments); <-- Adding this breaks the update
      });
  }
},

As soon as I add the invalidate in the updater, the manual updates are no longer visible and the event does not get added until the refetch completes resulting in flashing of the UI.

In my opinion this should work but if that behaviour is on purpose please advice on how else this should be solved.

(I wanted to create a repro but the Postgres db for https://trygql.formidable.dev/ is down. If needed I can provide one later when its up again)

Reproduction

See description

Urql version

"urql": "^4.0.7"
"@urql/exchange-graphcache": "^7.0.2",

Validations

  • I can confirm that this is a bug report, and not a feature request, RFC, question, or discussion, for which GitHub Discussions should be used
  • Read the docs.
  • Follow our Code of Conduct

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions