Provide more control on when queries are executed #1169
-
I recently had an issue in my Flutter app where I realized that, in some situations, a GraphQL query could be executed dozens of times per second. It appeared to me that these executions were triggered by the reconstruction of the widget containing them, itself triggered by the appearance or disappearance of the keyboard (this is a well-known behavior with iOS, see also here). While the dozens of rebuilds triggered by a keyboard change on iOS has been considered as a bug, and some work is being done to reduce the amount of rebuilds, the fact that a widget can be rebuilt up to 60 times per second (and 120 for some devices) is normal and expected. Many sources insist that the build method must be idempotent, i.e. it must not change anything in the state of the widget or make expensive calls or calculations. For example, retrieving data from an external API, or performing expensive calculations, can be done at widget initialization. However, it seems to me (from my 2 months of learning Flutter and React Native) that this principle is not respected by this GraphQL library. If the cache strategy is set as "cache and network" or even "network only", the requests will be executed at each rebuild, up to 60 times per second. So I would like a better way to control the execution of my GraphQL queries. For example with a specific cache method that will not make any network call if the cache version is less than X seconds/minutes old, or by allowing queries outside the build method. If such a solution already exists, it would be interesting to highlight it in the library documentation. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Ok, let's make the point, that the bug is inside the flutter and not inside the library. If you rebuild the widget you have the behavior that the widget has for this use case. We have different policies for that and you can check the docs https://github.com/zino-hofmann/graphql-flutter/tree/main/packages/graphql#direct-cache-access-api If you are facing an error with the Flutter widget you can use the plain graphql client and implement your fetch logic as you want, and of the curse, any PR that improves the docs is considered asap and merged faster than other feature implementations. BTW the issue that opened 27 days ago is not a well-known bug! |
Beta Was this translation helpful? Give feedback.
Ok, let's make the point, that the bug is inside the flutter and not inside the library. If you rebuild the widget you have the behavior that the widget has for this use case.
We have different policies for that and you can check the docs https://github.com/zino-hofmann/graphql-flutter/tree/main/packages/graphql#direct-cache-access-api
If you are facing an error with the Flutter widget you can use the plain graphql client and implement your fetch logic as you want, and of the curse, any PR that improves the docs is considered asap and merged faster than other feature implementations.
BTW the issue that opened 27 days ago is not a well-known bug!