graphene: Can't use custom field as input in mutation

I have a custom field (written with @syrusakbary help in this issue):

from functools import partial

class ProtoEnum(graphene.Field):
    def __init__(self, protobuf_enum, *args, **kwargs):
        super(ProtoEnum, self).__init__(graphene.Int, *args, **kwargs)
        self.protobuf_enum = protobuf_enum

    def get_resolver(self, parent_resolver):
        return partial(_enum_resolver, self.protobuf_enum, parent_resolver)

def _enum_resolver(protobuf_enum, parent_resolver, root, args, context, info):
    resolved_value = parent_resolver(root, args, context, info)
    if resolved_value not in protobuf_enum.values():
        raise ValueError('Enum value "%s" is invalid' % resolved_value)
    return resolved_value

I wanted to use it as a input in my mutation like so:

class Input:
    my_field = ProtoEnum(...)

Which gave me this error:

File "third_party/python3/graphene/types/field.py", line 54, in __init__
  File "third_party/python3/graphene/types/argument.py", line 61, in to_arguments
ValueError: Expected my_field to be Argument, but received ProtoEnum. Try using Argument(Int!).

I tried wrapping it in Argument, which in turn gave me this error:

File "third_party/python3/graphene/types/schema.py", line 44, in __init__
  File "third_party/python3/graphene/types/schema.py", line 105, in build_typemap
  File "third_party/python3/graphene/types/typemap.py", line 65, in __init__
  File "third_party/python3/graphql/type/typemap.py", line 16, in __init__
  File "third_party/python3/graphene/types/typemap.py", line 73, in reducer
  File "third_party/python3/graphene/types/typemap.py", line 103, in graphene_reducer
  File "third_party/python3/graphql/type/typemap.py", line 79, in reducer
  File "third_party/python3/graphql/pyutils/cached_property.py", line 16, in __get__
  File "third_party/python3/graphql/type/definition.py", line 180, in fields
  File "third_party/python3/graphql/type/definition.py", line 189, in define_field_map
  File "third_party/python3/graphene/types/typemap.py", line 246, in construct_fields_for_type
  File "third_party/python3/graphene/types/typemap.py", line 295, in get_field_type
AttributeError: 'ProtoEnum' object has no attribute '_meta'

I tried adding a Meta class but that didn’t do anything. I can use custom field just fine in my schema otherwise.

Am I doing something wrong or is this just not supported?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18 (3 by maintainers)

Most upvoted comments

I’m getting similar errors using a subclass of the built in Enum type, seems like using it in inputs is not supported to me. I found this comment from over a year ago but it no longer seems to work.

I’ve tried all of the following (and wrapped with Argument too) and received an error every time

class Input:
    type = ProjectTypeEnum(required=False)
class Input:
    type = ProjectTypeEnum(required=False).InputField()
class Input:
    type = ProjectTypeEnum(required=False).Field()