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

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

问题描述

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



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

  class TextLogErrorGraph(DjangoObjectType):

def bug_suggestions_resolver(root,args ,上下文,信息):来自treeherder.model的
导入error_summary
返回error_summary.bug_suggestions_line(root)

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

注意,我不知道类型字段使用。有人能帮我吗? :)

解决方案

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天全站免登陆