Query, Mutate possible over websockets?

Paul,

Subscribing to a mutation does not make sense. You subscribe to changes in a database. A mutation is made to be a one time transaction, so you use a Promise. You change something, and get the results back from that re-fetch query immediately. There is no such thing as a mutation that would keep changing. A normal query is used in the same manor. You send the query to the api using a promise, and you get back the results immediately. Like the REST api, sending a request to your api endpoint using some kind of fetch, will return your data immediately one time. This is just how fetch works, specifically using a graphql endpoint with the http (https for secure) protocol adhering to the graphql spec.

Subscribing to a query, however, uses the ws (wss for secure) protocol to create a TCP connection. There is no such thing as a subscribable mutation. In fact, you subscribe to a query via the GraphQl spec by literally leaving the query the same, and replacing the word “query” with the word “subscription.” Any changes in your query will be resent to your secure client in real time. Because this is a feed of data, you use observables instead of promises on your client to receive the data. URQL uses WONKA, but you could also use RXJS. This depends on your framework and use case, and they can be translated using the standard Observable spec.

In sum, you cannot subscribe to a mutation, as that is not how neither WS protocol, nor GraphQL protocol work. It also does not make sense.


It should also be noted that there are two known implementations of the Websocket Protocol:

DGraph still uses the OLD implementation of Subscriptions that is no longer being maintained. The creator of graphql-ws has put in a feature request for Dgraph to change to graphql over websocket, but it is something that was never added to the feature list. Considering it contains security issues, I think it is more of a security problem than a feature. However, it is worth noting that it MAY (I am not sure) break the current websocket implementations that DGraph uses.

J

1 Like