2

我想设置一个石墨烯 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
4

1 回答 1

2
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

这将返回以下内容:

OrderedDict([('domainQuery', OrderedDict([('domain', u'a.com'), ('host', OrderedDict([('name', 'The Host for a.com')])), ('registrar', OrderedDict([('name', 'The Registrar for a.com')]))]))])
于 2017-02-10T18:17:14.603 回答