我想设置一个石墨烯 python API,当提供域名时,它可以让用户查询主机、注册商或两者。此时,我只想能够以任何其他resolve
方法访问传入的域名。
似乎我不能self
用来存储传入的域名,因为它评估为None
.
对以下代码的任何建议将不胜感激......
import graphene
class DomainInput(graphene.InputObjectType):
dname = graphene.String()
class DomainName(graphene.ObjectType):
name = graphene.String()
class Host(graphene.ObjectType):
name = graphene.String()
class Registrar(graphene.ObjectType):
name = graphene.String()
class Query(graphene.ObjectType):
domainname = graphene.Field(Host, domain=DomainInput())
host = graphene.String()
registrar = graphene.String()
def __init__(self):
self.domain_name = ''
def resolve_domainname(self, args, context, info):
domain = args.get('domain')
self.domain_name = domain.get('dname')
return DomainName(name='For domain name: {}'.format(self.domain_name))
def resolve_host(self, args, context, info):
return Host(name='HOST for '.format(self.domain_name))
def resolve_registrar(self, args, context, info):
return Registrar(name='REGISTRAR for '.format(self.domain_name))
if __name__ == '__main__':
di = graphene.Schema(query=Query)
query = '''
query something{
domainname(domain: {dname:"a.com"}) { name }
host
registrar
}
'''
result = di.execute(query)
print result.data