用于没有模型的对象的石墨烯解析器 [英] Graphene resolver for an object that has no model

查看:23
本文介绍了用于没有模型的对象的石墨烯解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个返回由函数创建的对象的解析器.它从 memcached 获取数据,因此没有我可以将其绑定到的实际 model.

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

class TextLogErrorGraph(DjangoObjectType):def bug_suggestions_resolver(root, args, context, info):从 treeherder.model 导入 error_summary返回 error_summary.bug_suggestions_line(root)bug_suggestions = graphene.Field(TypeForAnObjectHere,resolver=bug_suggestions_resolver)

注意我不知道要使用什么 typefield.有人能帮我吗?:)

解决方案

GraphQL 旨在与后端无关,并且 Graphene 旨在支持各种 Python 后端,例如

I'm trying to write a resolver that returns an object created by a function. It gets the data from memcached, so there is no actual model I can tie it to.

I think my main issue is I can't figure out what type to use and how to set it up. I'm using this in conjunction with Django, but I don't think it's a django issue (afaict). Here's my code so far:

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)

Notice I don't know what type or field to use. Can someone help me? :)

解决方案

GraphQL is designed to be backend agnostic, and Graphene is build to support various python backends like Django and SQLAlchemy. To integrate your custom backend, simply define your models using Graphene's type system and roll out your own resolvers.

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
)

这篇关于用于没有模型的对象的石墨烯解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