Skip to content

Commit

Permalink
Merge pull request #52 from NeedleInAJayStack/subscription
Browse files Browse the repository at this point in the history
Subscription
  • Loading branch information
paulofaria authored Apr 16, 2021
2 parents 35cf75f + 9813c75 commit 93fcea3
Show file tree
Hide file tree
Showing 9 changed files with 536 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"repositoryURL": "https://github.com/GraphQLSwift/GraphQL.git",
"state": {
"branch": null,
"revision": "69f2cd4835ff60b5fae841a680abdc1d56592dd4",
"version": "1.1.7"
"revision": "abf41c20c79331444ee3a290373d7c647c244383",
"version": "1.2.0"
}
},
{
Expand Down
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: "1.1.8")),
.package(url: "https://github.com/GraphQLSwift/GraphQL.git", .upToNextMajor(from: "1.2.0"))
],
targets: [
.target(name: "Graphiti", dependencies: ["GraphQL"]),
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ struct Resolver {
}
```

#### Subscription

This library supports GraphQL subscriptions. To use them, you must create a concrete subclass of the `EventStream` class that implements event streaming
functionality.

If you don't feel like creating a subclass yourself, you can use the [GraphQLRxSwift](https://github.com/GraphQLSwift/GraphQLRxSwift) repository
to integrate [RxSwift](https://github.com/ReactiveX/RxSwift) observables out-of-the-box. Or you can use that repository as a reference to connect a different
stream library like [ReactiveSwift](https://github.com/ReactiveCocoa/ReactiveSwift), [OpenCombine](https://github.com/OpenCombine/OpenCombine), or
one that you've created yourself.

## Star Wars API example

Check the [Star Wars API](Tests/GraphitiTests/StarWarsAPI/StarWarsAPI.swift) for a more complete example.
Expand Down
17 changes: 17 additions & 0 deletions Sources/Graphiti/API/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,21 @@ extension API {
operationName: operationName
)
}

public func subscribe(
request: String,
context: ContextType,
on eventLoopGroup: EventLoopGroup,
variables: [String: Map] = [:],
operationName: String? = nil
) -> EventLoopFuture<SubscriptionResult> {
return schema.subscribe(
request: request,
resolver: resolver,
context: context,
eventLoopGroup: eventLoopGroup,
variables: variables,
operationName: operationName
)
}
}
23 changes: 23 additions & 0 deletions Sources/Graphiti/Schema/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,27 @@ public extension Schema {
return eventLoopGroup.next().makeFailedFuture(error)
}
}

func subscribe(
request: String,
resolver: Resolver,
context: Context,
eventLoopGroup: EventLoopGroup,
variables: [String: Map] = [:],
operationName: String? = nil
) -> EventLoopFuture<SubscriptionResult> {
do {
return try graphqlSubscribe(
schema: schema,
request: request,
rootValue: resolver,
context: context,
eventLoopGroup: eventLoopGroup,
variableValues: variables,
operationName: operationName
)
} catch {
return eventLoopGroup.next().makeFailedFuture(error)
}
}
}
Loading

0 comments on commit 93fcea3

Please sign in to comment.