@@ -57,12 +57,9 @@ The result of this query is a `GraphQLResult` that encodes to the following JSON
5757
5858### Subscription
5959
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:
6663
6764``` swift
6865let schema = try GraphQLSchema (
@@ -71,16 +68,16 @@ let schema = try GraphQLSchema(
7168 fields : [
7269 " hello" : GraphQLField (
7370 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
7572 return eventResult
7673 },
77- subscribe : { _ , _ , _ , _ , _ in // Defines how to construct the event stream
74+ subscribe : { _ , _ , _ , _ in // Defines how to construct the event stream
7875 return AsyncThrowingStream< String , Error > { continuation in
7976 let timer = Timer.scheduledTimer (
8077 withTimeInterval : 3 ,
8178 repeats : true ,
8279 ) {
83- continuation.yield (" world" ) // Emits "world" every 3 seconds
80+ continuation.yield (" world" ) // Emits "world" every 3 seconds
8481 }
8582 }
8683 }
@@ -96,7 +93,8 @@ To execute a subscription use the `graphqlSubscribe` function:
9693let subscriptionResult = try await graphqlSubscribe (
9794 schema : schema,
9895)
99- for try await result in concurrentStream.stream {
96+ let stream = subscriptionResult.get ()
97+ for try await result in stream {
10098 print (result)
10199}
102100```
0 commit comments