libgraphql_core/operation/
query.rs

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