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

With makeDefaultStorage and offlineExchange: error: "Cannot read property '0' of null" #864

Closed
frederikhors opened this issue Jun 14, 2020 · 3 comments · Fixed by #866
Closed
Labels
bug 🐛 Oh no! A bug or unintented behaviour.

Comments

@frederikhors
Copy link
Contributor

frederikhors commented Jun 14, 2020

Steps to reproduce

  1. go on this CRUD example here: https://codesandbox.io/s/urql-svelte-crud-with-indexeddb-cqg5i
  2. click on "New todo"
  3. save it with button
  4. click on "Reload" button
  5. it gives an error: Cannot read property '0' of null

GIF:

issue

Code:

import gql from "graphql-tag";
import { initClient, dedupExchange, fetchExchange } from "@urql/svelte";
import { offlineExchange } from "@urql/exchange-graphcache";
import { makeDefaultStorage } from "@urql/exchange-graphcache/default-storage";
import { TODOS_QUERY, TODO_FRAG } from "./gql";

const updates = {
  Mutation: {
    addTodo: (result, args, cache, info) => {
      const allFields = cache.inspectFields("Query");
      const todoQueries = allFields.filter(x => x.fieldName === "todos");
      todoQueries.forEach(x => {
        cache.updateQuery(
          { query: TODOS_QUERY, variables: x.arguments },
          data => {
            return {
              ...data,
              todos: [...data.todos, result.addTodo]
            };
          }
        );
      });
    }
  }
};

const optimistic = {
  addTodo: (variables, cache, info) => {
    return {
      ...variables,
      __typename: "Todo",
      id: "TEMP_ID_" + Date.now(),
      complete: false,
      createdAt: Date.now()
    };
  }
};

const storage = makeDefaultStorage({
  idbName: "graphcache-v3",
  maxAge: 7
});

const exchanges = [
  dedupExchange,
  offlineExchange({ storage, updates, optimistic }),
  fetchExchange
];

function OnInit() {
  initClient({ url: "https://h1pcl.sse.codesandbox.io", exchanges });
}

export default {
  OnInit
};
@frederikhors frederikhors added the bug 🐛 Oh no! A bug or unintented behaviour. label Jun 14, 2020
@frederikhors
Copy link
Contributor Author

I thought it was due to this: https://spectrum.chat/urql/help/is-there-a-way-to-invalidate-queries-with-variables-without-using-variables~5e4fc1b8-849e-46e9-a9ca-72698a8433d0?m=MTU4NTkyMDE5ODMwOA==.

But it's happening also with cache.invalidate for the deleteTodo resolver:

deleteTodo: (result, args, cache, info) => {
  if (result.deleteTodo) {
    cache.invalidate({ __typename: "Todo", id: args.id });
  }
}

@frederikhors
Copy link
Contributor Author

Here the gif for deleteTodo with cache.invalidate().

issue

@kitten
Copy link
Member

kitten commented Jun 14, 2020

Yea, I found the issue. I think it’s a bug with our default storage, so I’ll have to take another look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Oh no! A bug or unintented behaviour.
Projects
None yet
2 participants