File: queries.py

package info (click to toggle)
strawberry-graphql-django 0.62.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,968 kB
  • sloc: python: 27,530; sh: 17; makefile: 16
file content (27 lines) | stat: -rw-r--r-- 599 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from __future__ import annotations

from typing import TYPE_CHECKING

from django.core.exceptions import ValidationError

import strawberry_django

from .utils import get_current_user

if TYPE_CHECKING:
    from strawberry.types import Info

    from strawberry_django.utils.typing import UserType


def resolve_current_user(info: Info) -> UserType:
    user = get_current_user(info)

    if not getattr(user, "is_authenticated", False):
        raise ValidationError("User is not logged in.")

    return user


def current_user():
    return strawberry_django.field(resolver=resolve_current_user)