@@ -2,7 +2,6 @@ use super::*;
2
2
use crate :: name;
3
3
use crate :: parser:: Parser ;
4
4
use crate :: parser:: SourceSpan ;
5
- use crate :: schema:: ArgumentByNameError ;
6
5
use crate :: schema:: SchemaBuilder ;
7
6
use crate :: validation:: DiagnosticList ;
8
7
use crate :: validation:: Valid ;
@@ -218,6 +217,8 @@ impl Definition {
218
217
}
219
218
}
220
219
220
+ /// If this node was parsed from a source file, returns the file ID and source span
221
+ /// (start and end byte offsets) within that file.
221
222
pub fn location ( & self ) -> Option < SourceSpan > {
222
223
match self {
223
224
Self :: OperationDefinition ( def) => def. location ( ) ,
@@ -242,7 +243,8 @@ impl Definition {
242
243
243
244
/// Return the name of this type definition or extension.
244
245
///
245
- /// Operations may be anonymous, and schema definitions never have a name, in that case this function returns `None`.
246
+ /// Operations may be anonymous, and schema definitions and extensions never have a name.
247
+ /// In those cases this method returns `None`.
246
248
pub fn name ( & self ) -> Option < & Name > {
247
249
match self {
248
250
Self :: OperationDefinition ( def) => def. name . as_ref ( ) ,
@@ -554,6 +556,11 @@ impl DirectiveList {
554
556
self . get ( name) . is_some ( )
555
557
}
556
558
559
+ /// Accepts either [`Node<Directive>`] or [`Directive`].
560
+ pub fn push ( & mut self , directive : impl Into < Node < Directive > > ) {
561
+ self . 0 . push ( directive. into ( ) ) ;
562
+ }
563
+
557
564
serialize_method ! ( ) ;
558
565
}
559
566
@@ -620,6 +627,13 @@ impl FromIterator<Directive> for DirectiveList {
620
627
}
621
628
622
629
impl Directive {
630
+ pub fn new ( name : Name ) -> Self {
631
+ Self {
632
+ name,
633
+ arguments : Vec :: new ( ) ,
634
+ }
635
+ }
636
+
623
637
/// Returns the value of the argument named `name`, accounting for nullability
624
638
/// and for the default value in `schema`’s directive definition.
625
639
pub fn argument_by_name < ' doc_or_schema > (
@@ -802,7 +816,9 @@ impl Type {
802
816
Type :: List ( Box :: new ( self ) )
803
817
}
804
818
805
- /// If the type is a list type or a non-null list type, return the item type.
819
+ /// If the type is a list type (nullable or not), returns the inner item type.
820
+ ///
821
+ /// Otherwise returns `self` unchanged.
806
822
///
807
823
/// # Example
808
824
/// ```
@@ -832,18 +848,20 @@ impl Type {
832
848
matches ! ( self , Type :: NonNullNamed ( _) | Type :: NonNullList ( _) )
833
849
}
834
850
835
- /// Returns whether this type is a list, on a non-null list
851
+ /// Returns whether this type is a list type (nullable or not)
836
852
pub fn is_list ( & self ) -> bool {
837
853
matches ! ( self , Type :: List ( _) | Type :: NonNullList ( _) )
838
854
}
839
855
856
+ /// Returns whether this type is a named type (nullable or not), as opposed to a list type.
840
857
pub fn is_named ( & self ) -> bool {
841
858
matches ! ( self , Type :: Named ( _) | Type :: NonNullNamed ( _) )
842
859
}
843
860
844
861
/// Can a value of this type be used when the `target` type is expected?
845
862
///
846
- /// Implementation of spec function `AreTypesCompatible()`.
863
+ /// Implementation of spec function
864
+ /// [_AreTypesCompatible()_](https://spec.graphql.org/draft/#AreTypesCompatible()).
847
865
pub fn is_assignable_to ( & self , target : & Self ) -> bool {
848
866
match ( target, self ) {
849
867
// Can't assign a nullable type to a non-nullable type.
@@ -894,7 +912,7 @@ impl FieldDefinition {
894
912
impl InputValueDefinition {
895
913
/// Returns true if usage sites are required to provide a value for this input value.
896
914
///
897
- /// That means :
915
+ /// An input value is required when :
898
916
/// - its type is non-null, and
899
917
/// - it does not have a default value
900
918
pub fn is_required ( & self ) -> bool {
@@ -909,6 +927,8 @@ impl EnumValueDefinition {
909
927
}
910
928
911
929
impl Selection {
930
+ /// If this node was parsed from a source file, returns the file ID and source span
931
+ /// (start and end byte offsets) within that file.
912
932
pub fn location ( & self ) -> Option < SourceSpan > {
913
933
match self {
914
934
Self :: Field ( field) => field. location ( ) ,
@@ -945,17 +965,19 @@ impl Selection {
945
965
}
946
966
947
967
impl Field {
948
- /// Get the name that will be used for this field selection in response formatting .
968
+ /// Get the name that will be used for this field selection in [ response `data`] .
949
969
///
950
- /// For example, in this operation, the response name is " sourceField" :
970
+ /// For example, in this operation, the response name is ` sourceField` :
951
971
/// ```graphql
952
972
/// query GetField { sourceField }
953
973
/// ```
954
974
///
955
- /// But in this operation that uses an alias, the response name is " responseField" :
975
+ /// But in this operation that uses an alias, the response name is ` responseField` :
956
976
/// ```graphql
957
977
/// query GetField { responseField: sourceField }
958
978
/// ```
979
+ ///
980
+ /// [response `data`]: https://spec.graphql.org/draft/#sec-Response-Format
959
981
pub fn response_name ( & self ) -> & Name {
960
982
self . alias . as_ref ( ) . unwrap_or ( & self . name )
961
983
}
@@ -1000,6 +1022,9 @@ impl Value {
1000
1022
}
1001
1023
}
1002
1024
1025
+ /// Convert a [`FloatValue`] **_or [`IntValue`]_** to floating point representation.
1026
+ ///
1027
+ /// Returns `None` if the value is of a different kind, or if the conversion overflows.
1003
1028
pub fn to_f64 ( & self ) -> Option < f64 > {
1004
1029
match self {
1005
1030
Value :: Float ( value) => value. try_to_f64 ( ) . ok ( ) ,
@@ -1523,6 +1548,7 @@ impl From<InputObjectTypeExtension> for Definition {
1523
1548
}
1524
1549
}
1525
1550
1551
+ /// The Rust unit value a.k.a empty tuple converts to [`Value::Null`].
1526
1552
impl From < ( ) > for Value {
1527
1553
fn from ( _value : ( ) ) -> Self {
1528
1554
Value :: Null
@@ -1565,6 +1591,7 @@ impl From<bool> for Value {
1565
1591
}
1566
1592
}
1567
1593
1594
+ /// The Rust unit value a.k.a empty tuple converts to [`Value::Null`].
1568
1595
impl From < ( ) > for Node < Value > {
1569
1596
fn from ( value : ( ) ) -> Self {
1570
1597
Node :: new ( value. into ( ) )
0 commit comments