1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use actix_web::{
guard,
web::{self, ServiceConfig},
HttpResponse,
};
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
EmptySubscription, ObjectType, SubscriptionType,
};
use std::any::Any;
use crate::auth::middleware::AuthCheck;
use crate::errors::*;
use crate::graphql::get_schema_without_subscriptions;
use crate::options::{AuthCheckBlockState, Options};
use crate::routes::graphql;
pub fn create_graphql_server<C, Q, M, S>(
opts: Options<C, Q, M, S>,
) -> Result<impl FnOnce(&mut ServiceConfig) + Clone>
where
C: Any + Send + Sync + Clone,
Q: Clone + ObjectType + 'static,
M: Clone + ObjectType + 'static,
S: Clone + SubscriptionType + 'static,
{
let schema =
get_schema_without_subscriptions(opts.schema, opts.subscriptions_server_data, opts.ctx)?;
let auth_middleware = match opts.authentication_block_state {
AuthCheckBlockState::AllowAll => AuthCheck::new(&opts.jwt_secret).allow_all(),
AuthCheckBlockState::AllowMissing => AuthCheck::new(&opts.jwt_secret).allow_missing(),
AuthCheckBlockState::BlockUnauthenticated => {
AuthCheck::new(&opts.jwt_secret).block_unauthenticated()
}
};
let graphql_endpoint = opts.graphql_endpoint;
let playground_endpoint = opts.playground_endpoint;
let configurer = move |cfg: &mut ServiceConfig| {
cfg.data(schema.clone())
.service(
web::resource(&graphql_endpoint)
.guard(guard::Post())
.wrap(auth_middleware.clone())
.to(graphql::<Q, M, EmptySubscription>),
);
let graphql_endpoint_for_closure = graphql_endpoint;
let graphiql_closure = move || {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(playground_source(
GraphQLPlaygroundConfig::new(&graphql_endpoint_for_closure)
.subscription_endpoint(&graphql_endpoint_for_closure),
))
};
match playground_endpoint {
Some(playground_endpoint) if cfg!(debug_assertions) => {
cfg.service(
web::resource(playground_endpoint)
.guard(guard::Get())
.to(graphiql_closure),
);
}
Some(playground_endpoint) => {
cfg.service(
web::resource(playground_endpoint)
.guard(guard::Get())
.wrap(auth_middleware.clone())
.to(graphiql_closure),
);
}
None => (),
};
};
Ok(configurer)
}