Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error executing mutations offline without the network parameter #15

Closed
lawrenz1337 opened this issue Aug 2, 2019 · 19 comments
Closed
Labels
bug Something isn't working

Comments

@lawrenz1337
Copy link

Hello!
I use Create-react-app for the application and thus I use import graphql from "babel-plugin-relay/macro"; because I don't have a customizable .babelrc. The library seems to work one way. It stores everything into the localStorage but when the connection is back it just doesn't do anything

@morrys
Copy link
Owner

morrys commented Aug 2, 2019

Hi @lawrenz1337,
can you better describe the case of error that shows?
Which fetch policy are you using?

Thank you,
Lorenzo

@lawrenz1337
Copy link
Author

The funny thing is that there are no errors. The data is being stored in the localStorage and afterwards it just doesnt do anytning. And I am pretty sure that I don't have any fetch policies

@morrys
Copy link
Owner

morrys commented Aug 2, 2019

If no recovery policy is selected, relay uses network_only.

Among the added features I have provided "fetchPolicy CACHE_FIRST (which is version 1.0.0 I will change to STORE_OR_NETWORK)"

This policy allows you to take advantage of the local cache and avoid invoking the network.

Furthermore, when the application is offline, the library will automatically use the STORE_ONLY policy.

@morrys
Copy link
Owner

morrys commented Aug 2, 2019

In this repository I created the version relay-hooks respecting as much as possible the indications of what will be the official version.

In the next version I integrate the logic of the query renderer (useQuery) also in this repository.

Here you can find more information: relay-hooks issue 5

Please ask for any questions.

@lawrenz1337
Copy link
Author

@morrys so basically what you are saying if I change fetchPolicy to CACHE_FIRST it will hydrate the store and work properly?

@morrys
Copy link
Owner

morrys commented Aug 2, 2019

I say that the store is always rehydrated, but this is evident with the CACHE_FIRST policy because the execution of the query is avoided.

If you use network_only and the application is online, the store is not used (as it should be).

Did I explain myself well? :)

@morrys morrys changed the title Store does not re-hydrate when using babel-plugin-relay question on store and fetch policies Aug 3, 2019
@morrys morrys added the question Further information is requested label Aug 3, 2019
@lawrenz1337
Copy link
Author

Ok, so do I need to define a refetchContainer for each query i need to be refetched and pass fetchPolicy: "CACHE_FIRST" as options?

@morrys
Copy link
Owner

morrys commented Aug 5, 2019

It's enough

import {QueryRenderer} from 'react-relay-offline';
 <QueryRenderer
      environment={environment}
      dataFrom="CACHE_FIRST"
...

@lawrenz1337
Copy link
Author

Sorry but it still doesn't work :( Data that i send offline never reaches the server... Maybe I could write the steps I did and we can pinpoint the problem there?

  1. change the environment with import { Store, Environment } from "react-relay-offline";
  2. change the app with import {QueryRenderer} from 'react-relay-offline'; <QueryRenderer environment={environment} dataFrom="CACHE_FIRST" ...
  3. change the mutation with import { commitMutation } from "react-relay-offline";
  4. go to the app + go offline + send mutation + go online.

After those steps nothing gets sent to the server. I still think that this is cause by graphql not being imported from react-relay-offline but idk

@morrys
Copy link
Owner

morrys commented Aug 5, 2019

Don't worry, I realized that you had problems in restoring the store ...

can you try adding these offline options and tell me the outputs on the console after you have done step 4 again?

const offlineOptions = {
   finish: (isSuccess, mutations) => {// optional
     console.log ("finish offline", isSuccess, mutations);
   },
   onComplete: (options) => {// optional
     const {id, offlinePayload, snapshot} = options;
    console.log ("onComplete offline", options);
     return true;
   },
   onDiscard: (options) => {// optional
     const {id, offlinePayload, error} = options;
     console.log ("onDiscard offline", options);
     return true;
   },
   onPublish: (offlinePayload) => {// optional
     console.log ("onPublish offline", offlinePayload);
     return offlinePayload
   }
};

Thanks

@lawrenz1337
Copy link
Author

lawrenz1337 commented Aug 5, 2019

here you go!
offline
First log line appeared after i sent my mutation. The second and third appeared after i went online. It does show some error btw, i can give you a stack trace if u need it
Thanks for the help

@morrys
Copy link
Owner

morrys commented Aug 5, 2019

Maybe you found an error :)
Can you try adding network property to offlineOptions?

const offlineOptions = {
   network: network, //optional
   finish: (isSuccess, mutations) => {// optional
     console.log ("finish offline", isSuccess, mutations);
   },
   onComplete: (options) => {// optional
     const {id, offlinePayload, snapshot} = options;
    console.log ("onComplete offline", options);
     return true;
   },
   onDiscard: (options) => {// optional
     const {id, offlinePayload, error} = options;
     console.log ("onDiscard offline", options);
     return true;
   },
   onPublish: (offlinePayload) => {// optional
     console.log ("onPublish offline", offlinePayload);
     return offlinePayload
   }
};

https://github.com/morrys/offline-examples/blob/master/relay/todo-updater/js/relay/index.js#L35

thanks :)

@lawrenz1337
Copy link
Author

@morrys Yep, it fixed it! Thanks a lot! but why is that parameter optional? and what does it do exactly because I couldn't find any docs about it

@morrys
Copy link
Owner

morrys commented Aug 6, 2019

@lawrenz1337, this is what I wrote in README.md (I'm working on better documentation through docusaurus)

network: it is possible to configure a different network for the execution of mutations in the queue; all the information of the mutation saved in the offline store are inserted into the "metadata" field of the CacheConfig so that they can be used during communication with the server

It is optional because by default it uses the same network set in the environment.
This is the error I have to fix in the next release (I hope to release it between today and tomorrow)

I advise you, at the beginning, to always use these options offline in order to be able to debug what is happening in the offline store :)

Thank you for reporting, please ask or open other issues for any information.

Lorenzo

@morrys morrys changed the title question on store and fetch policies error executing mutations offline without the network parameter Aug 6, 2019
@morrys morrys added bug Something isn't working and removed question Further information is requested labels Aug 6, 2019
@lawrenz1337
Copy link
Author

@morrys The thing that confused me the most was that it didn't throw any exceptions to the console and I didn't see any errors until you gave me that debugging code. Thanks for the help :)

Mikhail

@morrys
Copy link
Owner

morrys commented Aug 6, 2019

I add a note in the project to think of a better error report or a utility to log :)

It is thanks to the contribution of those who use this library that you get to have a better result :)

@morrys
Copy link
Owner

morrys commented Aug 6, 2019

@lawrenz1337, with the version 0.8.1 of react-relay-offline the network parameter is optional again :)

Let me know if you can confirm it too :)

thanks again

Lorenzo

@lawrenz1337
Copy link
Author

@morrys Thanks for that but I will wait to update to 0.8.1 :) Probably will update next week. Cheers

@morrys
Copy link
Owner

morrys commented Aug 6, 2019

Meanwhile I close the issue, if you find other problems we reopen it :)

@morrys morrys closed this as completed Aug 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants