[go: up one dir, main page]

cynic/
core.rs

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use std::borrow::Cow;

use crate::{queries::SelectionBuilder, QueryVariablesFields};

/// A trait that marks a type as part of a GraphQL query.
///
/// This will usually be derived, but can be manually implemented if required.
pub trait QueryFragment: Sized {
    /// The type in a schema that this `QueryFragment` represents
    type SchemaType;

    /// The variables that are required to execute this `QueryFragment`
    type VariablesFields: QueryVariablesFields;

    /// The name of the type in the GraphQL schema
    const TYPE: Option<&'static str> = None;

    /// Adds this fragment to the query being built by `builder`
    fn query(builder: SelectionBuilder<'_, Self::SchemaType, Self::VariablesFields>);

    /// The name of this fragment, useful for operations, maybe fragments if we ever support them...
    fn name() -> Option<Cow<'static, str>> {
        // Most QueryFragments don't need a name so return None
        None
    }
}

impl<T> QueryFragment for Option<T>
where
    T: QueryFragment,
{
    type SchemaType = Option<T::SchemaType>;
    type VariablesFields = T::VariablesFields;

    fn query(builder: SelectionBuilder<'_, Self::SchemaType, Self::VariablesFields>) {
        T::query(builder.into_inner())
    }
}

impl<T> QueryFragment for Vec<T>
where
    T: QueryFragment,
{
    type SchemaType = Vec<T::SchemaType>;
    type VariablesFields = T::VariablesFields;

    fn query(builder: SelectionBuilder<'_, Self::SchemaType, Self::VariablesFields>) {
        T::query(builder.into_inner())
    }
}

impl<T> QueryFragment for Box<T>
where
    T: QueryFragment,
{
    type SchemaType = T::SchemaType;
    type VariablesFields = T::VariablesFields;

    const TYPE: Option<&'static str> = T::TYPE;

    fn query(builder: SelectionBuilder<'_, Self::SchemaType, Self::VariablesFields>) {
        T::query(builder)
    }
}

impl<T> QueryFragment for std::rc::Rc<T>
where
    T: QueryFragment,
{
    type SchemaType = T::SchemaType;
    type VariablesFields = T::VariablesFields;

    const TYPE: Option<&'static str> = T::TYPE;

    fn query(builder: SelectionBuilder<'_, Self::SchemaType, Self::VariablesFields>) {
        T::query(builder)
    }
}

impl<T> QueryFragment for std::sync::Arc<T>
where
    T: QueryFragment,
{
    type SchemaType = T::SchemaType;
    type VariablesFields = T::VariablesFields;

    const TYPE: Option<&'static str> = T::TYPE;

    fn query(builder: SelectionBuilder<'_, Self::SchemaType, Self::VariablesFields>) {
        T::query(builder)
    }
}

impl QueryFragment for bool {
    type SchemaType = bool;
    type VariablesFields = ();

    fn query(_builder: SelectionBuilder<'_, Self::SchemaType, Self::VariablesFields>) {}
}

impl QueryFragment for String {
    type SchemaType = String;
    type VariablesFields = ();

    fn query(_builder: SelectionBuilder<'_, Self::SchemaType, Self::VariablesFields>) {}
}

/// A QueryFragment that contains a set of inline fragments
///
/// This should be derived on an enum with newtype variants where each
/// inner type is a `QueryFragment` for an appropriate type.
pub trait InlineFragments<'de>: QueryFragment + serde::de::Deserialize<'de> {
    /// Attempts to deserialize a variant with the given typename.
    fn deserialize_variant<D>(typename: &str, deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>;
}

/// A GraphQL Enum.
///
/// Note that in GraphQL these can't contain data - they are just a set of
/// strings.
///
/// This should be be derived on an enum with unit variants.
pub trait Enum: serde::de::DeserializeOwned + serde::Serialize {
    /// The enum in the schema that this type represents.
    type SchemaType;
}

impl<T> Enum for Option<T>
where
    T: Enum,
{
    type SchemaType = Option<T::SchemaType>;
}

impl<T> Enum for Vec<T>
where
    T: Enum,
{
    type SchemaType = Vec<T::SchemaType>;
}

impl<T> Enum for Box<T>
where
    T: Enum,
{
    type SchemaType = T::SchemaType;
}

/// A GraphQL input object.
///
/// This should be derived on a struct.
pub trait InputObject: serde::Serialize {
    /// The input object in the schema that this type represents.
    type SchemaType;
}

impl<T> InputObject for Option<T>
where
    T: InputObject,
{
    type SchemaType = Option<T::SchemaType>;
}

impl<T> InputObject for Vec<T>
where
    T: InputObject,
{
    type SchemaType = Vec<T::SchemaType>;
}

impl<T> InputObject for [T]
where
    T: InputObject,
{
    type SchemaType = Vec<T::SchemaType>;
}

impl<T, const SIZE: usize> InputObject for [T; SIZE]
where
    T: InputObject,
    Self: serde::Serialize,
{
    type SchemaType = Vec<T::SchemaType>;
}

impl<T> InputObject for Box<T>
where
    T: InputObject,
{
    type SchemaType = T::SchemaType;
}

impl<T: ?Sized> InputObject for &T
where
    T: InputObject,
{
    type SchemaType = T::SchemaType;
}