n1ru4l/graphql-live-query

List query doesn't get updated

strblr opened this issue · 1 comments

Hey,

I encountered what seems to be a bug on certain list updates. I have a (simplified) schema that looks like this:

type Query {
    project(id: ID!): Project!
    contradictions(
      projectId: ID!
      filters: ListFiltersInput
    ): ContradictionPage!
  }

type Mutation {
    selectContradiction(contradiction: ID!): Project!
  }

When instantiating the InMemoryLiveQueryStore, I use this as indexBy:

[
  {
    field: "Query.contradictions",
    args: ["projectId"]
  }
];

Then my frontend gql document looks like this:

export const ContradictionsQuery = gql`
  ${ContradictionPageFragment}

  query Contradictions($projectId: ID!, $filters: ListFiltersInput) @live {
    contradictions(projectId: $projectId, filters: $filters) {
      ...ContradictionPage
    }
  }
`;

But then I have the following mutation resolver:

async selectContradiction(
      _,
      { contradiction: id },
      { currentUser, invalidate }
    ) {
      // ... bunch of checks and db fetching
      // .........
      project = (await Project.setById(project._id, {
        selectedContradiction: project.selectedContradiction?.equals(
          contradiction._id
        )
          ? null
          : contradiction._id
      }))!;
      invalidate([
        `Query.project(id:"${project._id.toHexString()}")`,
        `Query.contradictions(projectId:"${project._id.toHexString()}")`
      ]);
      return project;
    }

Problem: This invalidate call doesn't actually invalidate the contradictions list. The query doesn't get re-executed, no diff is sent to the client for that list. The Query.project invalidation however works correctly.

I made sure that:

  • The index I wanted (Query.contradictions(projectId:"......")) actually exists in the store by checking its internal map. Also the content of the map for that key is coherent with the live queries being made.
  • The index I pass to invalidate is correct (console.log'd it) and matches the one in the store

Do you have an idea of what the problem may be?

Turns out this was a bug created by the use of dataloader. For reasons yet unknown, a stale version of a MongoDB document is cached in a dataloader, making previous and next contradictions queries the same even after update, thus no diff, thus no udpate.