1
- from typing import Any , Dict , Callable , Optional , List
2
-
3
- from graphql .type .schema import GraphQLSchema
4
- from graphql .type .definition import GraphQLObjectType , GraphQLField , GraphQLScalarType
1
+ from graphql .type .definition import GraphQLObjectType , GraphQLScalarType
5
2
6
3
7
4
class TypeAdapter (object ):
@@ -23,15 +20,15 @@ class TypeAdapter(object):
23
20
apply(): pass in a GQL response to replace all instances of custom
24
21
scalar strings with their deserialized representation."""
25
22
26
- def __init__ (self , schema : GraphQLSchema , custom_types : Dict [ str , Any ] = {}) -> None :
23
+ def __init__ (self , schema , custom_types = {}):
27
24
""" schema: a graphQL schema in the GraphQLSchema format
28
25
custom_types: a Dict[str, Any],
29
26
where str is the name of the custom scalar type, and
30
27
Any is a class which has a `parse_value(str)` function"""
31
28
self .schema = schema
32
29
self .custom_types = custom_types
33
30
34
- def _follow_type_chain (self , node : Any ) -> Any :
31
+ def _follow_type_chain (self , node ) :
35
32
""" Get the type of the schema node in question.
36
33
37
34
In the GraphQL schema, GraphQLFields have a "type" property. However, often
@@ -47,15 +44,15 @@ def _follow_type_chain(self, node: Any) -> Any:
47
44
48
45
return field_type
49
46
50
- def _get_scalar_type_name (self , field : GraphQLField ) -> Optional [ str ] :
47
+ def _get_scalar_type_name (self , field ) :
51
48
"""Returns the name of the type if the type is a scalar type.
52
49
Returns None otherwise"""
53
50
node = self ._follow_type_chain (field )
54
51
if isinstance (node , GraphQLScalarType ):
55
52
return node .name
56
53
return None
57
54
58
- def _lookup_scalar_type (self , keys : List [ str ]) -> Optional [ str ] :
55
+ def _lookup_scalar_type (self , keys ) :
59
56
"""Search through the GQL schema and return the type identified by 'keys'.
60
57
61
58
If keys (e.g. ['film', 'release_date']) points to a scalar type, then
@@ -68,7 +65,7 @@ def _lookup_scalar_type(self, keys: List[str]) -> Optional[str]:
68
65
By default the root level is `schema.query`, if that fails, then we check
69
66
`schema.mutation`."""
70
67
71
- def traverse_schema (node : Any , lookup : List [ str ] ):
68
+ def traverse_schema (node , lookup ):
72
69
if not lookup :
73
70
return self ._get_scalar_type_name (node )
74
71
@@ -87,7 +84,7 @@ def traverse_schema(node: Any, lookup: List[str]):
87
84
except (KeyError , AttributeError ):
88
85
return None
89
86
90
- def _get_decoded_scalar_type (self , keys : List [ str ] , value : Any ) -> Any :
87
+ def _get_decoded_scalar_type (self , keys , value ) :
91
88
"""Get the decoded value of the type identified by `keys`.
92
89
93
90
If the type is not a custom scalar, then return the original value.
@@ -99,7 +96,7 @@ def _get_decoded_scalar_type(self, keys: List[str], value: Any) -> Any:
99
96
return self .custom_types [scalar_type ].parse_value (value )
100
97
return value
101
98
102
- def convert_scalars (self , response : Dict [ str , Any ]) -> Dict [ str , Any ] :
99
+ def convert_scalars (self , response ) :
103
100
"""Recursively traverse the GQL response
104
101
105
102
Recursively traverses the GQL response and calls _get_decoded_scalar_type()
@@ -110,7 +107,7 @@ def convert_scalars(self, response: Dict[str, Any]) -> Dict[str, Any]:
110
107
111
108
Builds a new tree with the substituted values so old `response` is not
112
109
modified."""
113
- def iterate (node : Any , keys : List [ str ] = []):
110
+ def iterate (node , keys = []):
114
111
if isinstance (node , dict ):
115
112
return {_key : iterate (value , keys + [_key ]) for _key , value in node .items ()}
116
113
elif isinstance (node , list ):
0 commit comments