@@ -20,7 +20,7 @@ class TypeAdaptor(object):
20
20
decoded value. All of this logic happens in `_substitute()`.
21
21
22
22
Public Interface:
23
- parse (): pass in a GQL response to replace all instances of custom
23
+ apply (): pass in a GQL response to replace all instances of custom
24
24
scalar strings with their deserialized representation."""
25
25
26
26
def __init__ (self , schema : GraphQLSchema , custom_scalars : Dict [str , Any ] = {}) -> None :
@@ -32,11 +32,12 @@ def __init__(self, schema: GraphQLSchema, custom_scalars: Dict[str, Any] = {}) -
32
32
self .custom_scalars = custom_scalars
33
33
34
34
def _follow_type_chain (self , node : Any ) -> Any :
35
- """In the schema GraphQL types are often listed with the format
36
- `obj.type.of_type...` where there are 0 or more 'of_type' fields before
37
- you get to the type you are interested in.
35
+ """ Get the type of the schema node in question.
38
36
39
- This is a convenience method to help us get to these nested types."""
37
+ In the GraphQL schema, GraphQLFields have a "type" property. However, often
38
+ that dict has an "of_type" property itself. In order to get to the actual
39
+ type, we need to indefinitely follow the chain of "of_type" fields to get
40
+ to the last one, which is the one we care about."""
40
41
if isinstance (node , GraphQLObjectType ):
41
42
return node
42
43
@@ -55,16 +56,17 @@ def _get_scalar_type_name(self, field: GraphQLField) -> Optional[str]:
55
56
return None
56
57
57
58
def _lookup_scalar_type (self , keys : List [str ]) -> Optional [str ]:
58
- """
59
- `keys` is a breadcrumb trail telling us where to look in the GraphQL schema.
60
- By default the root level is `schema.query`, if that fails, then we check
61
- `schema.mutation`.
59
+ """Search through the GQL schema and return the type identified by 'keys'.
62
60
63
- If keys (e.g. ['wallet ', 'balance ']) points to a scalar type, then
64
- this function returns the name of that type. (e.g. 'Money ')
61
+ If keys (e.g. ['film ', 'release_date ']) points to a scalar type, then
62
+ this function returns the name of that type. (e.g. 'DateTime ')
65
63
66
64
If it is not a scalar type (e..g a GraphQLObject or list), then this
67
- function returns None"""
65
+ function returns None.
66
+
67
+ `keys` is a breadcrumb trail telling us where to look in the GraphQL schema.
68
+ By default the root level is `schema.query`, if that fails, then we check
69
+ `schema.mutation`."""
68
70
69
71
def iterate (node : Any , lookup : List [str ]):
70
72
lookup = lookup .copy ()
@@ -83,24 +85,27 @@ def iterate(node: Any, lookup: List[str]):
83
85
return None
84
86
85
87
def _substitute (self , keys : List [str ], value : Any ) -> Any :
86
- """Looks in the GraphQL schema to find the type identified by 'keys'
88
+ """Get the decoded value of the type identified by `keys`.
89
+
90
+ If the type is not a custom scalar, then return the original value.
87
91
88
- If that type is not a custom scalar, we return the original value.
89
- If it is a custom scalar, we return the deserialized value, as
90
- processed by `<CustomScalarType>.parse_value()`"""
92
+ If it is a custom scalar, return the deserialized value, as
93
+ output by `<CustomScalarType>.parse_value()`"""
91
94
scalar_type = self ._lookup_scalar_type (keys )
92
95
if scalar_type and scalar_type in self .custom_scalars :
93
96
return self .custom_scalars [scalar_type ].parse_value (value )
94
97
return value
95
98
96
99
def _traverse (self , response : Dict [str , Any ], substitute : Callable ) -> Dict [str , Any ]:
97
- """Recursively traverses the GQL response and calls the `substitute`
100
+ """Recursively traverse the GQL response
101
+
102
+ Recursively traverses the GQL response and calls the `substitute`
98
103
function on all leaf nodes. The function is called with 2 arguments:
99
104
keys: List[str] is a breadcrumb trail telling us where we are in the
100
105
response, and therefore, where to look in the GQL Schema.
101
- value: Any is the value at that node in the tree
106
+ value: Any is the value at that node in the response
102
107
103
- Builds a new tree with the substituted values so `response` is not
108
+ Builds a new tree with the substituted values so old `response` is not
104
109
modified."""
105
110
def iterate (node : Any , keys : List [str ] = []):
106
111
if isinstance (node , dict ):
0 commit comments