Skip to main content

libgraphql_core/operation/
subscription.rs

1use crate::ast;
2use crate::operation::FragmentRegistry;
3use crate::types::GraphQLType;
4use crate::DirectiveAnnotation;
5use crate::loc;
6use crate::operation::OperationTrait;
7use crate::operation::OperationData;
8use crate::operation::SelectionSet;
9use crate::operation::SubscriptionBuilder;
10use crate::operation::SubscriptionBuildError;
11use crate::operation::Variable;
12use crate::schema::Schema;
13use indexmap::IndexMap;
14use inherent::inherent;
15
16#[derive(Clone, Debug, PartialEq)]
17pub struct Subscription<'schema: 'fragreg, 'fragreg>(
18    pub(super) OperationData<'schema, 'fragreg>,
19);
20
21#[inherent]
22impl<'schema: 'fragreg, 'fragreg> OperationTrait<
23    'schema,
24    'fragreg,
25    ast::operation::Subscription,
26    SubscriptionBuildError,
27    SubscriptionBuilder<'schema, 'fragreg>,
28> for Subscription<'schema, 'fragreg> {
29    /// Convenience wrapper around [`SubscriptionBuilder::new()`].
30    pub fn builder(
31        schema: &'schema Schema,
32        fragment_registry: &'fragreg FragmentRegistry<'schema>,
33    ) -> SubscriptionBuilder<'schema, 'fragreg> {
34        SubscriptionBuilder::new(schema, fragment_registry)
35    }
36
37    /// The list of [`DirectiveAnnotation`]s applied to this [`Subscription`].
38    pub fn directives(&self) -> &Vec<DirectiveAnnotation> {
39        &self.0.directives
40    }
41
42    /// The [`loc::SourceLocation`] indicating where this [`Subscription`]
43    /// operation was defined.
44    pub fn def_location(&self) -> &loc::SourceLocation {
45        &self.0.def_location
46    }
47
48    /// Access the name of this [`Subscription`] (if one was specified).
49    pub fn name(&self) -> Option<&str> {
50        self.0.name.as_deref()
51    }
52
53    /// Returns the root `Subscription` [`GraphQLType`] defined in the schema
54    /// for this [`Subscription`] operation.
55    pub fn root_graphql_type(&self, schema: &'schema Schema) -> &GraphQLType {
56        // TODO: Define a test that asserts that it's impossible to build a
57        //       `Subscription` operation against a schema that doesn't define a
58        //       mutation root type.
59        schema.subscription_type().unwrap()
60    }
61
62    /// Access the [`SelectionSet`] defined for this [`Subscription`].
63    pub fn selection_set(&self) -> &SelectionSet<'fragreg> {
64        &self.0.selection_set
65    }
66
67    /// Access the [`Variable`]s defined on this [`Subscription`].
68    pub fn variables(&self) -> &IndexMap<String, Variable> {
69        &self.0.variables
70    }
71}