Skip to content

Cache is not updating and new queries are not being displayed #17

Closed
@dwthgs

Description

@dwthgs

The cache gets persisted to the localStorage and it works offline but it never updates when the app gets online.
Once the cache gets persisted nothing will be changed until I clear the persisted cache/data from localStorage but it will only update for only one request and it will be stuck there with the new query data.

So basically what is happening is if there is persisted cached/data the query won't display or persist anything new.

Here is the code:

import React, { Component } from 'react'
import { ApolloClient } from "@wora/apollo-offline";
import { HttpLink } from "apollo-link-http";
import ApolloCache from '@wora/apollo-cache';
import { ApolloProvider } from '@apollo/react-hooks'

import { Query } from '@apollo/react-components';
import gql from 'graphql-tag'

const GET_BOOKS = gql`
{
    books {
        title
        id
    }
}
`



class App extends Component {
  state = {
    isLoading: true,
    client: null
  }

  async componentDidMount() {
    const httpLink = new HttpLink({
      uri: "http://localhost:8000/graphql"
    });

    const client = new ApolloClient({
      link: httpLink,
      cache: new ApolloCache({
        dataIdFromObject: o => o.id
      })
    });
    await client.hydrated()
    this.setState({
      isLoading: false,
      client
    })
  }

  render() {
    if(this.state.isLoading) return <div>Loading...</div>
    return (
      <ApolloProvider client={this.state.client}>
        <Query query={GET_BOOKS}>
          {({ loading, error, data }) => {
            if (loading) return <p>Loading...</p>;
            if (error) return <p>Error :(</p>;

            return data.books.map(({ id, title }) => (
                <div key={id}>
                <p>
                    {id}: {title}
                </p>
                </div>
            ));
          }}
        </Query>
      </ApolloProvider>
    )
  }
}

export default App

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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions