async_graphql/dynamic/
directive.rs

1use indexmap::IndexMap;
2
3use crate::{registry::MetaDirectiveInvocation, Value};
4
5/// A GraphQL directive
6#[derive(Debug, Clone)]
7pub struct Directive {
8    name: String,
9    args: IndexMap<String, Value>,
10}
11
12impl Directive {
13    /// Create a directive usage
14    pub fn new(name: impl Into<String>) -> Self {
15        Self {
16            name: name.into(),
17            args: IndexMap::default(),
18        }
19    }
20
21    /// Add an argument to the directive
22    #[inline]
23    pub fn argument(mut self, name: impl Into<String>, value: Value) -> Self {
24        self.args.insert(name.into(), value);
25        self
26    }
27}
28
29impl From<Directive> for MetaDirectiveInvocation {
30    fn from(directive: Directive) -> Self {
31        Self {
32            name: directive.name,
33            args: directive.args,
34        }
35    }
36}
37
38pub fn to_meta_directive_invocation(directives: Vec<Directive>) -> Vec<MetaDirectiveInvocation> {
39    directives
40        .into_iter()
41        .map(MetaDirectiveInvocation::from)
42        .collect()
43}