石墨烯中的Custom ConnectionField

时间:2017-09-12 14:51:23

标签: relay graphene-python

我不了解如何在石墨烯中的ConnectionField中使用自定义字段。我有类似的东西:

class ShipConnection(Connection):
    extra = String()

    class Meta:
        node = Ship

SHIPS = ['Tug boat', 'Row boat', 'Canoe']

class Query(AbstractType):
    ships = relay.ConnectionField(ShipConnection)

    def resolve_ships(self, args, context, info):
        return ShipConnection(
            extra='Some extra text',
            edges=???
        )

通常情况下,你会说:

    def resolve_ships(self, args, context, info):
        return SHIPS

但是如何在额外的中返回一些列表?

2 个答案:

答案 0 :(得分:1)

答案结果是使用石墨烯的resolve_connection类的无证类方法,称为def resolve_ships(self, args, context, info): field = relay.ConnectionField.resolve_connection( ShipConnection, args, SHIPS ) field.extra = 'Whatever' return field 。以下作品:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
 
 
class MainForm(QDialog):
 
    def __init__(self, fn=None,parent=None):
        super(MainForm, self).__init__(parent,\
           flags=Qt.WindowMinimizeButtonHint|Qt.WindowMaximizeButtonHint)

答案 1 :(得分:1)

正确的方法是完全解释here

class Ship(graphene.ObjectType):
    ship_type = String()

    def resolve_ship_type(self, info):
         return self.ship_type

    class Meta:
          interfaces = (Node,)

class ShipConnection(Connection):
    total_count = Int() # i've found count on connections very useful! 

    def resolve_total_count(self, info):
        return get_count_of_all_ships()

    class Meta:
        node = Ship

    class Edge:
        other = String()
        def resolve_other(self, info):
            return "This is other: " + self.node.other

class Query(graphene.ObjectType):
    ships = relay.ConnectionField(ShipConnection)

    def resolve_ships(self, info):
        return get_ships_from_database_or_something_idk_its_your_implmentation()

schema = graphene.Schema(query=Query)

我不知道是否推荐这样做,但 resolve_total_count 方法也可以实现为:

def resolve_total_count(self, info):
    return len(self.iterable)

我不知道 iterable 属性是否记录在任何地方,但我在调查 Connection 类时能够找到它