apollographql / ac3-state-management-examples

✨ Learn Apollo Client 3's state management best practices
492 stars 179 forks source link

Reactive variables and HMR (hot module reloading) / fast-refresh #15

Open caycecollins opened 4 years ago

caycecollins commented 4 years ago

I have a project (not publicly available) that's using the same patterns shown in the apollo-local-state example. During development with HMR setup, components using the useQuery hook fail to receive updates when the mutation function is ran after an HMR update. Everything works fine before the HMR update, and a page reload fixes it after the update, but that defeats the purpose of HRM of course... is this a known issue? I haven't been able to find anything regarding this yet.

I haven't had a chance to create a simple example repo or codesandbox to show the issue, but wanted to ask here just in case it's a known issue.

stemmlerjs commented 4 years ago

@caycecollins, I know we were running into a few snags earlier last month with UI reactivity and getting reactive variables hooked up. I would love to hear how it's working for you now that we've tightened up AC3 with the official release.

caycecollins commented 4 years ago

@stemmlerjs thanks for the response. I just updated to 3.0.1 today and it doesn't appear to be resolved. It should be noted that I am using gatsby for this project, which I'm hoping isn't causing any additional issues here.

Here's some code about my specific implementation:

I have a modal component that uses a reactive variable to control it's open/close state:

const { data: { loginModal } = {} } = useQuery(GET_LOGIN_MODAL)
const { isOpen } = loginModal
const [showModal, setModalVisibility] = React.useState(false)

React.useEffect(() => {
  setModalVisibility(isOpen)
}, [isOpen])

const handleModalClose = () => {
  updateLoginModal({ isOpen: false })
}

Query:

import { gql } from "@apollo/client"

export const GET_LOGIN_MODAL = gql`
  query GetLoginModal {
    loginModal @client {
      isOpen
    }
  }
`

Mutation:

import { loginModalVar } from "../cache"

export const updateLoginModal = ({ isOpen }) => loginModalVar({ isOpen })

I am unable to open/close the modal after an HMR update. I added a log statement before and after the update in the mutation and it does properly change the variable, but the query in my modal component no longer receives update about it's change.

caycecollins commented 3 years ago

I'm still unable to resolve the issue but wanted to reference the open issues in the apollo-client repository which have some better explanations, examples, and outline efforts being taken to resolve the issue(s): https://github.com/apollographql/apollo-client/issues/6661 https://github.com/apollographql/apollo-client/issues/5870