import re

from graphql.utils.ast_to_dict import ast_to_dict

class Resolvers:
    @staticmethod
    def generic_resolver_list(graphene_object, args, info):
        mongo_object = graphene_object.__MODEL__

        fields = [k for k, v in get_fields(info).items() if k[:2] != '__']
        results = mongo_object.objects(**args).only(*fields)

        if results:
            def get_user_attrs(u):
                return {f: getattr(u, f) for f in fields}

            return [graphene_object(**get_user_attrs(u)) for u in results]
        else:
            return []

    @staticmethod
    def generic_resolver(graphene_object, args, info):
        mongo_object = graphene_object.__MODEL__

        fields = [k for k, v in get_fields(info).items() if k[:2] != '__']
        fields = [convert_camel_case(f) for f in fields]

        result = mongo_object.objects(**args).only(*fields).first()

        if result:
            args_with_data = {f: getattr(result, f) for f in fields}

            print(args_with_data)

            return graphene_object(**args_with_data)
        else:
            return None

def with_metaclass(*args, **kwargs):
    from six import with_metaclass as six_with_metaclass
    return six_with_metaclass(*args, **kwargs)

def convert_camel_case(string):
    res = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', string)
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', res).lower()


# author: mixxorz
def collect_fields(node, fragments):
    field = {}

    if node.get('selection_set'):
        for leaf in node['selection_set']['selections']:
            if leaf['kind'] == 'Field':
                field.update({
                    leaf['name']['value']: collect_fields(leaf, fragments)
                })
            elif leaf['kind'] == 'FragmentSpread':
                field.update(collect_fields(fragments[leaf['name']['value']], fragments))

    return field

# author: mixxorz
def get_fields(info):
    fragments = {}
    node = ast_to_dict(info.field_asts[0])

    for name, value in info.fragments.items():
        fragments[name] = ast_to_dict(value)

    return collect_fields(node, fragments)
@@ -1,15 +1,4 @@
-import re%0A%0A
 from
@@ -43,16 +43,73 @@
 to_dict%0A
+from graphene.utils.str_converters import to_snake_case%0A%0A
 %0Aclass R
@@ -825,29 +825,24 @@
 elds = %5B
-convert_camel
+to_snake
 _case(f)
@@ -1021,44 +1021,8 @@
 ds%7D%0A
-%0A            print(args_with_data)%0A%0A
     
@@ -1113,300 +1113,8 @@
 ne%0A%0A
-def with_metaclass(*args, **kwargs):%0A    from six import with_metaclass as six_with_metaclass%0A    return six_with_metaclass(*args, **kwargs)%0A%0Adef convert_camel_case(string):%0A    res = re.sub('(.)(%5BA-Z%5D%5Ba-z%5D+)', r'%5C1_%5C2', string)%0A    return re.sub('(%5Ba-z0-9%5D)(%5BA-Z%5D)', r'%5C1_%5C2', res).lower()%0A%0A
 %0A# a
@@ -1579,16 +1579,16 @@
 ents))%0A%0A
-
     retu
@@ -1597,16 +1597,17 @@
  field%0A%0A
+%0A
 # author

clear of utils