question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refetch query ignores recent change to local storage

See original GitHub issue

Intended outcome:

When using useMutation we are able to manually call the mutation and execute a sychronous action right after such as a write to local storage.

Actual outcome:

If a refetch query is specified, it seems that recent local storage is not up to date. The refetch query is performed on an outdated local storage not detecting the recent entry into local storage.

How to reproduce the issue:

apollo setup

import { cache } from 'libraries/apollo/cache';
import { resolvers } from 'libraries/apollo/resolvers';
import withApollo from 'next-with-apollo';

const authLink = setContext((_, { headers }) => {
  const mytoken= localStorage.getItem('mytoken'); // // <----- this should have been set on just finished mutation but is still empty

  return {
    headers: {
      ...headers,
      Authorization: token ? `Bearer ${mytoken}` : "",
    }
  }
});


function createClient({ headers }) {
  return new ApolloClient({
    link: authLink.concat(createHttpLink({ uri: 'http://localhost:2222',  credentials: 'include' })),
    cache: cache,
    resolvers: resolvers
  })
}

export default withApollo(createClient);

mutation action

const response = yield call(authInMutation, {
  variables: {
    username: email,
    password: password
  }
});
localStorage.setItem("mytoken", response.data.access_token) // <----- write to local storage after successful sign in

queries & mutations

const AUTH_IN_MUTATION = gql`
  mutation AUTH_IN_MUTATION($email: String!, $password: String!) {
    auth_in(input: { email: $email, password: $password }) {
      message
    }
  }
`;

const AUTH_REFRESH_QUERY= gql`
  query AUTH_REFRESH_QUERY{
      session
  }
`;

const [authInMutation, { loading, data, error }] = useMutation(AUTH_IN_MUTATION , {
  refetchQueries: [{
    query: AUTH_REFRESH_QUERY // <--- refetch this query after successful sign in
  }]
});

Versions

  System:
    OS: Windows 10 10.0.19042
  Binaries:
    Node: 15.8.0 - ~\scoop\apps\nodejs\current\node.EXE
    Yarn: 1.22.10 - ~\scoop\apps\nodejs\current\bin\yarn.CMD
    npm: 7.5.1 - ~\scoop\apps\nodejs\current\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.423.0), Chromium (89.0.774.54)
  npmPackages:
    @apollo/client: ^3.3.11 => 3.3.11
    next-with-apollo: ^5.1.1 => 5.1.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
newtoniumx3commented, Mar 15, 2021

I’m having a hard time re-creating a simpler environment as its got some complex config with nextjs, apollo and redux-saga all together. This could potentially be related to the integration with saga and the flow of execution.

Although upon closer look, isn’t it normal that the refetch occurs during the mutation call? Since it’s part of the mutation execution configuration? If it didn’t refetch during that call, how then would you instruct it to refetch the queries at a later time?

I’m going to debug this a little more and try to make this into a separate repo, if I can’t reproduce today I’ll close this today until I can. Thank you for your assistance.

0reactions
newtoniumx3commented, Mar 15, 2021

I decided to switch to cookie based for now which resolves the issue as the storing of the cookie happens behind the scenes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refetching queries in Apollo Client - Apollo GraphQL Docs
The InMemoryCache helps you determine which active queries might have been invalidated by recent cache updates. Local cache updates and refetching work ...
Read more >
Apollo useQuery() - "refetch" is ignored if the response is the ...
This is a scenario where apollo's caches come handy. Client import { resolvers, typeDefs } from './resolvers'; let cache = new ...
Read more >
Advanced Topics on Caching – Angular
With refetchQueries you can specify one or more queries that you want to run after a mutation is completed in order to refetch...
Read more >
Tips and Tricks for working with Apollo Cache - Medium
When new tasks are added to the data store, we can tell Apollo to refetch any previous FETCH_TASKS query results in its cache...
Read more >
Cache Behavior - Redux Toolkit
RTK Query > Usage > Cache Behavior: defaults, cache lifetimes, ... endpoint to always refetch when a new subscriber to the query is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found