question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ApolloClient: ObservableQuery.map/.filter/.reduce/.flatMap don't work

See original GitHub issue

Intended outcome: I’d like to use the basic zen-observable methods like map and filter to pipe the results of a query through some transformations before subscribing to it.

apolloClient.watchQuery({ query })
  .map(result => transformResult(result))
  .subscribe(console.log);

The docs say “Refer to the zen-observable documentation for additional context and API options.” — I read that as the standard zen-observable API should be available and cooperate with the ObservableQuery API.

Actual outcome:

Calling any of the above methods cause runtime errors that look like:

/sandbox/node_modules/apollo-client/bundle.umd.js:146
      _this.variables = options.variables || {};
                                ^

TypeError: Cannot read property 'variables' of undefined

The actual line of code where the error occurs is here.

How to reproduce the issue:

Reproduction: https://codesandbox.io/embed/apollo-client-observable-query-map-509cw

zen-observable seems to support subclassing, but it assumes that the subclass constructor has the same signature. This creates two problems:

  1. When .map creates a new ObservableQuery, it doesn’t know about the constructor’s queryManager, options, and shouldSubscribe arguments.
  2. Even after I hacked in support for new ObservableQuery(fn), the new observable returned from .map doesn’t have a query manager so ObservableQuery’s methods like refetch do not work.

Versions

apollo-client@2.6.4

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:10
  • Comments:9

github_iconTop GitHub Comments

3reactions
jhamptoncommented, Apr 8, 2020

@hwillson Do we have a sense if this might be address in AC3?

2reactions
oscar6echocommented, Apr 2, 2020

What works for me:

import Observable from 'zen-observable';
// client built from from 'apollo-client' not 'apollo-boost';

const obs = client.subscribe({
    query: gql(queryString),
    variables,
    ...opts
  });
  const obs2 = Observable.from(obs).map((e) => transform(e));
  obs2.subscribe({
    next,
    error,
    complete
  });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - ApolloClient: ObservableQuery.map/.filter/.reduce ...
ApolloClient : ObservableQuery.map/.filter/.reduce/.flatMap don't work. ... I'd like to use the basic zen-observable methods like map and filter to pipe the ...
Read more >
ObservableQuery - Apollo GraphQL Docs
Update the variables of this observable query, and fetch the new results if they've changed. If you want to force new results, use...
Read more >
Advanced topics on caching in Apollo Client
This article describes special cases and considerations when using the Apollo Client cache. Bypassing the cache. Sometimes you shouldn't use the cache for...
Read more >
Refetching queries in Apollo Client - Apollo GraphQL Docs
include (or both). If onQueryUpdated is not provided, the default implementation returns the result of calling observableQuery.refetch() . When onQueryUpdated ...
Read more >
Reading and writing data to the cache - Apollo GraphQL Docs
Apollo Client automatically queries for every object's __typename by default, even if you don't include this field in your query string. Do not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found