【问题标题】:access content of parent's field in graphene python在石墨烯python中访问父字段的内容
【发布时间】:2019-11-18 03:27:27
【问题描述】:

我在 python 中使用石墨烯。

假设我有以下架构:

extends type Query {
    a(search:String):A 
}

type A {
    b:B
    important_info:ID
}

type B {
  fieldone: String
  fieldtwo: String
}

现在我想查询:

query {
   a(search:"search string") {
       b {
            fieldone
         }
   }
}

但是fieldone 是基于important_info

我的班级B 是这样的:

class B(graphene.ObjectType):
    fieldone = graphene.String()
    fieldtwo = graphene.String()

    def resolve_fieldone(self,info):
        # Here I want access to important_info, but I don't know how ...

        return "something based on important_info"

如何从fieldone 的解析器中访问important info

【问题讨论】:

    标签: graphql graphene-python


    【解决方案1】:

    似乎没有针对此要求的明显或记录在案的解决方案。

    我通过将根对象添加到最外层类型中的info.context 来解决它:

    class B(ObjectType):
        c = String()
    
        def resolve_c(parent, info):
            return 'foo' if info.context['z'] == '' else 'bar'
    
    class A(ObjectType):
        b = Field(B)
    
        def resolve_b(parent, info):
            return parent.b
    
    class Query(ObjectType):
        a = Field(A)
        z = String()
    
        def resolve_a(parent, info):
            return some_function_to_get_a()
    
    
        def resolve_z(parent, info):
            z = some_function_to_get_z()
            info.context['z'] = z
    
            return z
    

    【讨论】:

      猜你喜欢
      • 2019-11-30
      • 2019-04-03
      • 2020-08-11
      • 2018-04-07
      • 2017-05-13
      • 2017-05-11
      • 2022-01-24
      • 2019-12-20
      • 2019-08-08
      相关资源
      最近更新 更多