Skip to main content

libgraphql_core/operation/
operation.rs

1use indexmap::IndexMap;
2
3use crate::loc;
4use crate::operation::Query;
5use crate::operation::Mutation;
6use crate::operation::SelectionSet;
7use crate::operation::Subscription;
8use crate::operation::Variable;
9use crate::schema::Schema;
10use crate::types::GraphQLType;
11use crate::DirectiveAnnotation;
12use std::boxed::Box;
13
14#[derive(Clone, Debug, PartialEq)]
15pub enum Operation<'schema: 'fragreg, 'fragreg> {
16    Query(Box<Query<'schema, 'fragreg>>),
17    Mutation(Box<Mutation<'schema, 'fragreg>>),
18    Subscription(Box<Subscription<'schema, 'fragreg>>),
19}
20impl<'schema: 'fragreg, 'fragreg> Operation<'schema, 'fragreg> {
21    pub fn def_location(&self) -> &loc::SourceLocation {
22        match self {
23            Self::Mutation(op) => op.def_location(),
24            Self::Query(op) => op.def_location(),
25            Self::Subscription(op) => op.def_location(),
26        }
27    }
28
29    pub fn directives(&self) -> &Vec<DirectiveAnnotation> {
30        match self {
31            Self::Mutation(op) => op.directives(),
32            Self::Query(op) => op.directives(),
33            Self::Subscription(op) => op.directives(),
34        }
35    }
36
37    pub fn is_mutation(&self) -> bool {
38        matches!(self, Self::Mutation(_))
39    }
40
41    pub fn is_query(&self) -> bool {
42        matches!(self, Self::Query(_))
43    }
44
45    pub fn is_subscription(&self) -> bool {
46        matches!(self, Self::Subscription(_))
47    }
48
49    pub fn name(&self) -> Option<&str> {
50        match self {
51            Self::Mutation(op) => op.name(),
52            Self::Query(op) => op.name(),
53            Self::Subscription(op) => op.name(),
54        }
55    }
56
57    pub fn root_graphql_type(&self, schema: &'schema Schema) -> &GraphQLType {
58        match self {
59            Self::Mutation(op) => op.root_graphql_type(schema),
60            Self::Query(op) => op.root_graphql_type(schema),
61            Self::Subscription(op) => op.root_graphql_type(schema),
62        }
63    }
64
65    pub fn selection_set(&self) -> &SelectionSet<'fragreg> {
66        match self {
67            Self::Mutation(op) => op.selection_set(),
68            Self::Query(op) => op.selection_set(),
69            Self::Subscription(op) => op.selection_set(),
70        }
71    }
72
73    pub fn variables(&self) -> &IndexMap<String, Variable> {
74        match self {
75            Self::Mutation(op) => op.variables(),
76            Self::Query(op) => op.variables(),
77            Self::Subscription(op) => op.variables(),
78        }
79    }
80}