Skip to content

Commit

Permalink
hack: try out some gql annotation ideas
Browse files Browse the repository at this point in the history
  • Loading branch information
parrotmac committed Dec 17, 2023
1 parent 60daa0f commit 5591be7
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
67 changes: 67 additions & 0 deletions gifter/graphql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from . import models

import inspect, sys
from pprint import pprint

schema = {}


class String:
pass


class Int:
pass


class UUID:
pass


class JSON:
pass


class DateTime:
pass


class Boolean:
pass


for name, obj in inspect.getmembers(sys.modules[models.__name__]):
if inspect.isclass(obj):
if hasattr(obj, "_meta") and not obj._meta.abstract:
if not hasattr(obj, 'GQLMeta'):
continue

obj_gql_name = f"{obj.__name__}Type"
schema[obj_gql_name] = {}
fields = obj._meta.fields
for field in fields:

if not hasattr(field, "__graphql_enabled"):
continue

match field.get_internal_type():
case "CharField" | "TextField":
schema[obj_gql_name][field.name] = String
case "IntegerField":
schema[obj_gql_name][field.name] = Int
case "UUIDField":
schema[obj_gql_name][field.name] = UUID
case "JSONField":
schema[obj_gql_name][field.name] = JSON
case "DateTimeField":
schema[obj_gql_name][field.name] = DateTime
case "BooleanField":
schema[obj_gql_name][field.name] = Boolean
case "ForeignKey" | "AutoField":
pprint(field)
case _:
raise Exception(f"Unknown field type: {field.get_internal_type()}")


def foo():
pprint(schema)
15 changes: 14 additions & 1 deletion gifter/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import hashlib
from dataclasses import dataclass
from typing import Any
import uuid
import secrets

Expand Down Expand Up @@ -84,8 +86,19 @@ def __str__(self):
return self.display_name


def with_graphql(field):
setattr(field, "__graphql_enabled", True)
return field


class Wishlist(CommonBaseClass):
title = models.CharField(max_length=120, blank=False, null=False)

@dataclass
class GQLMeta:
enabled = True
auth_check_via = "groups"

title = with_graphql(models.CharField(max_length=120, blank=False, null=False))
owner = models.ForeignKey(User, on_delete=models.CASCADE)
group = models.ForeignKey(Group, on_delete=models.CASCADE)

Expand Down
7 changes: 7 additions & 0 deletions gifter/test_graphql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.test import TestCase
from . import graphql

class TestGraphql(TestCase):

def test_graphql(self):
graphql.foo()

0 comments on commit 5591be7

Please sign in to comment.