finchers_juniper/execute/
current_thread.rs1use finchers::endpoint;
2use finchers::endpoint::wrapper::Wrapper;
3use finchers::endpoint::{ApplyContext, ApplyResult, Endpoint, IntoEndpoint};
4use finchers::error::Error;
5
6use futures::future;
7use futures::{Future, Poll};
8
9use juniper::{GraphQLType, RootNode};
10use std::fmt;
11
12use super::Schema;
13use request::{GraphQLRequestEndpoint, GraphQLResponse, RequestFuture};
14
15pub fn current_thread<S>(schema: S) -> CurrentThread<S>
20where
21 S: Schema,
22{
23 CurrentThread { schema }
24}
25
26#[allow(missing_docs)]
27#[derive(Debug)]
28pub struct CurrentThread<S> {
29 schema: S,
30}
31
32impl<'a, S> IntoEndpoint<'a> for CurrentThread<S>
33where
34 S: Schema<Context = ()> + 'a,
35{
36 type Output = (GraphQLResponse,);
37 type Endpoint = CurrentThreadEndpoint<endpoint::Cloned<()>, S>;
38
39 fn into_endpoint(self) -> Self::Endpoint {
40 CurrentThreadEndpoint {
41 context: endpoint::cloned(()),
42 request: ::request::graphql_request(),
43 schema: self.schema,
44 }
45 }
46}
47
48impl<'a, E, CtxT, S> Wrapper<'a, E> for CurrentThread<S>
49where
50 E: Endpoint<'a, Output = (CtxT,)>,
51 S: Schema<Context = CtxT> + 'a,
52 CtxT: 'a,
53{
54 type Output = (GraphQLResponse,);
55 type Endpoint = CurrentThreadEndpoint<E, S>;
56
57 fn wrap(self, endpoint: E) -> Self::Endpoint {
58 CurrentThreadEndpoint {
59 context: endpoint,
60 request: ::request::graphql_request(),
61 schema: self.schema,
62 }
63 }
64}
65
66#[derive(Debug)]
67pub struct CurrentThreadEndpoint<E, S> {
68 context: E,
69 request: GraphQLRequestEndpoint,
70 schema: S,
71}
72
73impl<'a, E, S, CtxT> Endpoint<'a> for CurrentThreadEndpoint<E, S>
74where
75 E: Endpoint<'a, Output = (CtxT,)>,
76 S: Schema<Context = CtxT> + 'a,
77 CtxT: 'a,
78{
79 type Output = (GraphQLResponse,);
80 type Future = CurrentThreadFuture<'a, E, S::Query, S::Mutation, CtxT>;
81
82 fn apply(&'a self, cx: &mut ApplyContext<'_>) -> ApplyResult<Self::Future> {
83 let context = self.context.apply(cx)?;
84 let request = self.request.apply(cx)?;
85 Ok(CurrentThreadFuture {
86 inner: context.join(request),
87 root_node: self.schema.as_root_node(),
88 })
89 }
90}
91
92pub struct CurrentThreadFuture<'a, E, QueryT, MutationT, CtxT>
93where
94 E: Endpoint<'a, Output = (CtxT,)>,
95 QueryT: GraphQLType<Context = CtxT> + 'a,
96 MutationT: GraphQLType<Context = CtxT> + 'a,
97 CtxT: 'a,
98{
99 inner: future::Join<E::Future, RequestFuture<'a>>,
100 root_node: &'a RootNode<'static, QueryT, MutationT>,
101}
102
103impl<'a, E, QueryT, MutationT, CtxT> fmt::Debug
104 for CurrentThreadFuture<'a, E, QueryT, MutationT, CtxT>
105where
106 E: Endpoint<'a, Output = (CtxT,)>,
107 QueryT: GraphQLType<Context = CtxT> + 'a,
108 MutationT: GraphQLType<Context = CtxT> + 'a,
109 CtxT: 'a,
110{
111 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112 f.debug_struct("CurrentThreadFuture").finish()
113 }
114}
115
116impl<'a, E, QueryT, MutationT, CtxT> Future for CurrentThreadFuture<'a, E, QueryT, MutationT, CtxT>
117where
118 E: Endpoint<'a, Output = (CtxT,)>,
119 QueryT: GraphQLType<Context = CtxT>,
120 MutationT: GraphQLType<Context = CtxT>,
121 CtxT: 'a,
122{
123 type Item = (GraphQLResponse,);
124 type Error = Error;
125
126 fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
127 let ((context,), (request,)) = try_ready!(self.inner.poll());
128 let response = request.execute(self.root_node, &context);
129 Ok((response,).into())
130 }
131}