-
Describe the issue To Reproduce
Expected behavior
Device / execution context Additional context |
Beta Was this translation helpful? Give feedback.
Replies: 14 comments 4 replies
-
@Veeksi, maybe you can reproduce this in a test-case? See packages/graphql/test/graphql_client_test.dart for examples. |
Beta Was this translation helpful? Give feedback.
-
@budde377 How can I run those tests myself? Is there guide somewhere how to setup it? Should that policy work with normal query or does it need watchQuery to get network result when it is available because I used normal query? Also can mutation make it to not appear 🤔? |
Beta Was this translation helpful? Give feedback.
-
@Veeksi. I think it should work out of the box with The idea is that you can mock the server by specifying the result of queries and mutations. You don't need to use |
Beta Was this translation helpful? Give feedback.
-
@budde377 Yeah, I will try to learn how to use it. I looked into it but I have no clue how can I make like a mutation that adds one repository 🤔 Like I know how can I make gql form of it, not sure how the mocked server should respond to it. Maybe one usecase would be like this:
|
Beta Was this translation helpful? Give feedback.
-
I think you can more or less duplicate the example you have from your private API, e.g., when( // Setup fetch all repos
link.request(any),
).thenAnswer(
(_) => Stream.fromIterable([
Response(data: ...),
]),
);
final rQuery1 = await client.query(QueryOptions(...)); // Fetch all repos
// Add some assertions on the result
when( // Setup add repo reply
link.request(any),
).thenAnswer(
(_) => Stream.fromIterable([
Response(data: ...),
]),
);
final rMutate = await client.mutate(QueryOptions(...)); // Add repo
// assert something about the result.
final rQuery2 = await client.query(QueryOptions(fetchPolicy: FetchPolicy.cacheOnly)); // Check cache is updated
// Assert cache is updated
So assuming you have concrete queries and responses, you can write a test like the above setting up appropriate responses. |
Beta Was this translation helpful? Give feedback.
-
Hmm, I will try something tomorrow. I think I can also try to create local server and reproduce the issue that way, buts lets see! Thanks in advance :) |
Beta Was this translation helpful? Give feedback.
-
@budde377 I made a quick demo by creating a local go server. You can use it by reading the README and running the application. It should show the issue quite clearly. Hopefully this helps! Link to repository: https://github.com/Veeksi/graphgl_issue |
Beta Was this translation helpful? Give feedback.
-
@Veeksi, thanks for this! If I understand you correctly:
|
Beta Was this translation helpful? Give feedback.
-
@budde377 Yep, that's correct! It should get the added todo in the second query but it doesn't appear. However, third query correctly contains it |
Beta Was this translation helpful? Give feedback.
-
Alright, so there's no general way that the client can know which array to add the result to, or even know if the result is newly created. Imagine that the list contains saved todos would you still add this upon creation? Probably not. So you'll have to manually refetch the query. |
Beta Was this translation helpful? Give feedback.
-
But when refetching the added todo should appear in the list, right? Of course, normally I would add the todo to list without refetching but I guess the API should correctly show the added todo when making query after mutation? |
Beta Was this translation helpful? Give feedback.
-
Sure, but it depends who you ask:
You can use something like watchQuery to listen for changes like this and update your UI or choose a fetchPolicy which circumvents the cache. I'm not an expert in bloc, but this would have come out of the box if you're using graphql_flutter instead. |
Beta Was this translation helpful? Give feedback.
-
Sorry if I jump in, I want to add some reference about using graphql_flutter with block, there is a package that is the following one https://github.com/artflutter/graphql_flutter_bloc it implements an example to handle the subscription on the client-side. However, I would like to say my unpopular opinion that maybe the server offers the simple API to implement the use case, and I'm strongly opinionated that the aggregation cache + network data has no sense because you will get the new data late due to the network latency so IMHO the client has no possibility to show before the new one and after the oldest at list wait for the new data before showing the screen, and this means that there is a wrong fetch policy used. In addition, using the cache is important here, but from the server-side, in particular, you can have the server that cache operation, and invalidate the cache to each operation, in accordance with your server example. However, usually, in this case, the better thing to do is implement an additional API from your server to get the last update on the Todo list, and run two queries on the client-side, like the following one:
Ah! My idea is referring to your server example, that it is the basic and more common one, so maybe all my idea can be wrong with the different server implementation. P.S: If there is no opposition, I will move this issue to the discussion side |
Beta Was this translation helpful? Give feedback.
-
Yeah, I thought that CacheAndNetwork policy means that the query waits the network result and combines it to cached result if it differs and if there is no network, the app can use the cache. Also about that graphql_flutter_bloc, I don't really see a point to use it because logic between graphql and bloc is not that hard to manage and don't want to rely too much on 3rd party libraries which are not so popular. The way that I want to approach this issue next is probably network check in repository level so I can check do I choose networkOnly policy if there is network and cacheOnly if there is no network. It could be also nice to see if there would be that kind of policy aswell which prefers the network result and returns the cached result only if there is no network 🤔 |
Beta Was this translation helpful? Give feedback.
Alright, so there's no general way that the client can know which array to add the result to, or even know if the result is newly created. Imagine that the list contains saved todos would you still add this upon creation? Probably not.
So you'll have to manually refetch the query.