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.

Executing a graphqlOperation only returns a Promise and not Observabe

See original GitHub issue

Describe the bug Looking at the type definition of API.graphql I see that it either returns Promise<GraphQLResult> or Observable<object>.

However, when I look at the code returned from a query I see a Promise: image

Now I always have to do a ugly promise conversion, for example:

 return from<Promise<{ data: { getPosts: GetPostQuery[] } }>>(API.graphql(
      graphqlOperation(statement, args))
    )
      .pipe(
        map(response => response.data.getPosts)
      ) as Observable<GetPostQuery[]>;

Any chance Observable (RXJS and not Zen) can be used?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
rob3ccommented, Sep 9, 2020

I agree with the OP that rxjs should replace zen for the observable implementation 👍

While it may seem obvious that promises should be used for requests that only return single values and observables should be used for those that stream multiple values over time, that’s not necessarily the case.

The reason that libraries like angular use observables instead of promises for even simple http requests that will only every return single values (like the queries and mutations mentioned above), is that they make canceling the requests trivial. This also normally aborts the http request’s connection in addition to just leaving it alive and ignoring the result, which is a nice optimization to get for free. Standard promise implementations do not support cancellation, which makes things like typeahead searches, request timeouts and other request-related logic much harder to implement efficiently.

0reactions
mattiLeBlanccommented, Jul 24, 2019

It would also be nice to support generics <T> so that we can set our return type.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ObservableQuery - Apollo GraphQL Docs
Note: the promise will return null immediately if the query is not active (there are no subscribers). Arguments. Name / Type, Description. variables....
Read more >
API (GraphQL) - Fetch data - JavaScript - AWS Amplify Docs
Import the generated query and execute it with API.graphql : ... The TypeScript signature of API.graphql returns a Promise | Observable .
Read more >
angular - Property 'subscribe' does not exist on type 'Promise ...
As the result from API.graphql() can be both, a Promise when you query data for immediate return and alternatively an Observable for ...
Read more >
graphiql - npm
Start using graphiql in your project by running `npm i graphiql`. ... GraphQL-HTTP parameters and returns a Promise or Observable which ...
Read more >
RealTime ReasonML Subscriptions on AWS with Wonka
Self, that is just few lines of code. ... graphqlOperation) => Js.Promise.t(Types. ... returns a promise which is not what we want here....
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