cynic_parser/type_system/
extensions.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
use std::{borrow::Cow, fmt};

use crate::common::OperationType;

use super::{Argument, Description, Directive, RootOperationTypeDefinition, SchemaDefinition};

impl<'a> Description<'a> {
    pub fn to_cow(&self) -> Cow<'a, str> {
        self.literal().to_cow()
    }

    pub fn raw_untrimmed_str(&self) -> &'a str {
        self.literal().raw_untrimmed_str()
    }
}

impl fmt::Display for Description<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.literal().fmt(f)
    }
}

impl<'a> SchemaDefinition<'a> {
    pub fn root_definition(
        &self,
        operation_type: OperationType,
    ) -> Option<RootOperationTypeDefinition<'a>> {
        self.root_operations()
            .find(|op| op.operation_type() == operation_type)
    }

    pub fn root_query_definition(&self) -> Option<RootOperationTypeDefinition<'a>> {
        self.root_definition(OperationType::Query)
    }

    pub fn root_mutation_definition(&self) -> Option<RootOperationTypeDefinition<'a>> {
        self.root_definition(OperationType::Mutation)
    }

    pub fn root_subscription_definition(&self) -> Option<RootOperationTypeDefinition<'a>> {
        self.root_definition(OperationType::Subscription)
    }
}

impl<'a> Directive<'a> {
    pub fn argument(&self, name: &str) -> Option<Argument<'a>> {
        self.arguments().find(|arg| arg.name() == name)
    }
}