Skip to content

Swift Concurrency Support #78

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.library(name: "Graphiti", targets: ["Graphiti"]),
],
dependencies: [
.package(url: "https://github.com/GraphQLSwift/GraphQL.git", .upToNextMajor(from: "2.0.0"))
.package(url: "https://github.com/GraphQLSwift/GraphQL.git", from: "2.4.0")
],
targets: [
.target(name: "Graphiti", dependencies: ["GraphQL"]),
Expand Down
43 changes: 43 additions & 0 deletions Sources/Graphiti/API/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,46 @@ extension API {
)
}
}


#if compiler(>=5.5) && canImport(_Concurrency)

extension API {
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
public func execute(
request: String,
context: ContextType,
on eventLoopGroup: EventLoopGroup,
variables: [String: Map] = [:],
operationName: String? = nil
) async throws -> GraphQLResult {
return try await schema.execute(
request: request,
resolver: resolver,
context: context,
eventLoopGroup: eventLoopGroup,
variables: variables,
operationName: operationName
).get()
}

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
public func subscribe(
request: String,
context: ContextType,
on eventLoopGroup: EventLoopGroup,
variables: [String: Map] = [:],
operationName: String? = nil
) async throws -> SubscriptionResult {
return try await schema.subscribe(
request: request,
resolver: resolver,
context: context,
eventLoopGroup: eventLoopGroup,
variables: variables,
operationName: operationName
).get()
}
}

#endif
69 changes: 69 additions & 0 deletions Sources/Graphiti/Field/Field/Field.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,72 @@ public extension Field where Arguments == NoArguments {
self.init(name: name, arguments: [], syncResolve: syncResolve)
}
}

#if compiler(>=5.5) && canImport(_Concurrency)

public extension Field {

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
convenience init<ResolveType>(
name: String,
arguments: [ArgumentComponent<Arguments>],
concurrentResolve: @escaping ConcurrentResolve<ObjectType, Context, Arguments, ResolveType>
) {
let asyncResolve: AsyncResolve<ObjectType, Context, Arguments, ResolveType> = { type in
{ context, arguments, eventLoopGroup in
let promise = eventLoopGroup.next().makePromise(of: ResolveType.self)
promise.completeWithTask {
try await concurrentResolve(type)(context, arguments)
}
return promise.futureResult
}
}
self.init(name: name, arguments: arguments, asyncResolve: asyncResolve)
}
}

// MARK: ConcurrentResolve Initializers

public extension Field where FieldType : Encodable {
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
convenience init(
_ name: String,
at function: @escaping ConcurrentResolve<ObjectType, Context, Arguments, FieldType>,
@ArgumentComponentBuilder<Arguments> _ argument: () -> ArgumentComponent<Arguments>
) {
self.init(name: name, arguments: [argument()], concurrentResolve: function)
}

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
convenience init(
_ name: String,
at function: @escaping ConcurrentResolve<ObjectType, Context, Arguments, FieldType>,
@ArgumentComponentBuilder<Arguments> _ arguments: () -> [ArgumentComponent<Arguments>] = {[]}
) {
self.init(name: name, arguments: arguments(), concurrentResolve: function)
}
}

public extension Field {
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
convenience init<ResolveType>(
_ name: String,
at function: @escaping ConcurrentResolve<ObjectType, Context, Arguments, ResolveType>,
as: FieldType.Type,
@ArgumentComponentBuilder<Arguments> _ argument: () -> ArgumentComponent<Arguments>
) {
self.init(name: name, arguments: [argument()], concurrentResolve: function)
}

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
convenience init<ResolveType>(
_ name: String,
at function: @escaping ConcurrentResolve<ObjectType, Context, Arguments, ResolveType>,
as: FieldType.Type,
@ArgumentComponentBuilder<Arguments> _ arguments: () -> [ArgumentComponent<Arguments>] = {[]}
) {
self.init(name: name, arguments: arguments(), concurrentResolve: function)
}
}

#endif
13 changes: 13 additions & 0 deletions Sources/Graphiti/Field/Resolve/ConcurrentResolve.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import NIO

#if compiler(>=5.5) && canImport(_Concurrency)

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
public typealias ConcurrentResolve<ObjectType, Context, Arguments, ResolveType> = (
_ object: ObjectType
) -> (
_ context: Context,
_ arguments: Arguments
) async throws -> ResolveType

#endif
103 changes: 103 additions & 0 deletions Sources/Graphiti/Subscription/SubscribeField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,109 @@ public extension SubscriptionField {
}
}

