用于嵌套自定义对象的石墨烯解析器

Posted

技术标签:

【中文标题】用于嵌套自定义对象的石墨烯解析器【英文标题】:Graphene resolver for nested custom object 【发布时间】:2019-06-13 01:00:18 【问题描述】:

我正在尝试使用 Graphene 对以下数据进行建模:MessageFrequency 对象,表示在给定日期为特定聊天发送的消息数,MessageFrequencyRange 对象表示消息数每天发送的日期范围,全部用于给定的聊天。

我已经尝试创建自定义类,并且能够通过关注this *** 帖子获得可查询的MessageFrequency 对象,但我对如何让嵌套的自定义对象工作感到困惑。以下是我当前的代码。

class MessageFrequency(graphene.ObjectType):

    chat_id   = graphene.Int()
    date      = graphene.Date()
    frequency = graphene.Int()


class MessageFrequencyRange(graphene.ObjectType):

    chat_id     = graphene.Int()
    start_date  = graphene.Date()
    end_date    = graphene.Date()
    frequencies = graphene.NonNull(graphene.List(graphene.NonNull(MessageFrequency)))

    def resolve_frequencies(self, info):
        return [
            # some method to instantiate a MessageFrequency object...
            # Query.resolve_message_frequency ?
        ]

class Query(graphene.ObjectType):

    message_frequency = graphene.Field(
        MessageFrequency,
        chat_id=graphene.Int(required=True),
        date=graphene.Date(required=True),
    )
    message_frequency_range = graphene.Field(
        MessageFrequencyRange,
        chat_id=graphene.Int(required=True),
    )

    def resolve_message_frequency(self, info, chat_id, date):
        # test data for now
        return MessageFrequency(
            chat_id=9000,
            date=date,
            frequency=1
        )

    def resolve_message_frequency_range(self, info):
        # Should be creating a MessageFrequencyRange object, but not sure how to fill
        # in the frequencies attribute which isn't a scalar
        return None

我不确定我应该如何解析Query 中的MessageFrequencyRange 对象,因为它比解析MessageFrequency 对象更复杂(因为它具有非标量字段)。我也不确定如何解析MessageFrequencyRangefrequencies 属性,我想我应该使用之前编写的解析器,但我不确定如何调用它。

任何帮助将不胜感激!

【问题讨论】:

【参考方案1】:

我昨天做了一个嵌套对象。我会尽力回答。如果你发布你的模型会很好。

 class MessageFrequencyType(DjangoObjectType):
     class Meta:
        model = MessageFrequency # Not sure about the name of your model


 class MessageFrequencyRangeType(DjangoObjectType):
    class Meta:
       model = MessageFrequencyRange # Not sure about the name of your model


class Query(graphene.ObjectType):
    message_frequency = graphene.List(MessageFrequencyType)
    message_frequency_range = graphene.List(MessageFrequencyRangeType)

    def resolve_message_frequency(self, info, **kwargs):
        return MessageFrequency.objects.all()

    def resolve_message_frequency_range(self, info, **kwargs):
        return MessageFrequencyRange.objects.all()


class CreateMessageFrequency(graphene.Mutation):
    chat_id = graphene.Int()
    date = graphene.Date()
    frequency = graphene.Int()

   class Arguments:
       chat_id = graphene.Int()
       date = graphene.Date()
       frequency = graphene.Int()

   def mutate(self, info, chat_id, date, frequency):
       message_frequency = MessageFrequency(
           chat_id=chat_id,
           date=date,
           frequency=frequency,
       )
       message_frequency.save()

   return CreateMessageFrequency(
       chat_id=message_frequency.chat_id,
       date=message_frequency.date,
       frequency=message_frequency.frequency
   )


class CreateMessageFrequencyRange(graphene.Mutation):
    chat_id = graphene.Int()
    start_date = graphene.Date()
    end_date = graphene.Date()
    frequencies = graphene.Field(MessageFrequencyType)

   class Arguments:
       chat_id = graphene.Int()
       start_date = graphene.Date()
       end_date = graphene.Date()
       frequencies = graphene.Int()

   def mutate(self, info, chat_id, start_date, end_date, frequencies):
       chat_id=chat_id
       start_date=start_date
       end_date=end_date
       frequencies = MessageFrequency.objects.filter(id=frequencies).first()
       if not frequencies:
          raise Exception('Message Frequency not found')

   MessageFrequencyRange.objects.create(
       chat_id=chat_id,
       start_date=start_date,
       end_date=end_date,
       frequencies=frequencies
   )

   return CreateMessageFrequencyRange(
       chat_id=chat_id,
       start_date=start_date,
       end_date=end_date,
       frequencies=frequencies
   )


class Mutation(graphene.ObjectType):
    create_message_frequency = CreateMessageFrequency.Field()
    create_message_frequency_range = CreateMessageFrequencyRange.Field()

【讨论】:

我正在尝试在不使用 Django 或 Flask+SqlAlchemy 绑定的情况下进行此操作,因此我没有任何模型。在不使用 Django 的情况下,这样的东西是否可以工作? 不确定。

以上是关于用于嵌套自定义对象的石墨烯解析器的主要内容,如果未能解决你的问题,请参考以下文章

石墨烯和 SQLAlchemy:派生自基础对象类型

如何动态创建石墨烯对象?例如,我想在运行时根据配置文件添加字段和解析器

石墨烯 - GraphQL:“请求”对象没有属性“上下文”

石墨烯区块链智能合约

Django 石墨烯中继限制对用户拥有的对象的查询

Django 石墨烯,按对象过滤