-
Notifications
You must be signed in to change notification settings - Fork 14
/
input_types.py
67 lines (51 loc) · 1.85 KB
/
input_types.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import functools
from typing import Union
from django.db import models
from django.db.models import Field
from django.db.models.fields.reverse_related import ForeignObjectRel
import graphene
from graphene import Scalar
from graphene.types.structures import Structure
from graphene_django.converter import convert_django_field_with_choices
from graphene_django.registry import Registry
from .types import UploadType
@functools.singledispatch
def get_input_field(
field: Union[Field, ForeignObjectRel], registry: Registry
) -> Union[Scalar, Structure]:
"""Convert a model field into a GraphQL input type used in mutations.
:param field: A model field.
:param registry: Registry which holds a mapping between django models/fields
and Graphene types.
:return: A scalar that can be used as an input field in mutations.
"""
return convert_django_field_with_choices(field, registry)
@get_input_field.register(models.FileField)
def get_file_field(field, registry):
return UploadType(
description=field.help_text,
)
@get_input_field.register(models.BooleanField)
def get_boolean_field(field, registry):
return graphene.Boolean(
description=field.help_text,
)
@get_input_field.register(models.ForeignKey)
@get_input_field.register(models.OneToOneField)
def get_foreign_key_field(field, registry):
return graphene.ID(
description=field.help_text,
)
@get_input_field.register(models.ManyToManyField)
def get_many_to_many_field(field, registry):
return graphene.List(
graphene.ID,
description=field.help_text,
)
@get_input_field.register(models.ManyToOneRel)
@get_input_field.register(models.ManyToManyRel)
def get_relation_field(field, registry):
return graphene.List(
graphene.ID,
description=f"Set list of {field.related_model._meta.verbose_name_plural}",
)