import json

from graphene.types.generic import GenericScalar
from graphene_django import DjangoObjectType

from vbv_lernwelt.core.models import User


class UserType(DjangoObjectType):
    class Meta:
        model = User
        fields = (
            "id",
            "username",
            "first_name",
            "last_name",
            "email",
            "avatar_url",
            "language",
        )


class JSONStreamField(GenericScalar):
    @staticmethod
    def serialize(stream_value):
        if stream_value is None:
            return None
        return json.loads(json.dumps([block for block in stream_value.raw_data]))
