Executing a graphqlOperation only returns a Promise and not Observabe
See original GitHub issueDescribe 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:
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:
- Created 4 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
It would also be nice to support generics
<T>
so that we can set our return type.