[英]Graphene resolver for an object that has no model
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. 它从memcached获取数据,因此没有可以将其绑定的实际model
。
I think my main issue is I can't figure out what type
to use and how to set it up. 我认为我的主要问题是我无法弄清楚使用什么type
以及如何设置它。 I'm using this in conjunction with Django, but I don't think it's a django issue (afaict). 我和Django一起使用它,但我不认为这是一个django问题(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. 注意我不知道要使用什么type
或field
。 Can someone help me? 有人能帮我吗? :) :)
GraphQL is designed to be backend agnostic, and Graphene is build to support various python backends like Django and SQLAlchemy . GraphQL被设计为后端不可知,并且构建Graphene以支持各种python后端,如Django和SQLAlchemy 。 To integrate your custom backend, simply define your models using Graphene's type system and roll out your own resolvers. 要集成您的自定义后端,只需使用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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.