aerogear / offix

GraphQL Offline Client and Server
https://offix.dev
Apache License 2.0
758 stars 45 forks source link

Optimistic Response is not generated while offline or bad internet connection #344

Closed bdbch closed 4 years ago

bdbch commented 4 years ago

Bug Report

Hey, awesome project! I used a custom written wrapper for our offline support before but switching to a maintained project is way better.

I implemented offix in our messaging app and everything seems to work fine but I can't get our optimistic response to work. The problem is that everytime I try to send a message into a chat while I'm offline it does not generate the optimistic UI for the message so you have to wait until the client gets back online to see the optimistic UI for a second and then you see the actual message responded by the server.

Am I missing something or doing something wrong? This is how we send the mutation:

client.offlineMutate({
  idField: 'id',
  mutation: SEND_MESSAGE,
  optimisticResponse,
  update: () => sendMessageUpdate(chatId),
  variables: {
    chatId,
    clientGeneratedUUID: messageUUID,
    forwardedMessages: [],
    images: mappedImages,
    message,
  },
})

the optimistic response itself is self generated but just looks fine (and it's working when I'm online).

Do I have to do something to actually get the optimistic UI while offline?

wtrocki commented 4 years ago

Hi

If your response is the same or similar to your variables offix will generate it out of the box. There is no need to pass update function and optimistic response. For more info check our docs: https://offix.dev/docs/offix-cache

However if your case requires custom update and optimistic response I will look for the issue in sendMessageResponse that is missing in snippet. We should see this method called twice - once with your optimistic response and once with actual data.

wtrocki commented 4 years ago

For custom optimistic response and update method please check this chat app build with old version of offix:

https://github.com/aerogear-attic/chat-app/blob/master/client/src/components/ChatRoomScreen/index.tsx#L130

bdbch commented 4 years ago

Thanks! I'll look into that and report back if that helped me solving my issue.

bdbch commented 4 years ago

You were right, I had a problem in my sendMessageUpdate function. It works again. As I can see offix does not cache the update functions between app sessions so when I restart the app while offline, the update functions are gone right?

https://offix.dev/docs/offline-client#global-update-functions

The documentation here isn't really clear. Do you have an good example on how those global update functions should look like? I'm trying to keep my sent offline messages when the app is restarted but I can't get it working.

darahayes commented 4 years ago

@bdbch glad to hear you got to the bottom of that problem. With regards to the restoring optimistic responses problems. We had another user create an issue on the same topic and I left some tips there. Can you check my comments in https://github.com/aerogear/offix/issues/340 and see if there are helpful in any way?

wtrocki commented 4 years ago

We need this as part of the sample app and copy-paste your comment to the docs. I fully agree that our docs in that area are very unclear.

bdbch commented 4 years ago

Thanks! I'll take a look into your tips @darahayes.

@wtrocki is the documentation on github as well?

wtrocki commented 4 years ago

We have documentation on the website http://offix.dev Source code for that documentation can be found here: https://github.com/aerogear/offix/tree/master/docs

We are happy to accept contributions

bdbch commented 4 years ago

If I understand the concept I'll look into contributing a demo to the docs!

bdbch commented 4 years ago

I was able to resolve this issue here: https://github.com/aerogear/offix/issues/340