@@ -57,12 +57,9 @@ The result of this query is a `GraphQLResult` that encodes to the following JSON
57
57
58
58
### Subscription
59
59
60
- This package supports GraphQL subscription, but until the integration of ` AsyncSequence ` in Swift 5.5 the standard Swift library did not
61
- provide an event-stream construct. For historical reasons and backwards compatibility, this library implements subscriptions using an
62
- ` EventStream ` protocol that nearly every asynchronous stream implementation can conform to.
63
-
64
- To create a subscription field in a GraphQL schema, use the ` subscribe ` resolver that returns an ` EventStream ` . You must also provide a
65
- ` resolver ` , which defines how to process each event as it occurs and must return the field result type. Here is an example:
60
+ This package supports GraphQL subscription. To create a subscription field in a GraphQL schema, use the ` subscribe `
61
+ resolver that returns any type that conforms to ` AsyncSequence ` . You must also provide a ` resolver ` , which defines how
62
+ to process each event as it occurs and must return the field result type. Here is an example:
66
63
67
64
``` swift
68
65
let schema = try GraphQLSchema (
@@ -71,16 +68,16 @@ let schema = try GraphQLSchema(
71
68
fields : [
72
69
" hello" : GraphQLField (
73
70
type : GraphQLString,
74
- resolve : { eventResult, _ , _ , _ , _ in // Defines how to transform each event when it occurs
71
+ resolve : { eventResult, _ , _ , _ in // Defines how to transform each event when it occurs
75
72
return eventResult
76
73
},
77
- subscribe : { _ , _ , _ , _ , _ in // Defines how to construct the event stream
74
+ subscribe : { _ , _ , _ , _ in // Defines how to construct the event stream
78
75
return AsyncThrowingStream< String , Error > { continuation in
79
76
let timer = Timer.scheduledTimer (
80
77
withTimeInterval : 3 ,
81
78
repeats : true ,
82
79
) {
83
- continuation.yield (" world" ) // Emits "world" every 3 seconds
80
+ continuation.yield (" world" ) // Emits "world" every 3 seconds
84
81
}
85
82
}
86
83
}
@@ -96,7 +93,8 @@ To execute a subscription use the `graphqlSubscribe` function:
96
93
let subscriptionResult = try await graphqlSubscribe (
97
94
schema : schema,
98
95
)
99
- for try await result in concurrentStream.stream {
96
+ let stream = subscriptionResult.get ()
97
+ for try await result in stream {
100
98
print (result)
101
99
}
102
100
```
0 commit comments