1from tartiflette.types.enum import GraphQLEnumType
2from tartiflette.types.input_object import GraphQLInputObjectType
3from tartiflette.types.interface import GraphQLInterfaceType
4from tartiflette.types.list import GraphQLList
5from tartiflette.types.non_null import GraphQLNonNull
6from tartiflette.types.object import GraphQLObjectType
7from tartiflette.types.scalar import GraphQLScalarType
8from tartiflette.types.union import GraphQLUnionType
9
10__all__ = (
11    "get_wrapped_type",
12    "is_scalar_type",
13    "is_enum_type",
14    "is_input_object_type",
15    "is_list_type",
16    "is_non_null_type",
17    "is_wrapping_type",
18    "is_input_type",
19    "is_abstract_type",
20    "is_leaf_type",
21    "is_object_type",
22)
23
24
25def get_wrapped_type(graphql_type: "GraphQLType") -> "GraphQLType":
26    """
27    Unwraps the GraphQL type and to return the inner type.
28    :param graphql_type: schema type to unwrap
29    :type graphql_type: GraphQLType
30    :return: the unwrapped inner schema type
31    :rtype: GraphQLType
32    """
33    inner_type = graphql_type
34    while inner_type.is_wrapping_type:
35        inner_type = inner_type.wrapped_type
36    return inner_type
37
38
39def is_scalar_type(graphql_type: "GraphQLType") -> bool:
40    """
41    Determines whether or not the "GraphQLType" is a scalar type.
42    :param graphql_type: schema type to test
43    :type graphql_type: GraphQLType
44    :return: whether or not the "GraphQLType" is a scalar type.
45    :rtype: bool
46    """
47    return isinstance(graphql_type, GraphQLScalarType)
48
49
50def is_enum_type(graphql_type: "GraphQLType") -> bool:
51    """
52    Determines whether or not the "GraphQLType" is an enum type.
53    :param graphql_type: schema type to test
54    :type graphql_type: GraphQLType
55    :return: whether or not the "GraphQLType" is an enum type.
56    :rtype: bool
57    """
58    return isinstance(graphql_type, GraphQLEnumType)
59
60
61def is_input_object_type(graphql_type: "GraphQLType") -> bool:
62    """
63    Determines whether or not the "GraphQLType" is an input object type.
64    :param graphql_type: schema type to test
65    :type graphql_type: GraphQLType
66    :return: whether or not the "GraphQLType" is an input object type.
67    :rtype: bool
68    """
69    return isinstance(graphql_type, GraphQLInputObjectType)
70
71
72def is_list_type(graphql_type: "GraphQLType") -> bool:
73    """
74    Determines whether or not the "GraphQLType" is a list type.
75    :param graphql_type: schema type to test
76    :type graphql_type: GraphQLType
77    :return: whether or not the "GraphQLType" is a list type.
78    :rtype: bool
79    """
80    return isinstance(graphql_type, GraphQLList)
81
82
83def is_non_null_type(graphql_type: "GraphQLType") -> bool:
84    """
85    Determines whether or not the "GraphQLType" is a non null type.
86    :param graphql_type: schema type to test
87    :type graphql_type: GraphQLType
88    :return: whether or not the "GraphQLType" is a non null type.
89    :rtype: bool
90    """
91    return isinstance(graphql_type, GraphQLNonNull)
92
93
94def is_wrapping_type(graphql_type: "GraphQLType") -> bool:
95    """
96    Determines whether or not the "GraphQLType" is either a list or non null
97    type.
98    :param graphql_type: schema type to test
99    :type graphql_type: GraphQLType
100    :return: whether or not the "GraphQLType" is either a list or non null
101    type.
102    :rtype: bool
103    """
104    return isinstance(graphql_type, (GraphQLList, GraphQLNonNull))
105
106
107def is_input_type(graphql_type: "GraphQLType") -> bool:
108    """
109    Determines whether or not the "GraphQLType" is an input type.
110    :param graphql_type: schema type to test
111    :type graphql_type: GraphQLType
112    :return: whether or not the "GraphQLType" is an input type.
113    :rtype: bool
114    """
115    return isinstance(
116        graphql_type,
117        (GraphQLScalarType, GraphQLEnumType, GraphQLInputObjectType),
118    ) or (
119        graphql_type.is_wrapping_type
120        and is_input_type(graphql_type.wrapped_type)
121    )
122
123
124def is_abstract_type(graphql_type: "GraphQLType") -> bool:
125    """
126    Determines whether or not the "GraphQLType" is an abstract type.
127    :param graphql_type: schema type to test
128    :type graphql_type: GraphQLType
129    :return: whether or not the "GraphQLType" is an abstract type.
130    :rtype: bool
131    """
132    return isinstance(graphql_type, (GraphQLInterfaceType, GraphQLUnionType))
133
134
135def is_leaf_type(graphql_type: "GraphQLType") -> bool:
136    """
137    Determines whether or not the "GraphQLType" is a leaf type.
138    :param graphql_type: schema type to test
139    :type graphql_type: GraphQLType
140    :return: whether or not the "GraphQLType" is a leaf type.
141    :rtype: bool
142    """
143    return isinstance(graphql_type, (GraphQLScalarType, GraphQLEnumType))
144
145
146def is_object_type(graphql_type: "GraphQLType") -> bool:
147    """
148    Determines whether or not the "GraphQLType" is an object type.
149    :param graphql_type: schema type to test
150    :type graphql_type: GraphQLType
151    :return: whether or not the "GraphQLType" is an object type.
152    :rtype: bool
153    """
154    return isinstance(graphql_type, GraphQLObjectType)
155