【问题标题】:How to access a parameter of one resolver in another?如何在另一个解析器中访问一个解析器的参数?
【发布时间】:2023-03-22 06:42:01
【问题描述】:

这是代码 sn -p 我希望访问 Product 类中的 instanceName 属性

class Product(graphene.ObjectType):
    productName=graphene.String()
    productDependecies=graphene.List(ProductDependency)
   '''
                     I wish to access the instanceName here in this class How Can I do it 
   '''
  class Instance(graphene.ObjectType):
    instanceName=graphene.String()
    products=graphene.List(Product)
    def resolve_products(self,info):
       #some code that follows

【问题讨论】:

    标签: graphql graphene-python


    【解决方案1】:

    在您的resolve_products 方法中,当您实例化Product 实例列表时,您可以将实例名称传递给每个Product

    class Product(graphene.ObjectType):
        instance_name = graphene.String()
        ...
    
    class Instance(graphene.ObjectType):
        instance_name = graphene.String()
        ...
        def resolve_products(self, info):
            # return list of Product objects here, where
            # product = Product(instance_name=self.instance_name ...)
    

    建议您将值蛇形化并让石墨烯为您转换为驼峰形,在我的示例中,我使用蛇形大小写。

    这不是唯一的解决方案,但应该很简单。

    【讨论】:

      猜你喜欢
      • 2020-08-02
      • 2017-05-26
      • 2013-12-15
      • 2017-12-11
      • 2021-02-26
      • 2018-05-13
      • 2019-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多