Skip to main content

cynic_parser/type_system/
extensions.rs

1use std::{borrow::Cow, fmt};
2
3use crate::{common::OperationType, type_system::InputObjectDefinition};
4
5use super::{Argument, Description, Directive, RootOperationTypeDefinition, SchemaDefinition};
6
7impl<'a> Description<'a> {
8    pub fn to_cow(&self) -> Cow<'a, str> {
9        self.literal().to_cow()
10    }
11
12    pub fn raw_untrimmed_str(&self) -> &'a str {
13        self.literal().raw_untrimmed_str()
14    }
15}
16
17impl fmt::Display for Description<'_> {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        self.literal().fmt(f)
20    }
21}
22
23impl<'a> SchemaDefinition<'a> {
24    pub fn root_definition(
25        &self,
26        operation_type: OperationType,
27    ) -> Option<RootOperationTypeDefinition<'a>> {
28        self.root_operations()
29            .find(|op| op.operation_type() == operation_type)
30    }
31
32    pub fn root_query_definition(&self) -> Option<RootOperationTypeDefinition<'a>> {
33        self.root_definition(OperationType::Query)
34    }
35
36    pub fn root_mutation_definition(&self) -> Option<RootOperationTypeDefinition<'a>> {
37        self.root_definition(OperationType::Mutation)
38    }
39
40    pub fn root_subscription_definition(&self) -> Option<RootOperationTypeDefinition<'a>> {
41        self.root_definition(OperationType::Subscription)
42    }
43}
44
45impl<'a> Directive<'a> {
46    pub fn argument(&self, name: &str) -> Option<Argument<'a>> {
47        self.arguments().find(|arg| arg.name() == name)
48    }
49}
50
51impl InputObjectDefinition<'_> {
52    pub fn is_one_of(&self) -> bool {
53        self.directives()
54            .any(|directive| directive.name() == "oneOf")
55    }
56}