old_contents: 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
  
new_contents: 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:
              content = json.loads(response.content.decode())
          except json.JSONDecodeError:
              return response
  
          if 'data' in content and request.method == 'GET':
              flattened_content = {}
              index = 0
              for resource in list(content['data'].values()):
                  # Use case for retrieval of all records
                  if (resource and 'edges' in resource
                          and isinstance(resource['edges'], list)
                          and 'node' in resource['edges'][0]):
  
                      flattened_content[list(content['data'].keys())[index]] = []
                      for item in list(resource.values())[0]:
                          flattened_content[
                              list(content['data'].keys())[index]
                          ].append(list(item.values())[0])
  
                  # Use case for retrieval a record
                  elif resource is None or isinstance(resource, dict):
                      flattened_content[list(content['data'].keys())[index]] = resource
  
                  index += 1
              content = flattened_content
          elif 'data' in content and request.method == 'POST':
              flattened_content = {}
              for resource in list(content['data'].values()):
                  if resource:
                      item = list(resource.values())[0]
                      if item is None or isinstance(item, dict):
                          flattened_content[list(resource.keys())[0]] = item
              content = flattened_content
  
          response.content = json.dumps(content)
          return response
  
subject: Refactor GraphqlResponseFlattenerMiddleware to support multiple requests in one round trip.