#if compiler(>=5.5) && canImport(_Concurrency)

public extension SubscriptionField {

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
convenience init<ResolveType>(
name: String,
arguments: [ArgumentComponent<Arguments>],
concurrentResolve: @escaping ConcurrentResolve<SourceEventType, Context, Arguments, ResolveType>,
concurrentSubscribe: @escaping ConcurrentResolve<ObjectType, Context, Arguments, EventStream<SourceEventType>>
) {
let asyncResolve: AsyncResolve<SourceEventType, Context, Arguments, ResolveType> = { type in
{ context, arguments, eventLoopGroup in
let promise = eventLoopGroup.next().makePromise(of: ResolveType.self)
promise.completeWithTask {
try await concurrentResolve(type)(context, arguments)
}
return promise.futureResult
}
}
let asyncSubscribe: AsyncResolve<ObjectType, Context, Arguments, EventStream<SourceEventType>> = { type in
{ context, arguments, eventLoopGroup in
let promise = eventLoopGroup.next().makePromise(of: EventStream<SourceEventType>.self)
promise.completeWithTask {
try await concurrentSubscribe(type)(context, arguments)
}
return promise.futureResult
}
}
self.init(name: name, arguments: arguments, asyncResolve: asyncResolve, asyncSubscribe: asyncSubscribe)
}

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
convenience init(
name: String,
arguments: [ArgumentComponent<Arguments>],
as: FieldType.Type,
concurrentSubscribe: @escaping ConcurrentResolve<ObjectType, Context, Arguments, EventStream<SourceEventType>>
) {
let asyncSubscribe: AsyncResolve<ObjectType, Context, Arguments, EventStream<SourceEventType>> = { type in
{ context, arguments, eventLoopGroup in
let promise = eventLoopGroup.next().makePromise(of: EventStream<SourceEventType>.self)
promise.completeWithTask {
try await concurrentSubscribe(type)(context, arguments)
}
return promise.futureResult
}
}
self.init(name: name, arguments: arguments, as: `as`, asyncSubscribe: asyncSubscribe)
}
}

// MARK: ConcurrentResolve Initializers

public extension SubscriptionField where FieldType : Encodable {
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
convenience init(
_ name: String,
at function: @escaping ConcurrentResolve<SourceEventType, Context, Arguments, FieldType>,
atSub subFunc: @escaping ConcurrentResolve<ObjectType, Context, Arguments, EventStream<SourceEventType>>,
@ArgumentComponentBuilder<Arguments> _ argument: () -> ArgumentComponent<Arguments>
) {
self.init(name: name, arguments: [argument()], concurrentResolve: function, concurrentSubscribe: subFunc)
}

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
convenience init(
_ name: String,
at function: @escaping ConcurrentResolve<SourceEventType, Context, Arguments, FieldType>,
atSub subFunc: @escaping ConcurrentResolve<ObjectType, Context, Arguments, EventStream<SourceEventType>>,
@ArgumentComponentBuilder<Arguments> _ arguments: () -> [ArgumentComponent<Arguments>] = {[]}
) {
self.init(name: name, arguments: arguments(), concurrentResolve: function, concurrentSubscribe: subFunc)
}
}

public extension SubscriptionField {
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
convenience init<ResolveType>(
_ name: String,
at function: @escaping ConcurrentResolve<SourceEventType, Context, Arguments, ResolveType>,
as: FieldType.Type,
atSub subFunc: @escaping ConcurrentResolve<ObjectType, Context, Arguments, EventStream<SourceEventType>>,
@ArgumentComponentBuilder<Arguments> _ argument: () -> ArgumentComponent<Arguments>
) {
self.init(name: name, arguments: [argument()], concurrentResolve: function, concurrentSubscribe: subFunc)
}

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
convenience init<ResolveType>(
_ name: String,
at function: @escaping ConcurrentResolve<SourceEventType, Context, Arguments, ResolveType>,
as: FieldType.Type,
atSub subFunc: @escaping ConcurrentResolve<ObjectType, Context, Arguments, EventStream<SourceEventType>>,
@ArgumentComponentBuilder<Arguments> _ arguments: () -> [ArgumentComponent<Arguments>] = {[]}
) {
self.init(name: name, arguments: arguments(), concurrentResolve: function, concurrentSubscribe: subFunc)
}
}

#endif


// TODO Determine if we can use keypaths to initialize

// MARK: Keypath Initializers
Expand Down
Loading