lens-protocol / lens-sdk

The official SDK to interact with the Lens Protocol
https://docs.lens.xyz/docs/what-is-lens
MIT License
230 stars 72 forks source link

Reaction Toggle not updating cache for publications #964

Open dave4506 opened 3 weeks ago

dave4506 commented 3 weeks ago

Describe the bug Adding a reaction to a post NOT created by the signed-in user does not update the local apollo cache state, BUT updating a publication created by the signed-in user does result in a cache update.

To Reproduce Provide with as much details as possible the call, the hook, the interaction with the Lens SDK that has been problematic for you. Links to public GH repos or working examples are really appreciated.

Feels like #854 has very similar issues to me.

Expected behaviuor A clear and concise description of what you expected to happen. State should update for all publications

If using the React bindings, what is the essence of your React app?

Version of the packages you are using

"@lens-protocol/client": "^2.3.1",
    "@lens-protocol/metadata": "^1.2.0",
    "@lens-protocol/react-web": "^2.3.1",
    "@lens-protocol/wagmi": "^4.1.4",

Additional context Add any other context about the problem here.

dave4506 commented 3 weeks ago

I have managed to hone in on the issue; the updateFragment function utilized by the PublicationCacheManager returns null on non "user connected" posts.

Curiously, from looking into the apollo cache, I was able to focus in on the specific condition that allows for updateFragment to work:

If in ROOT_QUERY, there is a request for profile, it seems to work as intended, if there is no query there; the local cache will not work.

dave4506 commented 3 weeks ago

Found the issue, for posterity sake and for feedback to core team.

The issue relates to statsFor for profiles.

In lensConfig, I DO NOT have any statsFor configured. But when I was querying for these publications, I did it with the following:

 const res = usePublications({
    where: {
      publicationTypes: [PublicationType.Post],
      metadata: {
        publishedOn: [LENS_APP_ID],
        tags: {
          all: [postTags!.txn, postTags!.chain],
        },
      },
    },

Because of publishedOn, profiles return stats just for LENS_APP_ID, there is no stats for the case of [] (i.e all apps)

This becomes an issue because when the PublicationCacheManager attempts to find the cache result, it passes variables from the global config, which was set to [] for me. This meant that profiles queried by usePublications could not be "found" because they are missing stats for the [] condition.

I have temp fixed this by simply dropping publishedOn. But to me, this is a bug to fix.

cesarenaldi commented 1 day ago

Hey @dave4506 thank you for the detailed explanation and the temp workaround.

Your feedback is very welcome, indeed this (the interoperability of some nested field arguments) is an area we are looking at as part of the coming iteration of the Lens API. This specifically might just be simplified altogether removing the need for a lens config property and making the publishedOn enough for all use cases.