graphene-django
graphene-django copied to clipboard
Is there a way to elegantly use a django model as an input object type?
Say I have,
class PersonNode(DjangoObjectType):
class Meta:
model = Person
fields = ('first_name', 'last_name', 'age',
'sex', 'alive', 'unique_identifier',)
filter_fields = ('first_name', 'last_name', 'age',
'sex', 'alive', 'unique_identifier',)
interfaces = (graphene.relay.Node,)
class PersonNodeInput(graphene.InputObjectType):
first_name = graphene.String()
last_name = graphene.String()
# rest of person model fields
class Valid(graphene.ObjectType):
ok = graphene.Boolean()
class Query(graphene.ObjectType):
validate_person = graphene.Field(Valid, args={
"data": PersonNodeInput()})
person = graphene.relay.Node.Field(PersonNode)
all_people = DjangoFilterConnectionField(PersonNode)
def resolve_validate_person(root, info, data):
print("resolve validate person")
return Valid(ok=True)
Is it possible to avoid writing out PersonNodeInput? It would be nice if you could subclass something like "DjangoInputObjectType" and specify the model and fields you want in a Meta attribute.
SO question - https://stackoverflow.com/questions/66286436/is-there-a-way-to-elegantly-use-a-django-model-as-an-input-object-type
There isn't a class for that, it might be a good addition. graphene_django.types.construct_fields
takes a model and outputs graphene fields.
In graphene-django v3.1.1 there's a new DjangoFormInputObjectType
which could work for your purpose. Docs here (added in https://github.com/graphql-python/graphene-django/pull/1325).
There's also the great https://github.com/tOgg1/graphene-django-cud library which may accomplish what you need.