Observables instead of Promises
DxCx opened this issue ยท 9 comments
Hi guys,
As more I'm thinking about it the more i think it should be done,
So before I'm submitting a PR i would like to hear your thoughts about it.
So basically i want to add support for Observables (RxJs? Any other preferred library?) so resolver function will be able to return Observables as well (Observable | Promise | any)
Then create another execute function that will return Observable while chaining the original execute to call it with Observable.toPromise()
function.
This way GraphQLResult can be reactive and this can be really useful for subscriptions and later in the future @live
\ @stream
\ @defer
directives..
For the initial implementation I'm planing to always take(1).. later make it fully reactive..
So, what do you think?
I think @defer
and @stream
could also benefit from observables.
It will allow us to use the sweet power of observables in the server as well as the client.
Excellent proposition @DxCx!
Amazing idea. I think it will open a door for far better architecture and maintainability.
Further more observables natural for reactivity or pub/sub and they are by far the best way I currently know to manipulate data.
Was just able to confirm that my idea works pretty well with subscription. (on unit test)
However the idea is simple:
- graphqlReactive should be used instead of graphql
- an hot Observable (Or subject) is being saved in context object.
- subscription resolver is being triggered when subscription doc is sent.
- subscription resolver should return the hot observable from context mixed with any operator
that is needed because of arguments.
- subscription resolver should return the hot observable from context mixed with any operator
- graphqlReactive returns an observable of GraphQLResult which is updated for each change.
Closing this issue. Observables are exciting but do not yet have clear behavior relative to the GraphQL spec. Since this repository stands to be a production server and reference implementation, I don't think it's the right place to experiment with this.
I'm looking forward to seeing this sort of experimentation happen in additional libraries that don't need to rely on spec compliance. See more comments on this in #502.
Also just a note that the versions of @defer
and @stream
I've prototyped in Facebook's server have not required Observable, so we should be able to still pursue those ideas without this approach.
for everyone that is interested in this,
I've just release graphql-rxjs
package which adds Observable support to graphql engine:
https://www.npmjs.com/package/graphql-rxjs
Enjoy ๐
lol thanks @DxCx ๐