{"action":"created","issue":{"url":"https://api.github.com/repos/apollographql/apollo-client/issues/2712","repository_url":"https://api.github.com/repos/apollographql/apollo-client","labels_url":"https://api.github.com/repos/apollographql/apollo-client/issues/2712/labels{/name}","comments_url":"https://api.github.com/repos/apollographql/apollo-client/issues/2712/comments","events_url":"https://api.github.com/repos/apollographql/apollo-client/issues/2712/events","html_url":"https://github.com/apollographql/apollo-client/issues/2712","id":281085838,"node_id":"MDU6SXNzdWUyODEwODU4Mzg=","number":2712,"title":"setVariables doesn't notify subscribers when cache hit ","user":{"login":"szechyjs","id":847739,"node_id":"MDQ6VXNlcjg0NzczOQ==","avatar_url":"https://avatars3.githubusercontent.com/u/847739?v=4","gravatar_id":"","url":"https://api.github.com/users/szechyjs","html_url":"https://github.com/szechyjs","followers_url":"https://api.github.com/users/szechyjs/followers","following_url":"https://api.github.com/users/szechyjs/following{/other_user}","gists_url":"https://api.github.com/users/szechyjs/gists{/gist_id}","starred_url":"https://api.github.com/users/szechyjs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/szechyjs/subscriptions","organizations_url":"https://api.github.com/users/szechyjs/orgs","repos_url":"https://api.github.com/users/szechyjs/repos","events_url":"https://api.github.com/users/szechyjs/events{/privacy}","received_events_url":"https://api.github.com/users/szechyjs/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":{"login":"hwillson","id":137740,"node_id":"MDQ6VXNlcjEzNzc0MA==","avatar_url":"https://avatars1.githubusercontent.com/u/137740?v=4","gravatar_id":"","url":"https://api.github.com/users/hwillson","html_url":"https://github.com/hwillson","followers_url":"https://api.github.com/users/hwillson/followers","following_url":"https://api.github.com/users/hwillson/following{/other_user}","gists_url":"https://api.github.com/users/hwillson/gists{/gist_id}","starred_url":"https://api.github.com/users/hwillson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hwillson/subscriptions","organizations_url":"https://api.github.com/users/hwillson/orgs","repos_url":"https://api.github.com/users/hwillson/repos","events_url":"https://api.github.com/users/hwillson/events{/privacy}","received_events_url":"https://api.github.com/users/hwillson/received_events","type":"User","site_admin":false},"assignees":[{"login":"hwillson","id":137740,"node_id":"MDQ6VXNlcjEzNzc0MA==","avatar_url":"https://avatars1.githubusercontent.com/u/137740?v=4","gravatar_id":"","url":"https://api.github.com/users/hwillson","html_url":"https://github.com/hwillson","followers_url":"https://api.github.com/users/hwillson/followers","following_url":"https://api.github.com/users/hwillson/following{/other_user}","gists_url":"https://api.github.com/users/hwillson/gists{/gist_id}","starred_url":"https://api.github.com/users/hwillson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hwillson/subscriptions","organizations_url":"https://api.github.com/users/hwillson/orgs","repos_url":"https://api.github.com/users/hwillson/repos","events_url":"https://api.github.com/users/hwillson/events{/privacy}","received_events_url":"https://api.github.com/users/hwillson/received_events","type":"User","site_admin":false}],"milestone":null,"comments":3,"created_at":"2017-12-11T16:44:39Z","updated_at":"2018-07-05T15:53:19Z","closed_at":null,"author_association":"NONE","body":"**Intended outcome:**\r\nCalling `setVariables()` on a `ObservableQuery` should notify subscribers of a data change. This works as expected if the query is not cached and results in a network request. \r\n\r\n**Actual outcome:**\r\nIf a query has been cached `setVariables()` does not notify its subscribers of the data change. From tracing the code it appears the correct data is pulled from the cache and set as the query result, however subscribers are never notified of the result like they are when a network query occurs.\r\n\r\n**How to reproduce the issue:**\r\n\r\napp.module.ts\r\n```ts\r\nconstructor(apollo: Apollo, httpLink: HttpLink) {\r\n  apollo.create({\r\n    link: httpLink.create({}),\r\n    cache: new InMemoryCache()\r\n  });\r\n}\r\n```\r\n\r\ndata.service.ts\r\n```ts\r\nconst TestQuery = gql`\r\n  query TestQuery($name: String!) {\r\n    users(name: $name) {\r\n      name\r\n      userid\r\n    }\r\n  }\r\n`;\r\n\r\nconstructor(private apollo: Apollo) {\r\n  this.queryRef = this.apollo.watchQuery<QueryResponse>({\r\n    query: TestQuery,\r\n    variables: {\r\n      name: \"\"\r\n    }\r\n  });\r\n}\r\n\r\ngetUsers() {\r\n  return this.queryRef.valueChanges;\r\n}\r\n\r\nnameUpdate(name) {\r\n  this.queryRef.setVariables({\r\n    name: name\r\n  });\r\n}\r\n```\r\n\r\nview.component.ts\r\n```ts\r\nngOnInit() {\r\n  this.dataService.getUsers().subscribe({data}) => {\r\n    this.users = data.users;\r\n  });\r\n}\r\n\r\n// These are tied to user actions, but for demonstrating the issue, calling directly.\r\nthis.dataService.nameUpdate('bob');  // Performs new network query and updates observable\r\nthis.dataService.nameUpdate('john'); // Performs new network query and updates observable\r\nthis.dataService.nameUpdate('bob'); // Queries the cache and DOES NOT update observable\r\n```\r\n\r\n**Versions**\r\n- apollo-client@2.1.0\r\n- apollo-cache@1.0.2\r\n- apollo-cache-inmemory@1.1.3\r\n- apollo-angular@1.0.0\r\n"},"comment":{"url":"https://api.github.com/repos/apollographql/apollo-client/issues/comments/402769106","html_url":"https://github.com/apollographql/apollo-client/issues/2712#issuecomment-402769106","issue_url":"https://api.github.com/repos/apollographql/apollo-client/issues/2712","id":402769106,"node_id":"MDEyOklzc3VlQ29tbWVudDQwMjc2OTEwNg==","user":{"login":"hwillson","id":137740,"node_id":"MDQ6VXNlcjEzNzc0MA==","avatar_url":"https://avatars1.githubusercontent.com/u/137740?v=4","gravatar_id":"","url":"https://api.github.com/users/hwillson","html_url":"https://github.com/hwillson","followers_url":"https://api.github.com/users/hwillson/followers","following_url":"https://api.github.com/users/hwillson/following{/other_user}","gists_url":"https://api.github.com/users/hwillson/gists{/gist_id}","starred_url":"https://api.github.com/users/hwillson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hwillson/subscriptions","organizations_url":"https://api.github.com/users/hwillson/orgs","repos_url":"https://api.github.com/users/hwillson/repos","events_url":"https://api.github.com/users/hwillson/events{/privacy}","received_events_url":"https://api.github.com/users/hwillson/received_events","type":"User","site_admin":false},"created_at":"2018-07-05T15:53:19Z","updated_at":"2018-07-05T15:53:19Z","author_association":"CONTRIBUTOR","body":"Hi all - sorry for the delay in responding to this. \r\n\r\n### TL;DR\r\n\r\n`setVariables` is working as designed.\r\n\r\n### Background\r\n\r\n\r\nIn a nutshell, [`watchQuery`](https://www.apollographql.com/docs/react/api/apollo-client.html#ApolloClient.watchQuery) was designed to address the following:\r\n\r\n> ... suppose you call watchQuery on a GraphQL query that fetches an person's first name and last name and this person has a particular object identifer, provided by dataIdFromObject. Later, a different query fetches that same person's first and last name and his/her first name has now changed. Then, any observers associated with the results of the first query will be updated with a new result object. \r\n\r\nIt does this by watching for changes made in the Apollo Client store. The example listed in https://github.com/apollographql/apollo-client/issues/2712#issue-281085838 is using `watchQuery` a bit differently than the above. It's not only expecting the `ObservableQuery` to be notified of store changes (ie. when a new fetch is run with new variables, and new data is added to the store), but it's also expecting to be notified when there are no changes made to the store (ie. using `setVariables` with variables that have already been used before, and have a matching entry in the store). `setVariables` doesn't notify subscribers when an existing match is found in the store, as nothing new has been added/updated in the store. It's assuming that whatever is watching for store data changes, really only wants to be notified when there are actual store data changes. So another way of looking at this is that `watchQuery` is intended to be used to watch for store changes, whereas the example in https://github.com/apollographql/apollo-client/issues/2712#issue-281085838 is expecting `watchQuery` to be usable for watching for query result changes (which may or may not come from the store, might have changed, might not have changed, etc.).\r\n\r\nWhat has definitely added to the confusion here, is the fact that `setVariables` was not really intended to be part of `apollo-client`'s public API. The fact that it's exposed in the docs is kinda a bug; it's publicly accessible to make it easier for developers who are working on Apollo Client integration packages, that reuse/extend existing AC functionality. Actually, most of the `ObservableQuery` functions listed in the API docs are not really meant to be used directly - this definitely needs to be fixed (:hwillson ties string around finger:). \r\n\r\n`ObservableQuery` has a solution for handling situations like https://github.com/apollographql/apollo-client/issues/2712#issue-281085838, that is intended to be public - [`ObservableQuery.refetch`](https://www.apollographql.com/docs/react/api/apollo-client.html#ObservableQuery.refetch). The idea behind `refetch` is that if your variables have changed and you want to fetch data using those variables, you can pass them into `refetch(variables)`, have your data fetched, then saved back in the store, which then means your observers will be notified of the newly updated store data. Yes, `refectch` has one main drawback - it will always make a network request. So if you're refetching with variables that are already associated with data saved in the store, you'll be ignoring the store and refetching anyways. This can be worked around however by checking the cache first to see if a match exists, before firing the `refetch`.\r\n\r\nSo long story longer, `setVariables` is actually working as designed. We're definitely open to changing its behavior, but this is something that would need to be planned for a future major release. I'll add the discussion tag here to keep this issue open, and will be tracking all comments/suggestions/complaints/etc. Thanks all!"}}
{"actor":{"display_login":"hwillson"}}