juniper/types/
pointers.rs

1use std::sync::Arc;
2
3use arcstr::ArcStr;
4
5use crate::{
6    BoxFuture,
7    ast::{FromInputValue, InputValue, Selection, ToInputValue},
8    executor::{ExecutionResult, Executor, Registry},
9    schema::meta::MetaType,
10    types::{
11        async_await::GraphQLValueAsync,
12        base::{Arguments, GraphQLType, GraphQLValue},
13    },
14    value::{FromScalarValue, ScalarValue, ToScalarValue},
15};
16
17impl<S, T> GraphQLType<S> for Box<T>
18where
19    T: GraphQLType<S> + ?Sized,
20    S: ScalarValue,
21{
22    fn name(info: &Self::TypeInfo) -> Option<ArcStr> {
23        T::name(info)
24    }
25
26    fn meta(info: &Self::TypeInfo, registry: &mut Registry<S>) -> MetaType<S> {
27        T::meta(info, registry)
28    }
29}
30
31impl<S, T> GraphQLValue<S> for Box<T>
32where
33    T: GraphQLValue<S> + ?Sized,
34    S: ScalarValue,
35{
36    type Context = T::Context;
37    type TypeInfo = T::TypeInfo;
38
39    fn type_name(&self, info: &Self::TypeInfo) -> Option<ArcStr> {
40        (**self).type_name(info)
41    }
42
43    fn resolve_into_type(
44        &self,
45        info: &Self::TypeInfo,
46        name: &str,
47        selection_set: Option<&[Selection<S>]>,
48        executor: &Executor<Self::Context, S>,
49    ) -> ExecutionResult<S> {
50        (**self).resolve_into_type(info, name, selection_set, executor)
51    }
52
53    fn resolve_field(
54        &self,
55        info: &Self::TypeInfo,
56        field: &str,
57        args: &Arguments<S>,
58        executor: &Executor<Self::Context, S>,
59    ) -> ExecutionResult<S> {
60        (**self).resolve_field(info, field, args, executor)
61    }
62
63    fn resolve(
64        &self,
65        info: &Self::TypeInfo,
66        selection_set: Option<&[Selection<S>]>,
67        executor: &Executor<Self::Context, S>,
68    ) -> ExecutionResult<S> {
69        (**self).resolve(info, selection_set, executor)
70    }
71}
72
73impl<S, T> GraphQLValueAsync<S> for Box<T>
74where
75    T: GraphQLValueAsync<S> + ?Sized,
76    T::TypeInfo: Sync,
77    T::Context: Sync,
78    S: ScalarValue + Send + Sync,
79{
80    fn resolve_async<'a>(
81        &'a self,
82        info: &'a Self::TypeInfo,
83        selection_set: Option<&'a [Selection<S>]>,
84        executor: &'a Executor<Self::Context, S>,
85    ) -> BoxFuture<'a, ExecutionResult<S>> {
86        (**self).resolve_async(info, selection_set, executor)
87    }
88}
89
90impl<'s, T, S> FromScalarValue<'s, S> for Box<T>
91where
92    S: ScalarValue,
93    T: FromScalarValue<'s, S> + 's,
94{
95    type Error = T::Error;
96
97    fn from_scalar_value(v: &'s S) -> Result<Self, Self::Error> {
98        T::from_scalar_value(v).map(Self::new)
99    }
100}
101
102impl<T, S> ToScalarValue<S> for Box<T>
103where
104    T: ToScalarValue<S> + ?Sized,
105{
106    fn to_scalar_value(&self) -> S {
107        (**self).to_scalar_value()
108    }
109}
110
111impl<T, S> FromInputValue<S> for Box<T>
112where
113    S: ScalarValue,
114    T: FromInputValue<S>,
115{
116    type Error = T::Error;
117
118    fn from_input_value(v: &InputValue<S>) -> Result<Box<T>, Self::Error> {
119        <T as FromInputValue<S>>::from_input_value(v).map(Box::new)
120    }
121}
122
123impl<T, S> ToInputValue<S> for Box<T>
124where
125    T: ToInputValue<S> + ?Sized,
126{
127    fn to_input_value(&self) -> InputValue<S> {
128        (**self).to_input_value()
129    }
130}
131
132impl<S, T> GraphQLType<S> for &T
133where
134    T: GraphQLType<S> + ?Sized,
135    S: ScalarValue,
136{
137    fn name(info: &Self::TypeInfo) -> Option<ArcStr> {
138        T::name(info)
139    }
140
141    fn meta(info: &Self::TypeInfo, registry: &mut Registry<S>) -> MetaType<S> {
142        T::meta(info, registry)
143    }
144}
145
146impl<S, T> GraphQLValue<S> for &T
147where
148    S: ScalarValue,
149    T: GraphQLValue<S> + ?Sized,
150{
151    type Context = T::Context;
152    type TypeInfo = T::TypeInfo;
153
154    fn type_name(&self, info: &Self::TypeInfo) -> Option<ArcStr> {
155        (**self).type_name(info)
156    }
157
158    fn resolve_into_type(
159        &self,
160        info: &Self::TypeInfo,
161        name: &str,
162        selection_set: Option<&[Selection<S>]>,
163        executor: &Executor<Self::Context, S>,
164    ) -> ExecutionResult<S> {
165        (**self).resolve_into_type(info, name, selection_set, executor)
166    }
167
168    fn resolve_field(
169        &self,
170        info: &Self::TypeInfo,
171        field: &str,
172        args: &Arguments<S>,
173        executor: &Executor<Self::Context, S>,
174    ) -> ExecutionResult<S> {
175        (**self).resolve_field(info, field, args, executor)
176    }
177
178    fn resolve(
179        &self,
180        info: &Self::TypeInfo,
181        selection_set: Option<&[Selection<S>]>,
182        executor: &Executor<Self::Context, S>,
183    ) -> ExecutionResult<S> {
184        (**self).resolve(info, selection_set, executor)
185    }
186}
187
188impl<S, T> GraphQLValueAsync<S> for &T
189where
190    T: GraphQLValueAsync<S> + ?Sized,
191    T::TypeInfo: Sync,
192    T::Context: Sync,
193    S: ScalarValue + Send + Sync,
194{
195    fn resolve_field_async<'b>(
196        &'b self,
197        info: &'b Self::TypeInfo,
198        field_name: &'b str,
199        arguments: &'b Arguments<S>,
200        executor: &'b Executor<Self::Context, S>,
201    ) -> BoxFuture<'b, ExecutionResult<S>> {
202        (**self).resolve_field_async(info, field_name, arguments, executor)
203    }
204
205    fn resolve_async<'a>(
206        &'a self,
207        info: &'a Self::TypeInfo,
208        selection_set: Option<&'a [Selection<S>]>,
209        executor: &'a Executor<Self::Context, S>,
210    ) -> BoxFuture<'a, ExecutionResult<S>> {
211        (**self).resolve_async(info, selection_set, executor)
212    }
213}
214
215impl<T, S> ToScalarValue<S> for &T
216where
217    T: ToScalarValue<S> + ?Sized,
218{
219    fn to_scalar_value(&self) -> S {
220        (**self).to_scalar_value()
221    }
222}
223
224impl<T, S> ToInputValue<S> for &T
225where
226    T: ToInputValue<S> + ?Sized,
227{
228    fn to_input_value(&self) -> InputValue<S> {
229        (**self).to_input_value()
230    }
231}
232
233impl<S, T> GraphQLType<S> for Arc<T>
234where
235    S: ScalarValue,
236    T: GraphQLType<S> + ?Sized,
237{
238    fn name(info: &Self::TypeInfo) -> Option<ArcStr> {
239        T::name(info)
240    }
241
242    fn meta(info: &Self::TypeInfo, registry: &mut Registry<S>) -> MetaType<S> {
243        T::meta(info, registry)
244    }
245}
246
247impl<S, T> GraphQLValue<S> for Arc<T>
248where
249    S: ScalarValue,
250    T: GraphQLValue<S> + ?Sized,
251{
252    type Context = T::Context;
253    type TypeInfo = T::TypeInfo;
254
255    fn type_name(&self, info: &Self::TypeInfo) -> Option<ArcStr> {
256        (**self).type_name(info)
257    }
258
259    fn resolve_into_type(
260        &self,
261        info: &Self::TypeInfo,
262        name: &str,
263        selection_set: Option<&[Selection<S>]>,
264        executor: &Executor<Self::Context, S>,
265    ) -> ExecutionResult<S> {
266        (**self).resolve_into_type(info, name, selection_set, executor)
267    }
268
269    fn resolve_field(
270        &self,
271        info: &Self::TypeInfo,
272        field: &str,
273        args: &Arguments<S>,
274        executor: &Executor<Self::Context, S>,
275    ) -> ExecutionResult<S> {
276        (**self).resolve_field(info, field, args, executor)
277    }
278
279    fn resolve(
280        &self,
281        info: &Self::TypeInfo,
282        selection_set: Option<&[Selection<S>]>,
283        executor: &Executor<Self::Context, S>,
284    ) -> ExecutionResult<S> {
285        (**self).resolve(info, selection_set, executor)
286    }
287}
288
289impl<S, T> GraphQLValueAsync<S> for Arc<T>
290where
291    T: GraphQLValueAsync<S> + Send + ?Sized,
292    T::TypeInfo: Sync,
293    T::Context: Sync,
294    S: ScalarValue + Send + Sync,
295{
296    fn resolve_async<'a>(
297        &'a self,
298        info: &'a Self::TypeInfo,
299        selection_set: Option<&'a [Selection<S>]>,
300        executor: &'a Executor<Self::Context, S>,
301    ) -> BoxFuture<'a, ExecutionResult<S>> {
302        (**self).resolve_async(info, selection_set, executor)
303    }
304}
305
306impl<'s, T, S> FromScalarValue<'s, S> for Arc<T>
307where
308    S: ScalarValue,
309    T: FromScalarValue<'s, S> + 's,
310{
311    type Error = T::Error;
312
313    fn from_scalar_value(v: &'s S) -> Result<Self, Self::Error> {
314        T::from_scalar_value(v).map(Self::new)
315    }
316}
317
318impl<T, S> ToScalarValue<S> for Arc<T>
319where
320    T: ToScalarValue<S> + ?Sized,
321{
322    fn to_scalar_value(&self) -> S {
323        (**self).to_scalar_value()
324    }
325}
326
327impl<T, S> FromInputValue<S> for Arc<T>
328where
329    S: ScalarValue,
330    T: FromInputValue<S>,
331{
332    type Error = T::Error;
333
334    fn from_input_value(v: &InputValue<S>) -> Result<Arc<T>, Self::Error> {
335        <T as FromInputValue<S>>::from_input_value(v).map(Arc::new)
336    }
337}
338
339impl<T, S> ToInputValue<S> for Arc<T>
340where
341    T: ToInputValue<S> + ?Sized,
342{
343    fn to_input_value(&self) -> InputValue<S> {
344        (**self).to_input_value()
345    }
346}