import logging
from ariadne import MutationType, convert_kwargs_to_snake_case
from config import clients, messages, queue

mutation = MutationType()


@mutation.field("createMessage")
@convert_kwargs_to_snake_case
async def resolve_create_message(obj, info, content, client_id):
    try:
        message = {"content": content, "client_id": client_id}
        messages.append(message)
        await queue.put(message)
        return {"success": True, "message": message}
    except Exception as error:
        return {"success": False, "errors": [str(error)]}


@mutation.field("createClient")
@convert_kwargs_to_snake_case
async def resolve_create_client(obj, info, client_id):
    try:
        logging.info(f"Client id: {client_id}")
        if not clients.get(client_id):
            client = {"client_id": client_id}
            clients[client_id] = client
            return {"success": True, "client": client}
        return {"success": False, "errors": ["Client is taken"]}

    except Exception as error:
        return {"success": False, "errors": [str(error)]}

[((5, 11, 5, 25), 'ariadne.MutationType', 'MutationType', ({}, {}), '()', False, 'from ariadne import MutationType, convert_kwargs_to_snake_case\n'), ((13, 8, 13, 32), 'config.messages.append', 'messages.append', ({(13, 24, 13, 31): 'message'}, {}), '(message)', False, 'from config import clients, messages, queue\n'), ((24, 8, 24, 47), 'logging.info', 'logging.info', ({(24, 21, 24, 46): 'f"""Client id: {client_id}"""'}, {}), "(f'Client id: {client_id}')", False, 'import logging\n'), ((14, 14, 14, 32), 'config.queue.put', 'queue.put', ({(14, 24, 14, 31): 'message'}, {}), '(message)', False, 'from config import clients, messages, queue\n'), ((25, 15, 25, 37), 'config.clients.get', 'clients.get', ({(25, 27, 25, 36): 'client_id'}, {}), '(client_id)', False, 'from config import clients, messages, queue\n')]