-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Discussion] - Real-time data in Relay using the Mutations API #1166
Comments
Yup! At a high level, what you're describing is the approach that we are pursuing with GraphQL subscriptions. Relay is designed to process subscription payloads the same as mutation payloads: given the mutation/subscription query, config, and payload, it correctly updates the cache and notifies affected components. I wrote a brief note about how to implement real-time updates today using (mostly-public) APIs. Basically, your server can push mutation payloads to the client, and you can manually apply them to the store. I'd encourage you to try out this approach and let us know how it works. Also see #559, which has more context on how we're thinking about the future of the API: more low-level primitives that you can use in different ways. |
re the "cons":
|
Thanks for the clarification @josephsavona! If the subscription API is the same as the mutation API, then why a separate Subscription Type just for the sake of it? Why not something like Relay.Store.commitUpdateLocal(Mutation, Payload)? This way, you can reuse your mutations when data is pushed from the server as well as support client state if you wanted to right? |
Closing as this is possible today, and will be covered by #559: we're working toward a new core API that will allow "writing" data to the local store. |
The community has discussed many potential approaches to handling push data with Relay. One of the more complex aspects of this problem seems to be the fact that a change in data can have multiple / varying effects on the UI. Think the like count problem. While this is addressed by mutations in a synchronous request-response model, there isn't a lot of clarity around how subscriptions will fit in.
One approach I've been thinking of is to use the mutations API to achieve this but I wanted to get some feedback to make sure it's not a completely crazy idea.
Here goes: Given any change in the graph is essentially a mutation, what if GraphQL published the payload (along with meta-data about the client side mutation) to a pub-sub service in addition to returning the payload to the original client?
When a subscriber receives a message with the mutation payload, it can call Relay.commitUpdate with a localPayload prop which will tell your custom network layer to skip the network request and resolve the mutation locally with the payload received from the pub-sub service.
High level architecture:
The benefits of this approach could be (assuming I'm not missing any major complexities):
Some cons I can think of:
Would be great to get your feedback on whether this is an avenue worth pursuing!
The text was updated successfully, but these errors were encountered: