144
144
"GraphQLNamedInputType" ,
145
145
"GraphQLNamedOutputType" ,
146
146
"GraphQLNullableType" ,
147
+ "GraphQLNullableInputType" ,
148
+ "GraphQLNullableOutputType" ,
147
149
"GraphQLNonNull" ,
148
150
"GraphQLResolveInfo" ,
149
151
"GraphQLScalarType" ,
@@ -169,7 +171,7 @@ class GraphQLType:
169
171
"""Base class for all GraphQL types"""
170
172
171
173
# Note: We don't use slots for GraphQLType objects because memory considerations
172
- # are not really important for the schema definition and it would make caching
174
+ # are not really important for the schema definition, and it would make caching
173
175
# properties slower or more complicated.
174
176
175
177
@@ -188,7 +190,7 @@ def assert_type(type_: Any) -> GraphQLType:
188
190
189
191
# These types wrap and modify other types
190
192
191
- GT = TypeVar ("GT" , bound = GraphQLType )
193
+ GT = TypeVar ("GT" , bound = GraphQLType , covariant = True )
192
194
193
195
194
196
class GraphQLWrappingType (GraphQLType , Generic [GT ]):
@@ -1609,7 +1611,7 @@ def is_required_input_field(field: GraphQLInputField) -> bool:
1609
1611
# Wrapper types
1610
1612
1611
1613
1612
- class GraphQLList (Generic [ GT ], GraphQLWrappingType [GT ]):
1614
+ class GraphQLList (GraphQLWrappingType [GT ]):
1613
1615
"""List Type Wrapper
1614
1616
1615
1617
A list is a wrapping type which points to another type. Lists are often created
@@ -1645,10 +1647,10 @@ def assert_list_type(type_: Any) -> GraphQLList:
1645
1647
return type_
1646
1648
1647
1649
1648
- GNT = TypeVar ("GNT" , bound = "GraphQLNullableType" )
1650
+ GNT = TypeVar ("GNT" , bound = "GraphQLNullableType" , covariant = True )
1649
1651
1650
1652
1651
- class GraphQLNonNull (GraphQLWrappingType [GNT ], Generic [ GNT ] ):
1653
+ class GraphQLNonNull (GraphQLWrappingType [GNT ]):
1652
1654
"""Non-Null Type Wrapper
1653
1655
1654
1656
A non-null is a wrapping type which points to another type. Non-null types enforce
@@ -1680,41 +1682,108 @@ def __str__(self) -> str:
1680
1682
return f"{ self .of_type } !"
1681
1683
1682
1684
1683
- def is_non_null_type (type_ : Any ) -> TypeGuard [GraphQLNonNull ]:
1684
- return isinstance (type_ , GraphQLNonNull )
1685
-
1686
-
1687
- def assert_non_null_type (type_ : Any ) -> GraphQLNonNull :
1688
- if not is_non_null_type (type_ ):
1689
- raise TypeError (f"Expected { type_ } to be a GraphQL Non-Null type." )
1690
- return type_
1691
-
1692
-
1693
1685
# These types can all accept null as a value.
1694
1686
1695
- graphql_nullable_types = (
1687
+ GraphQLNullableType : TypeAlias = Union [
1696
1688
GraphQLScalarType ,
1697
1689
GraphQLObjectType ,
1698
1690
GraphQLInterfaceType ,
1699
1691
GraphQLUnionType ,
1700
1692
GraphQLEnumType ,
1701
1693
GraphQLInputObjectType ,
1702
1694
GraphQLList ,
1703
- )
1695
+ ]
1704
1696
1705
- GraphQLNullableType : TypeAlias = Union [
1697
+
1698
+ # These types may be used as input types for arguments and directives.
1699
+
1700
+ GraphQLNullableInputType : TypeAlias = Union [
1701
+ GraphQLScalarType ,
1702
+ GraphQLEnumType ,
1703
+ GraphQLInputObjectType ,
1704
+ # actually GraphQLList[GraphQLInputType], but we can't recurse
1705
+ GraphQLList ,
1706
+ ]
1707
+
1708
+ GraphQLInputType : TypeAlias = Union [
1709
+ GraphQLNullableInputType , GraphQLNonNull [GraphQLNullableInputType ]
1710
+ ]
1711
+
1712
+
1713
+ # These types may be used as output types as the result of fields.
1714
+
1715
+ GraphQLNullableOutputType : TypeAlias = Union [
1706
1716
GraphQLScalarType ,
1707
1717
GraphQLObjectType ,
1708
1718
GraphQLInterfaceType ,
1709
1719
GraphQLUnionType ,
1710
1720
GraphQLEnumType ,
1711
- GraphQLInputObjectType ,
1721
+ # actually GraphQLList[GraphQLOutputType], but we can't recurse
1712
1722
GraphQLList ,
1713
1723
]
1714
1724
1725
+ GraphQLOutputType : TypeAlias = Union [
1726
+ GraphQLNullableOutputType , GraphQLNonNull [GraphQLNullableOutputType ]
1727
+ ]
1728
+
1729
+
1730
+ # Predicates and Assertions
1731
+
1732
+
1733
+ def is_input_type (type_ : Any ) -> TypeGuard [GraphQLInputType ]:
1734
+ return isinstance (
1735
+ type_ , (GraphQLScalarType , GraphQLEnumType , GraphQLInputObjectType )
1736
+ ) or (isinstance (type_ , GraphQLWrappingType ) and is_input_type (type_ .of_type ))
1737
+
1738
+
1739
+ def assert_input_type (type_ : Any ) -> GraphQLInputType :
1740
+ if not is_input_type (type_ ):
1741
+ raise TypeError (f"Expected { type_ } to be a GraphQL input type." )
1742
+ return type_
1743
+
1744
+
1745
+ def is_output_type (type_ : Any ) -> TypeGuard [GraphQLOutputType ]:
1746
+ return isinstance (
1747
+ type_ ,
1748
+ (
1749
+ GraphQLScalarType ,
1750
+ GraphQLObjectType ,
1751
+ GraphQLInterfaceType ,
1752
+ GraphQLUnionType ,
1753
+ GraphQLEnumType ,
1754
+ ),
1755
+ ) or (isinstance (type_ , GraphQLWrappingType ) and is_output_type (type_ .of_type ))
1756
+
1757
+
1758
+ def assert_output_type (type_ : Any ) -> GraphQLOutputType :
1759
+ if not is_output_type (type_ ):
1760
+ raise TypeError (f"Expected { type_ } to be a GraphQL output type." )
1761
+ return type_
1762
+
1763
+
1764
+ def is_non_null_type (type_ : Any ) -> TypeGuard [GraphQLNonNull ]:
1765
+ return isinstance (type_ , GraphQLNonNull )
1766
+
1767
+
1768
+ def assert_non_null_type (type_ : Any ) -> GraphQLNonNull :
1769
+ if not is_non_null_type (type_ ):
1770
+ raise TypeError (f"Expected { type_ } to be a GraphQL Non-Null type." )
1771
+ return type_
1772
+
1715
1773
1716
1774
def is_nullable_type (type_ : Any ) -> TypeGuard [GraphQLNullableType ]:
1717
- return isinstance (type_ , graphql_nullable_types )
1775
+ return isinstance (
1776
+ type_ ,
1777
+ (
1778
+ GraphQLScalarType ,
1779
+ GraphQLObjectType ,
1780
+ GraphQLInterfaceType ,
1781
+ GraphQLUnionType ,
1782
+ GraphQLEnumType ,
1783
+ GraphQLInputObjectType ,
1784
+ GraphQLList ,
1785
+ ),
1786
+ )
1718
1787
1719
1788
1720
1789
def assert_nullable_type (type_ : Any ) -> GraphQLNullableType :
@@ -1747,59 +1816,6 @@ def get_nullable_type(
1747
1816
return cast (Optional [GraphQLNullableType ], type_ )
1748
1817
1749
1818
1750
- # These types may be used as input types for arguments and directives.
1751
-
1752
- graphql_input_types = (GraphQLScalarType , GraphQLEnumType , GraphQLInputObjectType )
1753
-
1754
- GraphQLInputType : TypeAlias = Union [
1755
- GraphQLScalarType , GraphQLEnumType , GraphQLInputObjectType , GraphQLWrappingType
1756
- ]
1757
-
1758
-
1759
- def is_input_type (type_ : Any ) -> TypeGuard [GraphQLInputType ]:
1760
- return isinstance (type_ , graphql_input_types ) or (
1761
- isinstance (type_ , GraphQLWrappingType ) and is_input_type (type_ .of_type )
1762
- )
1763
-
1764
-
1765
- def assert_input_type (type_ : Any ) -> GraphQLInputType :
1766
- if not is_input_type (type_ ):
1767
- raise TypeError (f"Expected { type_ } to be a GraphQL input type." )
1768
- return type_
1769
-
1770
-
1771
- # These types may be used as output types as the result of fields.
1772
-
1773
- graphql_output_types = (
1774
- GraphQLScalarType ,
1775
- GraphQLObjectType ,
1776
- GraphQLInterfaceType ,
1777
- GraphQLUnionType ,
1778
- GraphQLEnumType ,
1779
- )
1780
-
1781
- GraphQLOutputType : TypeAlias = Union [
1782
- GraphQLScalarType ,
1783
- GraphQLObjectType ,
1784
- GraphQLInterfaceType ,
1785
- GraphQLUnionType ,
1786
- GraphQLEnumType ,
1787
- GraphQLWrappingType ,
1788
- ]
1789
-
1790
-
1791
- def is_output_type (type_ : Any ) -> TypeGuard [GraphQLOutputType ]:
1792
- return isinstance (type_ , graphql_output_types ) or (
1793
- isinstance (type_ , GraphQLWrappingType ) and is_output_type (type_ .of_type )
1794
- )
1795
-
1796
-
1797
- def assert_output_type (type_ : Any ) -> GraphQLOutputType :
1798
- if not is_output_type (type_ ):
1799
- raise TypeError (f"Expected { type_ } to be a GraphQL output type." )
1800
- return type_
1801
-
1802
-
1803
1819
# These named types do not include modifiers like List or NonNull.
1804
1820
1805
1821
GraphQLNamedInputType : TypeAlias = Union [
@@ -1847,13 +1863,11 @@ def get_named_type(type_: Optional[GraphQLType]) -> Optional[GraphQLNamedType]:
1847
1863
1848
1864
# These types may describe types which may be leaf values.
1849
1865
1850
- graphql_leaf_types = (GraphQLScalarType , GraphQLEnumType )
1851
-
1852
1866
GraphQLLeafType : TypeAlias = Union [GraphQLScalarType , GraphQLEnumType ]
1853
1867
1854
1868
1855
1869
def is_leaf_type (type_ : Any ) -> TypeGuard [GraphQLLeafType ]:
1856
- return isinstance (type_ , graphql_leaf_types )
1870
+ return isinstance (type_ , ( GraphQLScalarType , GraphQLEnumType ) )
1857
1871
1858
1872
1859
1873
def assert_leaf_type (type_ : Any ) -> GraphQLLeafType :
@@ -1864,15 +1878,15 @@ def assert_leaf_type(type_: Any) -> GraphQLLeafType:
1864
1878
1865
1879
# These types may describe the parent context of a selection set.
1866
1880
1867
- graphql_composite_types = (GraphQLObjectType , GraphQLInterfaceType , GraphQLUnionType )
1868
-
1869
1881
GraphQLCompositeType : TypeAlias = Union [
1870
1882
GraphQLObjectType , GraphQLInterfaceType , GraphQLUnionType
1871
1883
]
1872
1884
1873
1885
1874
1886
def is_composite_type (type_ : Any ) -> TypeGuard [GraphQLCompositeType ]:
1875
- return isinstance (type_ , graphql_composite_types )
1887
+ return isinstance (
1888
+ type_ , (GraphQLObjectType , GraphQLInterfaceType , GraphQLUnionType )
1889
+ )
1876
1890
1877
1891
1878
1892
def assert_composite_type (type_ : Any ) -> GraphQLType :
@@ -1883,13 +1897,11 @@ def assert_composite_type(type_: Any) -> GraphQLType:
1883
1897
1884
1898
# These types may describe abstract types.
1885
1899
1886
- graphql_abstract_types = (GraphQLInterfaceType , GraphQLUnionType )
1887
-
1888
1900
GraphQLAbstractType : TypeAlias = Union [GraphQLInterfaceType , GraphQLUnionType ]
1889
1901
1890
1902
1891
1903
def is_abstract_type (type_ : Any ) -> TypeGuard [GraphQLAbstractType ]:
1892
- return isinstance (type_ , graphql_abstract_types )
1904
+ return isinstance (type_ , ( GraphQLInterfaceType , GraphQLUnionType ) )
1893
1905
1894
1906
1895
1907
def assert_abstract_type (type_ : Any ) -> GraphQLAbstractType :
0 commit comments