使用PythonGraphene设置一个值并得到几个值
Using Python Graphene to set a value and get several values
我想设置一个石墨烯 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
import graphene
class DomainName(graphene.Interface):
name = graphene.String()
domain = ''
class HostInfo(graphene.ObjectType):
class Meta:
interfaces = (DomainName,)
def resolve_name(self, args, context, info):
# Code to get the hosting provider
return 'The Host for {}'.format(self.domain)
class RegistrarInfo(graphene.ObjectType):
class Meta:
interfaces = (DomainName,)
def resolve_name(self, args, context, info):
# Code to get the registrar
return 'The Registrar for {}'.format(self.domain)
class DomainQuery(graphene.ObjectType):
host = graphene.Field(HostInfo)
registrar = graphene.Field(RegistrarInfo)
domain = graphene.String()
def resolve_host(self, args, context, info):
domain = HostInfo()
domain.domain = self.domain
return domain
def resolve_registrar(self, args, context, info):
domain = RegistrarInfo()
domain.domain = self.domain
return domain
class Query(graphene.ObjectType):
domain_query = graphene.Field(DomainQuery, domain=graphene.String(required=True))
def resolve_domain_query(self, args, context, info):
return DomainQuery(domain=args.get('domain'))
if __name__ == '__main__':
di = graphene.Schema(query=Query)
query = '''
query{
domainQuery(domain: "a.com") {
domain
host {
name
}
registrar {
name
}
}
}
'''
result = di.execute(query)
print result.data
这将 return 以下内容:
OrderedDict([('domainQuery', OrderedDict([('domain', u'a.com'), ('host', OrderedDict([('name', 'The Host for a.com')])), ('registrar', OrderedDict([('name', 'The Registrar for a.com')]))]))])
我想设置一个石墨烯 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
import graphene
class DomainName(graphene.Interface):
name = graphene.String()
domain = ''
class HostInfo(graphene.ObjectType):
class Meta:
interfaces = (DomainName,)
def resolve_name(self, args, context, info):
# Code to get the hosting provider
return 'The Host for {}'.format(self.domain)
class RegistrarInfo(graphene.ObjectType):
class Meta:
interfaces = (DomainName,)
def resolve_name(self, args, context, info):
# Code to get the registrar
return 'The Registrar for {}'.format(self.domain)
class DomainQuery(graphene.ObjectType):
host = graphene.Field(HostInfo)
registrar = graphene.Field(RegistrarInfo)
domain = graphene.String()
def resolve_host(self, args, context, info):
domain = HostInfo()
domain.domain = self.domain
return domain
def resolve_registrar(self, args, context, info):
domain = RegistrarInfo()
domain.domain = self.domain
return domain
class Query(graphene.ObjectType):
domain_query = graphene.Field(DomainQuery, domain=graphene.String(required=True))
def resolve_domain_query(self, args, context, info):
return DomainQuery(domain=args.get('domain'))
if __name__ == '__main__':
di = graphene.Schema(query=Query)
query = '''
query{
domainQuery(domain: "a.com") {
domain
host {
name
}
registrar {
name
}
}
}
'''
result = di.execute(query)
print result.data
这将 return 以下内容:
OrderedDict([('domainQuery', OrderedDict([('domain', u'a.com'), ('host', OrderedDict([('name', 'The Host for a.com')])), ('registrar', OrderedDict([('name', 'The Registrar for a.com')]))]))])