KundaPanda / strawberry-django-jwt

[UNMAINTAINED] JSON Web Token (JWT) authentication for Django with Strawberry GraphQL
MIT License
37 stars 15 forks source link

gives this error ImportError: cannot import name 'is_async' from 'strawberry_django.utils' #414

Open deepak7448 opened 1 year ago

deepak7448 commented 1 year ago

Screenshot from 2023-07-24 14-15-30

When I'm importing this - from strawberry_django_jwt.decorators import login_required then its gives this error ImportError: cannot import name 'is_async' from 'strawberry_django.utils'

Aeonian9 commented 8 months ago

Workaround when upgrading package versions and not running async Django.

Run this code before strawberry_django_jwt is initiated. For example in the beginning of urls.py.

import strawberry_django.utils
def fix_is_async(*args, **kwargs):
    return False
strawberry_django.utils.is_async = fix_is_async
advl commented 2 months ago

Here's how I fixed it

import asyncio

def is_async() -> bool:                                                         
    """
    Helper function to determine if the current context is asynchronous.

    :return: True if the current asyncio event loop is running, False otherwise.
    """
    try:
        loop = asyncio.get_running_loop()
        return loop.is_running()
    except RuntimeError:
        return False
    return False

Currently working on a fork lib towards a stable django 4 & 5 only build, updating deps, keeping the same API surface.

In case you're trying to do the same at home,

  1. I have not encountered any issue in getting the lib to work on Django 5 & the newest versions of strawberry libs aside from the one on this post.
  2. Deps updates are probably the most tricky, and dropping support for Django 3 (not because intrinsically being incompatible, but because of the lock tree creating issues) helped
  3. This library is partially typed. Overall the codebase seems stable and clean, as it's been there under the graphene version for quite some time.
  4. The codebase has little documentation, but the original docs - for graphene - seem to me good quality.

If successful in getting something clean, will release it publicly. Lmk if interested or willing to contribute to that - setting up sth on discord ?