2018-07-01 13:32:16 +03:00
|
|
|
from collections import Mapping, OrderedDict
|
2018-07-01 13:29:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
def deflate(node, index=None, path=None):
|
|
|
|
if index is None:
|
|
|
|
index = {}
|
|
|
|
if path is None:
|
|
|
|
path = []
|
|
|
|
|
|
|
|
if node and 'id' in node and '__typename' in node:
|
|
|
|
route = ','.join(path)
|
2018-07-01 13:32:16 +03:00
|
|
|
cache_key = ':'.join([route, str(node['__typename']), str(node['id'])])
|
2018-07-01 13:29:45 +03:00
|
|
|
|
2018-07-01 13:32:16 +03:00
|
|
|
if index.get(cache_key) is True:
|
2018-07-01 13:29:45 +03:00
|
|
|
return {
|
|
|
|
'__typename': node['__typename'],
|
|
|
|
'id': node['id'],
|
|
|
|
}
|
|
|
|
else:
|
2018-07-01 13:32:16 +03:00
|
|
|
index[cache_key] = True
|
2018-07-01 13:29:45 +03:00
|
|
|
|
|
|
|
field_names = node.keys()
|
|
|
|
result = OrderedDict()
|
|
|
|
|
|
|
|
for field_name in field_names:
|
|
|
|
value = node[field_name]
|
|
|
|
|
|
|
|
new_path = path + [field_name]
|
|
|
|
if isinstance(value, (list, tuple)):
|
|
|
|
result[field_name] = [
|
|
|
|
deflate(child, index, new_path) for child in value
|
|
|
|
]
|
|
|
|
elif isinstance(value, Mapping):
|
|
|
|
result[field_name] = deflate(value, index, new_path)
|
|
|
|
else:
|
|
|
|
result[field_name] = value
|
|
|
|
|
|
|
|
return result
|