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
use crate::{
AnyBox, GraphqlType, Interface, InterfaceMark, Object, OutputType, Register, Registry,
ResolveOwned, ResolveRef,
};
use async_graphql::dynamic::FieldValue;
use async_graphql::Context;
pub struct Instance<'v, I, T = ()>
where
I: ?Sized,
I: Interface,
{
pub(crate) _interface: std::marker::PhantomData<I>,
pub(crate) _target: std::marker::PhantomData<T>,
value: AnyBox<'v>,
}
impl<I: ?Sized> Instance<'_, I>
where
I: Interface,
{
#[inline]
pub fn new_owned<'a, T>(value: T) -> Instance<'a, I>
where
T: InterfaceMark<I> + Object + ResolveOwned<'a> + Send + Sync + 'static,
{
Instance {
_interface: std::marker::PhantomData,
_target: std::marker::PhantomData,
value: AnyBox::new_owned(value, <T as Object>::NAME.to_string()),
}
}
#[inline]
pub fn new_borrowed<'a, T>(value: &'a T) -> Instance<'a, I>
where
T: InterfaceMark<I> + Object + ResolveRef<'a> + Send + Sync + 'static,
{
Instance {
_interface: std::marker::PhantomData,
_target: std::marker::PhantomData,
value: AnyBox::new_borrowed(value, <T as Object>::NAME.to_string()),
}
}
}
impl<'a, I> ResolveOwned<'a> for Instance<'a, I>
where
I: ?Sized + Interface,
{
fn resolve_owned(self, ctx: &Context) -> async_graphql::Result<Option<FieldValue<'a>>> {
self.value.resolve_owned(ctx)
}
}
pub trait RegisterInstance<I, T>
where
I: ?Sized,
T: Object + 'static,
T: Send + Sync,
{
#[inline]
fn register_instance(registry: Registry) -> Registry {
registry
}
}
impl<I, T> Register for Instance<'_, I, T>
where
I: ?Sized,
I: Interface + 'static,
I: RegisterInstance<I, T>,
T: Object + 'static,
T: Send + Sync,
{
#[inline]
fn register(registry: Registry) -> Registry {
let registry = registry.register::<I>();
<I as RegisterInstance<I, T>>::register_instance(registry)
}
}
impl<I> Register for Instance<'_, I, ()>
where
I: ?Sized,
I: Interface + 'static,
{
#[inline]
fn register(registry: Registry) -> Registry {
registry.register::<I>()
}
}
impl<I> GraphqlType for Instance<'_, I>
where
I: Interface + 'static + ?Sized,
{
const NAME: &'static str = <I as GraphqlType>::NAME;
}
impl<I> OutputType for Instance<'_, I>
where
I: Interface + 'static + ?Sized,
{
const NAME: &'static str = <I as OutputType>::NAME;
}