Skip to content
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

Add subscription support #433

Merged
merged 343 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from 250 commits
Commits
Show all changes
343 commits
Select commit Hold shift + click to select a range
ab90737
Push errors to executor in `resolve_selection_set_into_iter`
nWacky Oct 22, 2019
2f04ea4
Change 'Arc' to 'Rc' in synchronous context
nWacky Oct 22, 2019
58f8cba
Push errors to executor in `resolve_selection_set_into_stream`
nWacky Oct 22, 2019
cf3b8e2
Comment out all tests, create new test module for subscriptions
nWacky Oct 22, 2019
6b6f7c4
Fix subscription macro
nWacky Oct 22, 2019
4408dcc
Add synchronous test for subscriptions, fix errors in macros, minor r…
nWacky Oct 22, 2019
0434559
Add async subscriptions test
nWacky Oct 23, 2019
40533d8
Add more sync subscription tests, encapsulate sync and async tests to…
nWacky Oct 23, 2019
c0202fb
Encapsulate async subscription creation logic
nWacky Oct 23, 2019
e9b08d5
Format
nWacky Oct 23, 2019
b95191f
Add `into_iter` for `IteratorGraphQLResponse`
nWacky Oct 24, 2019
9794489
Update playground with `into_iter`
nWacky Oct 24, 2019
ca3f321
Tried implementing `into_stream` with poll_next
nWacky Oct 24, 2019
67e0884
Implemented `into_stream`
nWacky Oct 24, 2019
b72cba7
Add `into_stream` to playground, fix bug
nWacky Oct 24, 2019
babff0a
Format
nWacky Oct 24, 2019
18d928c
Add docs, refactor
nWacky Oct 24, 2019
72fcdc4
Add docs to executor_wrappers
nWacky Oct 24, 2019
45194fb
Format [skip ci]
nWacky Oct 25, 2019
2b51dee
Fix warnings in juniper [skip ci]
nWacky Oct 25, 2019
e7ad2c5
Fix compilation error without "async" feature [skip ci]
nWacky Oct 25, 2019
fd6c0ff
Rebase onto `async-await`
nWacky Oct 29, 2019
e577b60
Tests pass [skip ci]
nWacky Oct 29, 2019
430487b
Merge branch 'async-await-subscriptions' of https://github.com/instru…
nWacky Oct 29, 2019
a08b379
Fix rocket playground compilation error [skip ci]
nWacky Oct 29, 2019
3572178
Rebase onto `async_await`
nWacky Oct 30, 2019
5145a8a
Fix error with not displayed results in GraphQL Playground
nWacky Oct 30, 2019
c867b57
Stop returning values after stop signal received
nWacky Oct 30, 2019
109e5a8
Stop stream after stop signal received
nWacky Oct 30, 2019
3bc69ec
Close stream after close message
nWacky Oct 31, 2019
7b720f3
Format, minor refactor
nWacky Oct 31, 2019
7d81472
Resolve variables
nWacky Oct 31, 2019
36110b6
Add instructions how to use variables with subscriptions (playground …
nWacky Oct 31, 2019
7b10956
Format imports, fix warnings, use id from request
nWacky Oct 31, 2019
5a79836
Fix "__Type" not found error in queries
nWacky Oct 31, 2019
068771a
Fix "_EmptySubscription" introspection query errors
nWacky Nov 1, 2019
0bbb537
Update graphql playground version
nWacky Nov 1, 2019
2ada844
Remove all `println`s from `juniper_warp`
nWacky Nov 1, 2019
0234924
Fix warnings, check if `warp_async` compiles without `async` feature …
nWacky Nov 1, 2019
4c0748d
Fix project's cargo.toml [skip ci]
nWacky Nov 1, 2019
7706ebc
Update comment in `warp_subscriptions` [skip ci]
nWacky Nov 1, 2019
7a262ed
Rebase onto `async-await`
nWacky Nov 1, 2019
54d4bec
Merge branch 'async-await-subscriptions' of https://github.com/instru…
nWacky Nov 1, 2019
4debdfb
Rebase onto `master`
nWacky Nov 4, 2019
6bed20f
Format
nWacky Nov 4, 2019
ea474ab
Merge branch 'async-await-subscriptions' of https://github.com/instru…
nWacky Nov 4, 2019
90873ac
Tests pass [skip ci]
nWacky Nov 4, 2019
153a870
Rebase onto `async-await`
nWacky Nov 5, 2019
b1a395c
Merge branch 'async-await-subscriptions' of https://github.com/instru…
nWacky Nov 5, 2019
599747f
Fix warnings and remove unnecessary .len() function for value::Object
tyranron Nov 5, 2019
acd1bf2
Refactor value::Object methods
tyranron Nov 5, 2019
5b1173d
Refactor validation module changes
tyranron Nov 5, 2019
1f1837b
Some corrections [skip ci]
tyranron Nov 5, 2019
2036ba5
Rebase juniper onto master
nWacky Nov 7, 2019
f3ee2c6
Fix warp compilation errors
nWacky Nov 7, 2019
a9d5e93
Merge branch 'async-await-subscriptions' of https://github.com/instru…
nWacky Nov 7, 2019
3a86158
Revert merge
nWacky Nov 7, 2019
365c5ec
Start resolving todo with macro
nWacky Nov 8, 2019
43f3478
Fix tests compilation errors
nWacky Nov 8, 2019
6b48eff
Rebase onto `async-await`
nWacky Nov 8, 2019
e6257b3
Merge branch 'async-await-upstream' into async-await-subscriptions
nWacky Nov 8, 2019
f95e15e
Resolve warnings, add 'as _' to StreamExt, TryStreamExt, FuturesExt
nWacky Nov 8, 2019
6a1a27d
Juniper's tests pass
nWacky Nov 8, 2019
8df420e
Add return types as iterators support for `graphql::subscription` macro
nWacky Nov 8, 2019
be1534e
Format
nWacky Nov 11, 2019
60cc4ff
Format playground
nWacky Nov 11, 2019
fe5bfa4
Reset `juniper_rocket` to async version, fix warnings
nWacky Nov 11, 2019
000e83a
Refactor docs in base.rs
nWacky Nov 11, 2019
39403e4
Add GraphQLTypeAsync manual implementation example
nWacky Nov 11, 2019
a927100
Fix tests
nWacky Nov 11, 2019
9af5d59
Make playground url optional, fix bug in #[juniper::subscription]
nWacky Nov 11, 2019
66e5388
Format, update docs in `async_await`
nWacky Nov 11, 2019
5627d3e
Add test for `test_extract_ok_type_from_std_result`, remove needless …
nWacky Nov 11, 2019
b05cfee
Format [skip ci]
nWacky Nov 11, 2019
76c3efa
Remove IteratorGraphQLResponse::into_iter\StreamGraphQLResponse::into…
nWacky Nov 12, 2019
bd430dd
Resolve error `warp::reject::Reject` not implemented, add #![deny(war…
nWacky Nov 13, 2019
932267f
Add `EmptySubscription` to `juniper_warp` tests
nWacky Nov 13, 2019
ffdfa6e
Rename `fields_count` to `field_count`
nWacky Nov 13, 2019
6a75ca5
User `take_while` in `Object::try_from_iter`
nWacky Nov 13, 2019
ce96a9b
Update docs on subscription macros
nWacky Nov 13, 2019
77d6862
Add FieldResult to `extract_result_segment`
nWacky Nov 13, 2019
c05748b
Use .for_each instead of .then.for_each [skip ci]
nWacky Nov 13, 2019
8e892a6
Remove needless structs from playground [skip ci]
nWacky Nov 13, 2019
2fcb523
Merge branch 'async-await-subscriptions-origin' into async-await-subs…
nWacky Nov 14, 2019
4ae9e44
Remove GraphQLSubscriptionType
nWacky Nov 14, 2019
f9d0ec6
Refactor subscription functions
nWacky Nov 14, 2019
65f10c2
Add default implementation for `StreamGraphQLResponse` back
nWacky Nov 14, 2019
3372df1
Remove `ValuesIterator`
nWacky Nov 14, 2019
92d0b89
Pass fragments vector by value in `Executor`
nWacky Nov 14, 2019
3d250df
Rename `GraphQLSubscriptionTypeAsync` to `GraphQLSubscriptionType`, r…
nWacky Nov 14, 2019
9577bf9
Tried owned executor [does not compile]
nWacky Nov 14, 2019
1c7a356
Add manual GraphQLType implementantion to playground [does not compile]
nWacky Nov 14, 2019
c34a312
Rename `ValuesStream` to `ValuesResultStream`, replace returned value…
nWacky Nov 15, 2019
661ae5e
Rename GraphQLSubscriptionType to GraphQLSubscriptionTypeAsync
nWacky Nov 15, 2019
0fc7532
Start adding better error logic
nWacky Nov 15, 2019
0f71b13
Rewrite resolve_selection_set_into_stream_recursive to return Result<…
nWacky Nov 15, 2019
283e3b4
Return errors, add errors to StreamGraphQLResponse [only juniper comp…
nWacky Nov 15, 2019
4152006
Return errors and close connection from juniper_warp
nWacky Nov 15, 2019
047e092
Try using `SubscriptionsExecutor` instead of `Executor` [does not com…
nWacky Nov 18, 2019
1fb4ada
Keep only necessary things in SubscriptionsExecutor
nWacky Nov 18, 2019
e2dfb54
Revert to 4152006d0d5037e2ce3e6992bef95e692313af71
nWacky Nov 18, 2019
74d303f
Return ExecutionError and return errors over graphql more correctly
nWacky Nov 18, 2019
eb153e9
Start updating macros [does not compile]
nWacky Nov 19, 2019
c90abaa
Resolve only the first item in selection set, panic otherwise. Use Su…
nWacky Nov 19, 2019
f189b89
Use executor in resolver
nWacky Nov 19, 2019
553f694
Format
nWacky Nov 19, 2019
73b3810
Return Null when got error while resolving subscription
nWacky Nov 19, 2019
954f325
resolve fragments on subscriptions
nWacky Nov 19, 2019
56f0d78
Fix bug when resolving fragment spread
nWacky Nov 20, 2019
d17548b
Fix warnings, format
nWacky Nov 20, 2019
d0c9b34
Macros compile
nWacky Nov 20, 2019
6e89673
Move subscriptions logic to separate module
nWacky Nov 20, 2019
3a2cdfe
Add SubscriptionCoordinator and SubscriptionConnection traits, Subscr…
nWacky Nov 20, 2019
f275a2c
Start implementing subscription connection for subscriptions example
nWacky Nov 20, 2019
f80cbbc
Try using Executor instead of SubscriptionsExecutor [only juniper com…
nWacky Nov 21, 2019
9851277
Use `Executor` instead of `SubscriptionsExecutor` in most places and …
nWacky Nov 21, 2019
ceab242
Uncomment resolver logic, fix lifetimes
nWacky Nov 22, 2019
ee6936f
Use Arc for FieldPath
nWacky Nov 22, 2019
0398ec1
Format
nWacky Nov 22, 2019
beadd15
Fix bug with endless loop on empty object
nWacky Nov 22, 2019
de2303b
Fix lifetime mismatch compilation error with fragment spread
nWacky Nov 25, 2019
cb5dc85
Encapsulate FieldError and ExecutionError to StreamError, add derive(…
nWacky Nov 25, 2019
b9d9322
Use Executor's errors vector, update http handlers
nWacky Nov 25, 2019
236452f
Add trait to convert T to Ok(t)
nWacky Nov 26, 2019
6ef4e69
Support both `T` and `Result<T, E>` as field resolvers
nWacky Nov 27, 2019
f32bb3b
Implement GraphQLTraitAsync for T, Result<T>, and T in stream, Result…
nWacky Nov 27, 2019
dc383c9
Encapsualte subscription-related traits to juniper
nWacky Nov 27, 2019
e7c2059
Fix bug with type alias
nWacky Nov 27, 2019
35944d4
Update macros
nWacky Nov 27, 2019
3ef45e8
Return errors from `into_stream` directly
nWacky Nov 27, 2019
f609577
Remove needless cloning
nWacky Nov 27, 2019
f5b99c9
Format
nWacky Nov 27, 2019
ccf0ad2
Pass Executor instead of OwnedExecutor to resolve_field_into_stream
nWacky Nov 27, 2019
c2ead6d
Update macros
nWacky Nov 27, 2019
03c5ed8
Refactor ExecutorDataVariables
nWacky Nov 27, 2019
5b17288
Allow multiple subscriptions to be executed
nWacky Nov 27, 2019
8474a6c
Format, add some docs and todos
nWacky Nov 27, 2019
d41e7d8
Fix warnings
nWacky Nov 28, 2019
d8c3692
Remove SubscriptionCoordinator and SubscriptionConnection
nWacky Nov 28, 2019
e04e2f8
Minor refactor
nWacky Nov 28, 2019
5d0e2b8
Export juniper_codegen::async to #[cfg(feature = "async")]
nWacky Nov 28, 2019
6475747
Mark local todos to todo#433, change all todos to TODO
nWacky Nov 28, 2019
815e442
Resolve local TODOs
nWacky Nov 28, 2019
6f70d1c
Update args.rs todos as in master
nWacky Nov 28, 2019
c0f8d80
Update tests
nWacky Nov 28, 2019
86bee52
Update tests
nWacky Nov 28, 2019
576e876
Rename executor_wrappers to owned_executor, update juniper_codegen todo
nWacky Nov 28, 2019
2ca2026
Format
nWacky Nov 28, 2019
bf0c85d
Lint juniper, clean up playground
nWacky Nov 28, 2019
e40c36e
Remove needless test
nWacky Nov 28, 2019
117860a
Update playgrounds
nWacky Nov 29, 2019
079f6dd
Rebase onto async-await
nWacky Nov 29, 2019
f30c422
Merge branch 'async-await-subscriptions' into async-await-subscriptio…
nWacky Nov 29, 2019
eda2d2b
Fix merge
nWacky Nov 29, 2019
533796b
Start fixing tests
nWacky Nov 29, 2019
ac21584
tests pass
nWacky Nov 29, 2019
9df3a44
Format imports
nWacky Nov 29, 2019
8f2d805
Fix compilation error on `run`
nWacky Nov 29, 2019
975cd9f
Format [skip ci]
nWacky Nov 29, 2019
481b3b5
Exclude subscriptions example from the project tree [skip ci]
nWacky Nov 29, 2019
d8379ac
Rebase onto master
nWacky Jan 27, 2020
a53ab44
Fix juniper compilation errors
nWacky Jan 27, 2020
0815d10
Resolve todos
nWacky Jan 27, 2020
17231bf
Review subscription execution logic
nWacky Jan 28, 2020
f7d23a9
Review subscription execution logic
nWacky Jan 28, 2020
37ede87
Minor refactor
nWacky Jan 28, 2020
cdbafdf
Fix warp and warp example compilation errors
nWacky Jan 28, 2020
faea73d
Format
nWacky Jan 28, 2020
ce56ce2
Fix warnings
nWacky Jan 28, 2020
9e09df3
Rename #[subscription] to #[graphqL_subscription]
nWacky Jan 28, 2020
44549b6
Fix tests compilation errors
nWacky Jan 28, 2020
6f1a5be
Fix test warnings
nWacky Jan 29, 2020
beaf1c8
Resolve todos, uncomment other tests
nWacky Jan 29, 2020
ad22bab
Resolve todos, uncomment and fix other tests
nWacky Jan 29, 2020
e4decf0
Format [skip ci]
nWacky Jan 29, 2020
81166b3
Merge branch 'async-await-subscriptions' of https://github.com/instru…
nWacky Jan 29, 2020
284e9b2
Fix merge [skip ci]
nWacky Jan 29, 2020
02d7176
Merge remote-tracking branch 'origin/master' into async-await-subscri…
nWacky Mar 3, 2020
05e1c8a
Fix compilation errors
nWacky Mar 3, 2020
58824dc
Format
nWacky Mar 3, 2020
a9bd13d
Sketch SubscriptionCoordinator::subscribe
nWacky Mar 3, 2020
263a3af
Create `Coordinator: SubscriptionCoordinator`
nWacky Mar 4, 2020
bb19863
Implement SubscriptionCoordinator::subscribe()
nWacky Mar 4, 2020
5725d4c
Implement Connection
nWacky Mar 4, 2020
a5231a1
Implement demo server, temporatily not return Box<SubscriptionConnect…
nWacky Mar 4, 2020
cbe08fa
Move subscriptions impl to juniper_subscriptions module, tried implem…
nWacky Mar 5, 2020
5f1325a
Delete old Connection implementation
nWacky Mar 5, 2020
02617f9
Implement From<Value::Object<Stream>> for Connection
nWacky Mar 5, 2020
abd8c89
Return Self::Connection from SubscriptionConnection::subscribe
nWacky Mar 5, 2020
584214d
Remove async trait from Subscription
nWacky Mar 6, 2020
12fef56
Format
nWacky Mar 6, 2020
5be6ce1
Resolve warnings and some todos
nWacky Mar 6, 2020
3102a92
Implement other members in `Connection::from_stream`
nWacky Mar 6, 2020
7b6d2b6
Implement Value::Null and Value::Scalar in `Connection::from_stream`
nWacky Mar 6, 2020
3161ed5
Refactor Connection::from_stream, implement value::list
nWacky Mar 6, 2020
75e27ed
Add some docs
nWacky Mar 6, 2020
d09e0ba
Implement fastest_response_stream
nWacky Mar 7, 2020
ce37269
Add docs for juniper_subscriptions
nWacky Mar 7, 2020
7a7e1de
Minor refactor: function names
nWacky Mar 7, 2020
8addc5e
Rebane ValuesResultStream to ValuesStream
nWacky Mar 7, 2020
3c88a45
Resolve todos
nWacky Mar 7, 2020
a28a8fc
Fix compilation errors without async feature
nWacky Mar 7, 2020
bd6595e
Clean up
nWacky Mar 7, 2020
c8ae02c
Format
nWacky Mar 8, 2020
f6b4561
Not panic on errors in juniper_warp
nWacky Mar 9, 2020
71b16fe
Fix error resolving "_schema" query
nWacky Mar 9, 2020
510282b
Add a todo: not panic on nonexisting fields inside GraphQL objects
nWacky Mar 9, 2020
4361a00
Move warp_subscriptions implementation to example
nWacky Mar 10, 2020
6d3feb7
Fix tests errors
nWacky Mar 10, 2020
54fe613
Tried writing tests with subscriptioncooddinator
nWacky Mar 10, 2020
ce7505c
Subscriptions tests pass
nWacky Mar 10, 2020
c7d7d2f
Remove needless todos [skip ci]
nWacky Mar 11, 2020
60594e3
Merge remote-tracking branch 'origin/async-await-subscriptions' into …
nWacky Mar 11, 2020
f29e621
Fix merge
nWacky Mar 11, 2020
5f69876
Merge remote-tracking branch 'upstream/master' into async-await-subsc…
nWacky Mar 11, 2020
f0a1a31
Fix merge
nWacky Mar 11, 2020
e53c062
Format
nWacky Mar 11, 2020
500cb10
Clean up, add todos [skip ci]
nWacky Mar 11, 2020
d027221
Fix tests failure
nWacky Mar 11, 2020
4bc481f
Add juniper_subscriptions tests
nWacky Mar 12, 2020
bcd5978
Clean up juniper_subscriptions::whole_values_stream logic
nWacky Mar 12, 2020
9737394
Format
nWacky Mar 12, 2020
7785ad7
Refactor
nWacky Mar 12, 2020
5bd8462
Refactor warp_subscriptions
nWacky Mar 13, 2020
da0138c
Subscription tests pass
nWacky Mar 13, 2020
b0b9486
Rebase warp_async
nWacky Mar 13, 2020
a7a1941
Refactor
nWacky Mar 15, 2020
63287a5
Add subscription macros tests
nWacky Mar 15, 2020
65e3047
Merge remote-tracking branch 'upstream/master' into async-await-subsc…
nWacky Mar 15, 2020
6863e4c
Fix merge
nWacky Mar 15, 2020
a7b537e
Update changelog and docs, minor refactor
nWacky Mar 15, 2020
aa27e42
Fix CI
nWacky Mar 15, 2020
21dac57
Format
nWacky Mar 15, 2020
a1656ee
Update changelogs
nWacky Mar 15, 2020
963f5e1
Apply suggestions
nWacky Mar 16, 2020
f86cf8b
Apply review suggestions
nWacky Mar 16, 2020
f837582
Refactor warp_subscriptions
nWacky Mar 17, 2020
66b5fc1
Fix tests
nWacky Mar 17, 2020
994440e
Add poll_fn back
nWacky Mar 17, 2020
5d0d5e8
Format
nWacky Mar 17, 2020
c56dbf4
Not exclude warp
nWacky Mar 17, 2020
b6ed979
Move graphql_subscriptions to juniper_warp
nWacky Mar 17, 2020
b0392ed
Update book and benches
nWacky Mar 17, 2020
2962ef7
Add type to EmptySubcription
nWacky Mar 17, 2020
67ba411
Fix book compile error
nWacky Mar 17, 2020
b62c339
Add subscriptions to release.toml
nWacky Mar 18, 2020
e3070e2
Merge remote-tracking branch 'upstream/master' into async-await-subsc…
nWacky Mar 18, 2020
c5e5fe9
Remove futures01 from juniper_warp
nWacky Mar 18, 2020
fcb0082
Format
nWacky Mar 18, 2020
1baa24f
Fix cargo.toml
nWacky Mar 18, 2020
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ members = [
"juniper_hyper",
"juniper_iron",
"juniper_rocket",
"juniper_subscriptions",
"juniper_warp",
]
exclude = [
"docs/book/tests",
# TODO enable warp
"juniper_warp",
nWacky marked this conversation as resolved.
Show resolved Hide resolved
"examples/warp_async",
"examples/warp_subscriptions",
# TODO enable async tests
"juniper_rocket_async",
]
1 change: 1 addition & 0 deletions examples/warp_async/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ env_logger = "0.6.2"
warp = "0.1.19"
futures = { version = "0.3.1", features = ["compat"] }
reqwest = "0.9.19"
tokio = { version = "0.2", features = ["rt-core", "macros"] }

juniper_codegen = { git = "https://github.com/graphql-rust/juniper", branch = "async-await", features = ["async"] }
juniper = { git = "https://github.com/graphql-rust/juniper", branch = "async-await", features = ["async"] }
Expand Down
14 changes: 8 additions & 6 deletions examples/warp_async/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! This example demonstrates async/await usage with warp.
//! NOTE: this uses tokio 0.1 , not the alpha tokio 0.2.

use juniper::{EmptyMutation, RootNode, FieldError};
use juniper::{EmptyMutation, EmptySubscription, RootNode, FieldError};
use warp::{http::Response, Filter};

#[derive(Clone)]
Expand Down Expand Up @@ -73,13 +73,14 @@ impl Query {
}
}

type Schema = RootNode<'static, Query, EmptyMutation<Context>>;
type Schema = RootNode<'static, Query, EmptyMutation<Context>, EmptySubscription<Context>>;

fn schema() -> Schema {
Schema::new(Query, EmptyMutation::<Context>::new())
Schema::new(Query, EmptyMutation::<Context>::new(), EmptySubscription::<Context>::new())
}

fn main() {
#[tokio::main]
async fn main() {
::std::env::set_var("RUST_LOG", "warp_async");
env_logger::init();

Expand All @@ -99,12 +100,13 @@ fn main() {
let graphql_filter = juniper_warp::make_graphql_filter_async(schema(), state.boxed());

warp::serve(
warp::get2()
warp::get()
.and(warp::path("graphiql"))
.and(juniper_warp::graphiql_filter("/graphql"))
.or(homepage)
.or(warp::path("graphql").and(graphql_filter))
.with(log),
)
.run(([127, 0, 0, 1], 8080));
.run(([127, 0, 0, 1], 8080))
.await
}
1 change: 1 addition & 0 deletions examples/warp_subscriptions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
20 changes: 20 additions & 0 deletions examples/warp_subscriptions/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "warp_subscriptions"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
env_logger = "0.6.2"
futures = { version = "=0.3.1" }
log = "0.4.8"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "0.2", features = ["rt-core", "macros"] }
warp = "0.2.1"

# TODO#433: get crates from GitHub
nWacky marked this conversation as resolved.
Show resolved Hide resolved
juniper = { path = "../../juniper" }
juniper_subscriptions = { path = "../../juniper_subscriptions"}
juniper_warp = { path = "../../juniper_warp" }
187 changes: 187 additions & 0 deletions examples/warp_subscriptions/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
//! This example demonstrates asynchronous subscriptions with warp and tokio 0.2

use std::{pin::Pin, sync::Arc, time::Duration};

use futures::{Future, FutureExt as _, Stream};
use juniper::{DefaultScalarValue, EmptyMutation, FieldError, RootNode};
use juniper_subscriptions::Coordinator;
use juniper_warp::playground_filter;
use warp::{http::Response, Filter};
use warp_subscriptions::*;

mod warp_subscriptions;

#[derive(Clone)]
struct Context {}

impl juniper::Context for Context {}

#[derive(Clone, Copy, juniper::GraphQLEnum)]
enum UserKind {
Admin,
User,
Guest,
}

struct User {
id: i32,
kind: UserKind,
name: String,
}

// Field resolvers implementation
#[juniper::graphql_object(Context = Context)]
impl User {
fn id(&self) -> i32 {
self.id
}

fn kind(&self) -> UserKind {
self.kind
}

fn name(&self) -> &str {
&self.name
}

async fn friends(&self) -> Vec<User> {
if self.id == 1 {
return vec![
User {
id: 11,
kind: UserKind::User,
name: "user11".into(),
},
User {
id: 12,
kind: UserKind::Admin,
name: "user12".into(),
},
User {
id: 13,
kind: UserKind::Guest,
name: "user13".into(),
},
];
} else if self.id == 2 {
return vec![User {
id: 21,
kind: UserKind::User,
name: "user21".into(),
}];
} else if self.id == 3 {
return vec![
User {
id: 31,
kind: UserKind::User,
name: "user31".into(),
},
User {
id: 32,
kind: UserKind::Guest,
name: "user32".into(),
},
];
} else {
return vec![];
}
}
}

struct Query;

#[juniper::graphql_object(Context = Context)]
impl Query {
async fn users(id: i32) -> Vec<User> {
vec![User {
id,
kind: UserKind::Admin,
name: "User Name".into(),
}]
}
}

type UsersStream = Pin<Box<dyn Stream<Item = Result<User, FieldError>> + Send>>;

struct Subscription;

#[juniper::graphql_subscription(Context = Context)]
impl Subscription {
async fn users() -> UsersStream {
let mut counter = 0;
let stream = tokio::time::interval(Duration::from_secs(5)).map(move |_| {
counter += 1;
if counter == 2 {
Err(FieldError::new(
"some field error from handler",
Value::Scalar(DefaultScalarValue::String(
"some additional string".to_string(),
)),
))
} else {
Ok(User {
id: counter,
kind: UserKind::Admin,
name: "stream user".to_string(),
})
}
});

Box::pin(stream)
}
}

type Schema = RootNode<'static, Query, EmptyMutation<Context>, Subscription>;

fn schema() -> Schema {
Schema::new(Query, EmptyMutation::new(), Subscription)
}

#[tokio::main]
async fn main() {
::std::env::set_var("RUST_LOG", "warp_subscriptions");
env_logger::init();

let log = warp::log("warp_server");

let homepage = warp::path::end().map(|| {
Response::builder()
.header("content-type", "text/html")
.body(format!(
"<html><h1>juniper_subscriptions demo</h1><div>visit <a href=\"/playground\">graphql playground</a></html>"
))
});

let qm_schema = schema();
let qm_state = warp::any().map(move || Context {});
let qm_graphql_filter = juniper_warp::make_graphql_filter_async(qm_schema, qm_state.boxed());

let sub_state = warp::any().map(move || Context {});
let coordinator = Arc::new(juniper_subscriptions::Coordinator::new(schema()));

log::info!("Listening on 127.0.0.1:8080");

let routes = (warp::path("subscriptions")
.and(warp::ws())
.and(sub_state.clone())
.and(warp::any().map(move || Arc::clone(&coordinator)))
.map(
|ws: warp::ws::Ws,
ctx: Context,
coordinator: Arc<Coordinator<'static, _, _, _, _, _>>| {
ws.on_upgrade(|websocket| -> Pin<Box<dyn Future<Output = ()> + Send>> {
graphql_subscriptions_async(websocket, coordinator, ctx).boxed()
})
},
))
.or(warp::post()
.and(warp::path("graphql"))
.and(qm_graphql_filter))
.or(warp::get()
.and(warp::path("playground"))
.and(playground_filter("/graphql", Some("/subscriptions"))))
.or(homepage)
.with(log);

warp::serve(routes).run(([127, 0, 0, 1], 8080)).await;
}
Loading