用于没有模型的对象的石墨烯解析器

2023-11-25

我正在尝试编写一个解析器,它返回由函数创建的对象。它从memcached获取数据,所以没有实际的model我可以把它绑起来。

我认为我的主要问题是我不知道什么type使用以及如何设置它。我将其与 Django 结合使用,但我不认为这是 django 问题(afaict)。到目前为止,这是我的代码:

class TextLogErrorGraph(DjangoObjectType):

    def bug_suggestions_resolver(root, args, context, info):
        from treeherder.model import error_summary
        return error_summary.bug_suggestions_line(root)

    bug_suggestions = graphene.Field(TypeForAnObjectHere, resolver=bug_suggestions_resolver)

注意我不知道什么type or field使用。有人能帮我吗? :)


GraphQL 被设计为与后端无关,而 Graphene 的构建是为了支持各种 Python 后端,例如Django and SQL炼金术。要集成您的自定义后端,只需使用 Graphene 定义您的模型类型系统并推出您自己的解析器。

import graphene
import time

class TextLogEntry(graphene.ObjectType):

    log_id = graphene.Int()
    text = graphene.String()
    timestamp = graphene.Float()
    level = graphene.String()

def textlog_resolver(root, args, context, info):
    log_id = args.get('log_id') # 123
    # fetch object...
    return TextLogEntry(
        log_id=log_id,
        text='Hello World',
        timestamp=time.time(),
        level='debug'
    )

class Query(graphene.ObjectType):

    textlog_entry = graphene.Field(
        TextLogEntry,
        log_id=graphene.Argument(graphene.Int, required=True),
        resolver=textlog_resolver
    )


schema = graphene.Schema(
    query=Query
)

Example query with GraphiQL

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

用于没有模型的对象的石墨烯解析器 的相关文章

随机推荐