import json


class GraphqlResponseFlattenerMiddleware(object):
    """Flatten the Graphql responses in a one-level JSON object."""

    def __init__(self, get_response=None):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        try:
            res = json.loads(response.content.decode())
        except json.JSONDecodeError:
            return response

        if request.method == 'GET':
            # Use case for retrieval of non-exitent record
            if 'data' in res and list(res['data'].values())[0] is None:
                res = None
            # Use case for retrieval of all records
            elif 'data' in res and 'edges' in list(res['data'].values())[0]:
                res = list(list(res['data'].values())[0].values())[0]
                res = [list(item.values())[0] for item in res]
            # Use case for retrieval of a single existing record
            elif 'data' in res and isinstance(list(res['data'].values())[0], dict):
                res = list(res['data'].values())[0]
        elif request.method == 'POST':
            # Use case for creating, deleting and updating a record
            if 'data' in res and isinstance(list(list(res['data'].values())[0].values())[0], dict):
                res = list(list(res['data'].values())[0].values())[0]
            # Use case for deleting and updating a record
            elif 'data' in res and list(list(res['data'].values())[0].values())[0] is None:
                res = None

        response.content = json.dumps(res)

        return response

@@ -315,19 +315,23 @@
         
-res
+content
  = json.
@@ -440,16 +440,38 @@
      if 
+'data' in content and 
 request.
@@ -503,96 +503,93 @@
     
-# Use case for retrieval of non-exitent record%0A            if 'data' in res and list(res
+flattened_content = %7B%7D%0A            index = 0%0A            for resource in list(content
 %5B'da
@@ -606,48 +606,14 @@
 s())
-%5B0%5D is None
 :%0A    
-            res = None%0A
     
@@ -676,141 +676,420 @@
     
-elif 'data' in res and 'edges' in list(res%5B'data'%5D.values())%5B0%5D:%0A                res = list(list(res%5B'data'%5D.values())%5B0%5D.values())%5B0
+    if (resource and 'edges' in resource%0A                        and isinstance(resource%5B'edges'%5D, list)%0A                        and 'node' in resource%5B'edges'%5D%5B0%5D):%0A%0A                    flattened_content%5Blist(content%5B'data'%5D.keys())%5Bindex%5D%5D = %5B%5D%0A                    for item in list(resource.values())%5B0%5D:%0A                        flattened_content%5B%0A                            list(content%5B'data'%5D.keys())%5Bindex
 %5D%0A  
@@ -1106,15 +1106,25 @@
     
-res = %5B
+        %5D.append(
 list
@@ -1145,26 +1145,15 @@
 )%5B0%5D
- for item in res%5D%0A
+)%0A%0A    
     
@@ -1189,28 +1189,9 @@
 val 
-of a single existing
+a
  rec
@@ -1210,395 +1210,444 @@
     
+    
 elif 
-'data' in res and isinstance(list(res%5B'data'%5D.values())%5B0%5D, dict):%0A                res = list(res%5B'data'%5D.values())%5B0%5D%0A        elif request.method == 'POST':%0A            # Use case for creating, deleting and updating a record%0A            if 'data' in res and isinstance(list(list(res%5B'data'%5D.values())%5B0%5D.values())%5B0%5D, dict):%0A                res = list(list(res%5B'data'%5D.values())%5B0%5D
+resource is None or isinstance(resource, dict):%0A                    flattened_content%5Blist(content%5B'data'%5D.keys())%5Bindex%5D%5D = resource%0A%0A                index += 1%0A            content = flattened_content%0A        elif 'data' in content and request.method == 'POST':%0A            flattened_content = %7B%7D%0A            for resource in list(content%5B'data'%5D.values()):%0A                if resource:%0A                    item = list(resource
 .val
@@ -1672,145 +1672,133 @@
     
-# Use cas
+        if item is Non
 e 
-f
 or 
-deleting and updating a record%0A            elif 'data' in res and list(list(res%5B'data'%5D.values())%5B0%5D.value
+isinstance(item, dict):%0A                        flattened_content%5Blist(resource.key
 s())%5B0%5D
- is None:
+%5D = item
 %0A   
@@ -1810,22 +1810,35 @@
     
-    res = None
+content = flattened_content
 %0A%0A  
@@ -1877,13 +1877,16 @@
 mps(
-res)%0A
+content)
 %0A   

Refactor GraphqlResponseFlattenerMiddleware to support multiple requests in one round trip.