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)]}

[((134, 148), 'ariadne.MutationType', 'MutationType', ([], {}), '()\n', (146, 148), False, 'from ariadne import MutationType, convert_kwargs_to_snake_case\n'), ((359, 383), 'config.messages.append', 'messages.append', (['message'], {}), '(message)\n', (374, 383), False, 'from config import clients, messages, queue\n'), ((695, 734), 'logging.info', 'logging.info', (['f"""Client id: {client_id}"""'], {}), "(f'Client id: {client_id}')\n", (707, 734), False, 'import logging\n'), ((398, 416), 'config.queue.put', 'queue.put', (['message'], {}), '(message)\n', (407, 416), False, 'from config import clients, messages, queue\n'), ((750, 772), 'config.clients.get', 'clients.get', (['client_id'], {}), '(client_id)\n', (761, 772), False, 'from config import clients, messages, queue\n')]