我正在尝试为我的 graphene.ObjectType 子类创建一个元类。我的主要目标是从一些包含其名称的字符串列表中创建类的属性(即石墨烯对象的字段)。
例如。list = ['quantity', 'position']
对于这个列表,我的班级Attack
必须是这样的:
class Attack(graphene.ObjectType):
quantity = graphene.String()
position = graphene.String()
我这样做的方式如下:
import graphene
from graphene.types.objecttype import ObjectType, ObjectTypeMeta
class mytype(ObjectTypeMeta):
def __new__(cls, clsname, base, clsdict):
print "Hello"
setattr(cls, 'quantity', graphene.String())
return ObjectTypeMeta.__new__(cls, clsname, base, clsdict)
class combined_meta(mytype, ObjectTypeMeta):
pass
class Attack(graphene.ObjectType):
__metaclass__ = combined_meta
def __init__(self, dic):
self.quantity = "123"
print "Hello world"
print dic
class Query(graphene.ObjectType):
attack = graphene.Field(Attack)
def resolve_attack(self, args, context, info):
return Attack(dict())
schema = graphene.Schema(query = Query)
query = '''
{
attack
}
'''
result = schema.execute(query)
print result.data
我看到"Hello"
有很多错误..
AssertionError: Attack fields must be a mapping (dict / OrderedDict) with field names as keys or a function which returns such a mapping.
我是不是做错了什么……?