Urql: Unable to invalidate null Connection type

Created on 12 Apr 2020  路  2Comments  路  Source: FormidableLabs/urql

[email protected]
@urql/[email protected]

Steps to reproduce

  1. Have a Connection type following the spec and a mutation that creates a Node type.
type Query {
  exercisesConnection(page: PageInput!): ExercisesConnection
}

type Mutation {
  createExercise(input: CreateExerciseInput!): CreateExerciseResult
}
  1. Configure the Graphcache's cacheExchange so that when creating a Node type, all queries for the corresponding Connection type (i.e. the different pages) are invalidated:
cacheExchange({
  updates: {
    Mutation: {
      createExercise: (_result, _args, cache) => {
        cache
          .inspectFields({ __typename: 'Query' })
          .filter(({ fieldName }) => fieldName === 'exercisesConnection')
          .forEach(({ fieldName, arguments: args }) => {
            cache.invalidate('Query', fieldName, args);
          });
      },
    },
  },
})
  1. Query the first page for which there are no results:
query {
  exercisesConnection({ page: { after: null, first: 10 } }) {
    ...
  }
}

This returns null for the exercisesConnection field as there are no exercises:

Screen Shot 2020-04-11 at 21 45 20

  1. Execute the mutation and return to the list view.

Expected behavior

The query for the first page (3) results in a cache miss and a new request is made to the server, populating the list with the newly created exercise.

Actual behavior

The query for the first page results in a cache hit, and the list remains empty.

If the page is refreshed, the new exercise appears:

Screen Shot 2020-04-11 at 21 51 04

The curious thing is that, at this point, if a second exercise is created, the cache is properly invalidated and the query is re-executed upon visiting the list view.

I'm gonna work on a reproduction ASAP, but figured I'd open this in the meantime just in case it's obvious what I'm doing wrong.

Thanks!

bug 馃悰

Most helpful comment

hey @olistic

Thank you for the awesome explanation, I found the cause and there's a PR up.

All 2 comments

If I add the following logging lines:

@@ -19,7 +19,10 @@ const cacheOptions = {
      .inspectFields({ __typename: 'Query' })
      .filter(({ fieldName }) => fieldName === 'exercisesConnection')
      .forEach(({ fieldName, arguments: args }) => {
+       console.log(`Invalidating Query.${fieldName} with following args:`, args);
+       console.log('Pre:', cache.resolve('Query', fieldName, args));
        cache.invalidate('Query', fieldName, args);
+       console.log('Post:', cache.resolve('Query', fieldName, args));
      });
  },
},

Console after creating the first exercise

Screen Shot 2020-04-11 at 22 00 21

Console after creating the second exercise

Screen Shot 2020-04-11 at 22 02 49

hey @olistic

Thank you for the awesome explanation, I found the cause and there's a PR up.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ivosequeros picture ivosequeros  路  5Comments

TLadd picture TLadd  路  4Comments

dotansimha picture dotansimha  路  4Comments

tgrecojs picture tgrecojs  路  4Comments

danielres picture danielres  路  3Comments