Cam*_*ron 4 python django graphql
我正在尝试编写一个返回函数创建的对象的解析器.它从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)
Run Code Online (Sandbox Code Playgroud)
请注意我不知道使用什么type或field使用.有人能帮我吗?:)
Iva*_*hoo 12
GraphQL被设计为后端不可知,并且构建Graphene以支持各种python后端,如Django和SQLAlchemy.要集成您的自定义后端,只需使用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
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2271 次 |
| 最近记录: |