You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+47-3Lines changed: 47 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,36 @@
1
-
# GraphQLTransportWS
1
+
# GraphQLTransportWS-DataSync
2
2
3
-
This implements the [graphql-transport-ws WebSocket subprotocol](https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md).
4
-
It is mainly intended for server support, but there is a basic client implementation included.
3
+
This implements the [graphql-transport-ws WebSocket subprotocol](https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md), with the additional capability to send `GraphQLRequests` from the Client to the Server over the same websocket to support the PassiveLogic DataSync spec.
4
+
5
+
It is mainly intended for server support, but there is a client implementation included for swift projects.
5
6
6
7
Features:
7
8
- Server implementation that implements defined protocol conversations
8
9
- Client and Server types that wrap messengers
9
10
- Codable Server and Client message structures
10
11
- Custom authentication support
11
12
13
+
## DataSync Additions
14
+
This DataSync fork allows for `Next` messages to be handled by the Server with a custom `onNext` callback exposed. The server will detect incoming `Next` frames that contain `subscribe` requests and reject them with an error to avoid nesting subscriptions, but will allow handling logic for `query` and `mutation` requests.
15
+
16
+
The Client implemeneted here now also supports sending `Next` messages to the server via adding an `Observable` object, which will automatically wrap and send `Next` frames as it updates.
17
+
18
+
### Example
19
+
*The client and the server has already gone through [successful connection initialisation.](https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md#successful-connection-initialisation)*
20
+
1.*Client* generates a unique ID for the following operation
21
+
2.*Client* dispatches the `Subscribe` message with the generated ID through the `id` field and the requested operation passed through the `payload` field
22
+
*All future communication is linked through this unique ID*
23
+
3.*Server* executes the streaming GraphQL operation
24
+
4.*Server* checks if the generated ID is unique across active streaming subscriptions
25
+
- If not unique, the server will close the socket with the event `4409: Subscriber for <generated-id> already exists`
26
+
If unique, continue...
27
+
5.*Server* optionally checks if the operation is valid before starting executing it, e.g. checking permissions
28
+
- If not valid, the server sends an `Error` message and deems the operation complete.
29
+
If valid, continue...
30
+
6.*Server* & *Client* dispatch results over time with the `Next` message
31
+
-*Server* & *Client* handle received `Next` messages with their respectively defined callbacks. All Context is assumed to be the same between the two and is established in step 3 when the subscription is created.
32
+
7. The operation completes as described in [the `graphql-transport-ws` protocol](https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md#streaming-operation)
33
+
12
34
## Usage
13
35
14
36
To use this package, include it in your `Package.swift` dependencies:
@@ -89,6 +111,28 @@ routes.webSocket(
89
111
)
90
112
```
91
113
114
+
You can also Create a client and add an observable callback to send data back to the server:
115
+
```swift
116
+
let messenger =WebSocketMessenger(websocket: websocket)
117
+
let client = Client<EmptyInitPayload>(messenger: messenger)
118
+
119
+
// add an observable to the client that will automatically send `Next` frames to the server as it updates
120
+
client.addObservable(observable:
121
+
Observable.create { observer in
122
+
// Some simple obsererver that fulfills every future with the same query. Your observer can be much more complex.
0 commit comments