Skip to main content

dagger_sdk/core/
introspection.rs

1#![allow(non_camel_case_types)]
2
3use serde::{Deserialize, Deserializer, Serialize};
4
5#[derive(Clone, Debug)]
6pub enum __DirectiveLocation {
7    QUERY,
8    MUTATION,
9    SUBSCRIPTION,
10    FIELD,
11    FRAGMENT_DEFINITION,
12    FRAGMENT_SPREAD,
13    INLINE_FRAGMENT,
14    SCHEMA,
15    SCALAR,
16    OBJECT,
17    FIELD_DEFINITION,
18    ARGUMENT_DEFINITION,
19    INTERFACE,
20    UNION,
21    ENUM,
22    ENUM_VALUE,
23    INPUT_OBJECT,
24    INPUT_FIELD_DEFINITION,
25    Other(String),
26}
27
28impl Serialize for __DirectiveLocation {
29    fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
30        ser.serialize_str(match *self {
31            __DirectiveLocation::QUERY => "QUERY",
32            __DirectiveLocation::MUTATION => "MUTATION",
33            __DirectiveLocation::SUBSCRIPTION => "SUBSCRIPTION",
34            __DirectiveLocation::FIELD => "FIELD",
35            __DirectiveLocation::FRAGMENT_DEFINITION => "FRAGMENT_DEFINITION",
36            __DirectiveLocation::FRAGMENT_SPREAD => "FRAGMENT_SPREAD",
37            __DirectiveLocation::INLINE_FRAGMENT => "INLINE_FRAGMENT",
38            __DirectiveLocation::SCHEMA => "SCHEMA",
39            __DirectiveLocation::SCALAR => "SCALAR",
40            __DirectiveLocation::OBJECT => "OBJECT",
41            __DirectiveLocation::FIELD_DEFINITION => "FIELD_DEFINITION",
42            __DirectiveLocation::ARGUMENT_DEFINITION => "ARGUMENT_DEFINITION",
43            __DirectiveLocation::INTERFACE => "INTERFACE",
44            __DirectiveLocation::UNION => "UNION",
45            __DirectiveLocation::ENUM => "ENUM",
46            __DirectiveLocation::ENUM_VALUE => "ENUM_VALUE",
47            __DirectiveLocation::INPUT_OBJECT => "INPUT_OBJECT",
48            __DirectiveLocation::INPUT_FIELD_DEFINITION => "INPUT_FIELD_DEFINITION",
49            __DirectiveLocation::Other(ref s) => s.as_str(),
50        })
51    }
52}
53
54impl<'de> Deserialize<'de> for __DirectiveLocation {
55    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
56        let s = <&'de str>::deserialize(deserializer)?;
57        match s {
58            "QUERY" => Ok(__DirectiveLocation::QUERY),
59            "MUTATION" => Ok(__DirectiveLocation::MUTATION),
60            "SUBSCRIPTION" => Ok(__DirectiveLocation::SUBSCRIPTION),
61            "FIELD" => Ok(__DirectiveLocation::FIELD),
62            "FRAGMENT_DEFINITION" => Ok(__DirectiveLocation::FRAGMENT_DEFINITION),
63            "FRAGMENT_SPREAD" => Ok(__DirectiveLocation::FRAGMENT_SPREAD),
64            "INLINE_FRAGMENT" => Ok(__DirectiveLocation::INLINE_FRAGMENT),
65            "SCHEMA" => Ok(__DirectiveLocation::SCHEMA),
66            "SCALAR" => Ok(__DirectiveLocation::SCALAR),
67            "OBJECT" => Ok(__DirectiveLocation::OBJECT),
68            "FIELD_DEFINITION" => Ok(__DirectiveLocation::FIELD_DEFINITION),
69            "ARGUMENT_DEFINITION" => Ok(__DirectiveLocation::ARGUMENT_DEFINITION),
70            "INTERFACE" => Ok(__DirectiveLocation::INTERFACE),
71            "UNION" => Ok(__DirectiveLocation::UNION),
72            "ENUM" => Ok(__DirectiveLocation::ENUM),
73            "ENUM_VALUE" => Ok(__DirectiveLocation::ENUM_VALUE),
74            "INPUT_OBJECT" => Ok(__DirectiveLocation::INPUT_OBJECT),
75            "INPUT_FIELD_DEFINITION" => Ok(__DirectiveLocation::INPUT_FIELD_DEFINITION),
76            _ => Ok(__DirectiveLocation::Other(s.to_string())),
77        }
78    }
79}
80
81#[derive(Clone, Debug, PartialEq)]
82pub enum __TypeKind {
83    SCALAR,
84    OBJECT,
85    INTERFACE,
86    UNION,
87    ENUM,
88    INPUT_OBJECT,
89    LIST,
90    NON_NULL,
91    Other(String),
92}
93
94impl Serialize for __TypeKind {
95    fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
96        ser.serialize_str(match *self {
97            __TypeKind::SCALAR => "SCALAR",
98            __TypeKind::OBJECT => "OBJECT",
99            __TypeKind::INTERFACE => "INTERFACE",
100            __TypeKind::UNION => "UNION",
101            __TypeKind::ENUM => "ENUM",
102            __TypeKind::INPUT_OBJECT => "INPUT_OBJECT",
103            __TypeKind::LIST => "LIST",
104            __TypeKind::NON_NULL => "NON_NULL",
105            __TypeKind::Other(ref s) => s.as_str(),
106        })
107    }
108}
109
110impl<'de> Deserialize<'de> for __TypeKind {
111    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
112        let s = <&'de str>::deserialize(deserializer)?;
113        match s {
114            "SCALAR" => Ok(__TypeKind::SCALAR),
115            "OBJECT" => Ok(__TypeKind::OBJECT),
116            "INTERFACE" => Ok(__TypeKind::INTERFACE),
117            "UNION" => Ok(__TypeKind::UNION),
118            "ENUM" => Ok(__TypeKind::ENUM),
119            "INPUT_OBJECT" => Ok(__TypeKind::INPUT_OBJECT),
120            "LIST" => Ok(__TypeKind::LIST),
121            "NON_NULL" => Ok(__TypeKind::NON_NULL),
122            _ => Ok(__TypeKind::Other(s.to_string())),
123        }
124    }
125}
126
127#[derive(Clone, Debug, Deserialize)]
128#[serde(rename_all = "camelCase")]
129pub struct FullType {
130    pub kind: Option<__TypeKind>,
131    pub name: Option<String>,
132    pub description: Option<String>,
133    pub fields: Option<Vec<FullTypeFields>>,
134    pub input_fields: Option<Vec<FullTypeInputFields>>,
135    pub interfaces: Option<Vec<FullTypeInterfaces>>,
136    pub enum_values: Option<Vec<FullTypeEnumValues>>,
137    pub possible_types: Option<Vec<FullTypePossibleTypes>>,
138}
139
140#[derive(Clone, Debug, Deserialize)]
141#[serde(rename_all = "camelCase")]
142pub struct FullTypeFieldsArgs {
143    #[serde(flatten)]
144    pub input_value: InputValue,
145}
146
147#[derive(Clone, Debug, Deserialize)]
148#[serde(rename_all = "camelCase")]
149pub struct FullTypeFieldsType {
150    #[serde(flatten)]
151    pub type_ref: TypeRef,
152}
153
154#[derive(Clone, Debug, Deserialize)]
155#[serde(rename_all = "camelCase")]
156pub struct FullTypeFields {
157    pub name: Option<String>,
158    pub description: Option<String>,
159    pub args: Option<Vec<Option<FullTypeFieldsArgs>>>,
160    #[serde(rename = "type")]
161    pub type_: Option<FullTypeFieldsType>,
162    pub is_deprecated: Option<bool>,
163    pub deprecation_reason: Option<String>,
164    #[serde(default)]
165    pub directives: Option<Vec<DirectiveApplication>>,
166
167    #[serde(skip)]
168    pub parent_type: Option<FullType>,
169}
170
171#[derive(Clone, Debug, Deserialize)]
172#[serde(rename_all = "camelCase")]
173pub struct FullTypeInputFields {
174    #[serde(flatten)]
175    pub input_value: InputValue,
176}
177
178#[derive(Clone, Debug, Deserialize)]
179#[serde(rename_all = "camelCase")]
180pub struct FullTypeInterfaces {
181    #[serde(flatten)]
182    pub type_ref: TypeRef,
183}
184
185#[derive(Clone, Debug, Deserialize)]
186#[serde(rename_all = "camelCase")]
187pub struct FullTypeEnumValues {
188    pub name: Option<String>,
189    pub description: Option<String>,
190    pub is_deprecated: Option<bool>,
191    pub deprecation_reason: Option<String>,
192}
193
194#[derive(Clone, Debug, Deserialize)]
195#[serde(rename_all = "camelCase")]
196pub struct FullTypePossibleTypes {
197    #[serde(flatten)]
198    pub type_ref: TypeRef,
199}
200
201#[derive(Clone, Debug, Deserialize)]
202#[serde(rename_all = "camelCase")]
203pub struct InputValue {
204    pub name: String,
205    pub description: Option<String>,
206    #[serde(rename = "type")]
207    pub type_: InputValueType,
208    pub default_value: Option<String>,
209    #[serde(default)]
210    pub directives: Option<Vec<DirectiveApplication>>,
211}
212
213type InputValueType = TypeRef;
214
215#[derive(Clone, Debug, Deserialize)]
216#[serde(rename_all = "camelCase")]
217pub struct TypeRef {
218    pub kind: Option<__TypeKind>,
219    pub name: Option<String>,
220    pub of_type: Option<Box<TypeRef>>,
221}
222
223#[derive(Clone, Debug, Deserialize)]
224#[serde(rename_all = "camelCase")]
225pub struct SchemaQueryType {
226    pub name: Option<String>,
227}
228
229#[derive(Clone, Debug, Deserialize)]
230#[serde(rename_all = "camelCase")]
231pub struct SchemaMutationType {
232    pub name: Option<String>,
233}
234
235#[derive(Clone, Debug, Deserialize)]
236#[serde(rename_all = "camelCase")]
237pub struct SchemaSubscriptionType {
238    pub name: Option<String>,
239}
240
241#[derive(Clone, Debug, Deserialize)]
242#[serde(rename_all = "camelCase")]
243pub struct SchemaTypes {
244    #[serde(flatten)]
245    pub full_type: FullType,
246}
247
248#[allow(dead_code)]
249#[derive(Clone, Debug, Deserialize)]
250#[serde(rename_all = "camelCase")]
251pub struct SchemaDirectivesArgs {
252    #[serde(flatten)]
253    input_value: InputValue,
254}
255
256#[derive(Clone, Debug, Deserialize)]
257#[serde(rename_all = "camelCase")]
258pub struct SchemaDirectives {
259    pub name: Option<String>,
260    pub description: Option<String>,
261    pub locations: Option<Vec<Option<__DirectiveLocation>>>,
262    pub args: Option<Vec<Option<SchemaDirectivesArgs>>>,
263}
264
265#[allow(dead_code)]
266#[derive(Clone, Debug, Deserialize)]
267#[serde(rename_all = "camelCase")]
268pub struct Schema {
269    pub query_type: Option<SchemaQueryType>,
270    pub mutation_type: Option<SchemaMutationType>,
271    pub subscription_type: Option<SchemaSubscriptionType>,
272    pub types: Option<Vec<Option<SchemaTypes>>>,
273    directives: Option<Vec<Option<SchemaDirectives>>>,
274}
275
276/// A directive applied to a schema element (field, argument, etc.).
277#[derive(Clone, Debug, Deserialize)]
278#[serde(rename_all = "camelCase")]
279pub struct DirectiveApplication {
280    pub name: String,
281    #[serde(default)]
282    pub args: Vec<DirectiveApplicationArg>,
283}
284
285/// A single argument on a directive application.
286#[derive(Clone, Debug, Deserialize)]
287#[serde(rename_all = "camelCase")]
288pub struct DirectiveApplicationArg {
289    pub name: String,
290    pub value: Option<String>,
291}
292
293/// Extension trait for extracting `@expectedType` from directive lists.
294pub trait DirectivesExt {
295    fn expected_type(&self) -> Option<String>;
296}
297
298impl DirectivesExt for Option<Vec<DirectiveApplication>> {
299    fn expected_type(&self) -> Option<String> {
300        let directives = self.as_ref()?;
301        let d = directives.iter().find(|d| d.name == "expectedType")?;
302        let arg = d.args.iter().find(|a| a.name == "name")?;
303        let raw = arg.value.as_ref()?;
304        // The value is JSON-encoded, e.g. `"\"Container\""`.
305        serde_json::from_str::<String>(raw).ok()
306    }
307}
308
309#[derive(Clone, Debug, Deserialize)]
310pub struct SchemaContainer {
311    #[serde(rename = "__schema")]
312    pub schema: Option<Schema>,
313}
314
315#[derive(Deserialize, Debug)]
316pub struct FullResponse<T> {
317    data: T,
318}
319
320#[derive(Debug, Deserialize)]
321#[serde(untagged)]
322pub enum IntrospectionResponse {
323    FullResponse(FullResponse<SchemaContainer>),
324    Schema(SchemaContainer),
325}
326
327impl IntrospectionResponse {
328    pub fn as_schema(&self) -> &SchemaContainer {
329        match self {
330            IntrospectionResponse::FullResponse(full_response) => &full_response.data,
331            IntrospectionResponse::Schema(schema) => schema,
332        }
333    }
334
335    pub fn into_schema(self) -> SchemaContainer {
336        match self {
337            IntrospectionResponse::FullResponse(full_response) => full_response.data,
338            IntrospectionResponse::Schema(schema) => schema,
339        }
340    }
341}