Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 79
Breaking: Converts to Swift Concurrency#166
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
24080b6d112be09ec313a9b4a6f21bc642cd25254b631650a34268d3e1c66d90d90d042cbc8b9a348434bc807e35c1e0bebf8b9424f01de2dfa0a600522fb64424f8e210fb72File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -45,8 +45,7 @@ Once a schema has been defined queries may be executed against it using the glob | ||
| ```swift | ||
| let result = try await graphql( | ||
| schema: schema, | ||
| request: "{ hello }", | ||
| eventLoopGroup: eventLoopGroup | ||
| request: "{ hello }" | ||
| ) | ||
| ``` | ||
| @@ -58,33 +57,29 @@ The result of this query is a `GraphQLResult` that encodes to the following JSON | ||
| ### Subscription | ||
| This package supports GraphQL subscription, but until the integration of `AsyncSequence` in Swift 5.5 the standard Swift library did not | ||
| provide an event-stream construct. For historical reasons and backwards compatibility, this library implements subscriptions using an | ||
| `EventStream` protocol that nearly every asynchronous stream implementation can conform to. | ||
| To create a subscription field in a GraphQL schema, use the `subscribe` resolver that returns an `EventStream`. You must also provide a | ||
| `resolver`, which defines how to process each event as it occurs and must return the field result type. Here is an example: | ||
| This package supports GraphQL subscription. To create a subscription field in a GraphQL schema, use the `subscribe` | ||
| resolver that returns any type that conforms to `AsyncSequence`. You must also provide a `resolver`, which defines how | ||
| to process each event as it occurs and must return the field result type. Here is an example: | ||
| ```swift | ||
| let schema = try GraphQLSchema( | ||
| subscribe: GraphQLObjectType( | ||
| name: "Subscribe", | ||
| fields: [ | ||
| "hello": GraphQLField( | ||
| "hello": GraphQLField( | ||
| type: GraphQLString, | ||
| resolve: { eventResult, _, _, _, _ in // Defines how to transform each event when it occurs | ||
| resolve: { eventResult, _, _, _ in // Defines how to transform each event when it occurs | ||
| return eventResult | ||
| }, | ||
| subscribe: { _, _, _, _, _ in // Defines how to construct the event stream | ||
| let asyncStream = AsyncThrowingStream<String, Error> { continuation in | ||
| subscribe: { _, _, _, _ in // Defines how to construct the event stream | ||
| return AsyncThrowingStream<String, Error> { continuation in | ||
NeedleInAJayStack marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| let timer = Timer.scheduledTimer( | ||
| withTimeInterval: 3, | ||
| repeats: true, | ||
| ) { | ||
| continuation.yield("world") // Emits "world" every 3 seconds | ||
| continuation.yield("world") // Emits "world" every 3 seconds | ||
| } | ||
| } | ||
| return ConcurrentEventStream<String>(asyncStream) | ||
| } | ||
| ) | ||
| ] | ||
| @@ -98,9 +93,8 @@ To execute a subscription use the `graphqlSubscribe` function: | ||
| let subscriptionResult = try await graphqlSubscribe( | ||
| schema: schema, | ||
| ) | ||
| // Must downcast from EventStream to concrete type to use in 'for await' loop below | ||
| let concurrentStream = subscriptionResult.stream! as! ConcurrentEventStream | ||
| for try await result in concurrentStream.stream { | ||
| let stream = subscriptionResult.get() | ||
| for try await result in stream { | ||
| print(result) | ||
| } | ||
| ``` | ||
| @@ -111,18 +105,15 @@ The code above will print the following JSON every 3 seconds: | ||
| { "hello": "world" } | ||
| ``` | ||
| The example above assumes that your environment has access to Swift Concurrency. If that is not the case, try using | ||
| [GraphQLRxSwift](https://github.com/GraphQLSwift/GraphQLRxSwift) | ||
| ## Encoding Results | ||
| If you encode a `GraphQLResult` with an ordinary `JSONEncoder`, there are no guarantees that the field order will match the query, | ||
| If you encode a `GraphQLResult` with an ordinary `JSONEncoder`, there are no guarantees that the field order will match the query, | ||
| violating the [GraphQL spec](https://spec.graphql.org/June2018/#sec-Serialized-Map-Ordering). To preserve this order, `GraphQLResult` | ||
| should be encoded using the `GraphQLJSONEncoder` provided by this package. | ||
| ## Support | ||
| This package supports Swift versions in [alignment with Swift NIO](https://github.com/apple/swift-nio?tab=readme-ov-file#swift-versions). | ||
| This package aims to support the previous three Swift versions. | ||
| For details on upgrading to new major versions, see [MIGRATION](MIGRATION.md). | ||
| @@ -140,7 +131,7 @@ To format your code, install `swiftformat` and run: | ||
| ```bash | ||
| swiftformat . | ||
| ``` | ||
| ``` | ||
| Most of this repo mirrors the structure of | ||
| (the canonical GraphQL implementation written in Javascript/Typescript)[https://github.com/graphql/graphql-js]. If there is any feature | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.