简体   繁体   English

Graphene/Flask/SQLAlchemy - 从路由入口点检索数据的推荐方法是什么?

[英]Graphene/Flask/SQLAlchemy - What is the recommended method to retrieve data from a route entry point?

Given a basic project structure as follows:给定一个基本的项目结构如下:

/
 app.py     <-- Flask app startup and basic routing
 models.py 
 schema.py  <-- Contains Graphene/SQLAlchemy schema definitions for queries/mutations

Say in my app.py I have some basic routes setup like so:在我的app.py中说,我有一些基本的路线设置,如下所示:

@app.route('/members/list', methods=['GET'])
def members():
    # What should I do here?

What is the "correct" way to retrieve data?检索数据的“正确”方法是什么? I can see a few different approaches, but I'm not sure if there's a recommended way and I can't seem to find a straightforward answer.我可以看到几种不同的方法,但我不确定是否有推荐的方法,而且我似乎找不到一个直截了当的答案。 For example:例如:

  1. return jsonify(session.query(MembersModel).all()) I feel that this is probably the right way, but it feels weird plopping this down right at the route (feels like I'm missing some service layer architecture) or that I'm not using schema.py correctly. return jsonify(session.query(MembersModel).all())我觉得这可能是正确的方法,但是在路线上直接把它放下感觉很奇怪(感觉就像我错过了一些服务层架构)或者我'没有正确使用schema.py If I were to go this method, does this sit with in my schema.py ?如果我要使用 go 这个方法,这个方法在我的schema.py中吗? Or should I be making a different service-esque file elsewhere?还是我应该在其他地方制作不同的服务式文件?

  2. Running a GraphQL query directly by myself like schema.execute('{ allMembers {... } }') via Graphene (as seen here ) and then parsing my result back in a response.我自己直接运行 GraphQL 查询,例如schema.execute('{ allMembers {... } }')通过 Graphene (如此处所示),然后在响应中解析我的结果。 This feels... wrong, having hardcoded GraphQL in my code when there's a better alternative in #1.这感觉......错了,在我的代码中硬编码 GraphQL,而在 #1 中有更好的选择。

I have prior experience with Spring and I always did it with an MVC styled controller <-> service <-> dao , but I'm not sure what the Flask/Graphene/SQLAlchemy/GraphQL/SQLite equivalent is.我之前有使用 Spring 的经验,我总是使用 MVC 风格的controller <-> service <-> dao ,但我不确定 Flask/Graphene/SQLAlchemy/GraphQL/SQLite 等价物是什么。 I have this nagging feeling that I'm missing an obvious answer here, so if anyone could direct me to some resources or help out, I'd appreciate it.我有一种烦人的感觉,我在这里错过了一个明显的答案,所以如果有人可以指导我获取一些资源或提供帮助,我将不胜感激。

Thanks!谢谢!

Okay, after hours of reading I finally realized it: I'm not supposed to be playing between REST web api's and GraphQL like this (disregarding legacy systems/migrations/etc).好的,经过几个小时的阅读,我终于意识到:我不应该在 REST web api 和 Z524DE3D2ADE4544176F60702B36FBDFDFZ 之间玩(忽略遗留系统/等) Essentially, GraphQL loosely competes with REST sort of in the vein with how JSON competes with XML.从本质上讲,GraphQL 与 REST 与 JSON 与 Z350167103D39CFEDF8 的竞争方式大致相同

I was under the impression that GraphQL was comparable to a higher-level SQL, wherein GraphQL sat above my SQLite layer and abstracted away traditional SQL with some new-fangled terminology and abstractions like relays and connections . I was under the impression that GraphQL was comparable to a higher-level SQL, wherein GraphQL sat above my SQLite layer and abstracted away traditional SQL with some new-fangled terminology and abstractions like relays and connections . Instead, GraphQL competes at an even higher level as mentioned earlier.相反,如前所述,GraphQL 在更高的水平上竞争。

So, when dealing with Flask, GraphQL and Graphene, the most I should be doing is execute queries via the GraphiQL interface or POST'em to my server directly - and not do something like GET /my-path/some-resource just to manually hit a GraphQL query somewhere in the backend.因此,在处理 Flask、GraphQL 和 Graphene 时,我应该做的最多的是通过 GraphiQL 接口执行查询或直接将它们发送到我的服务器 - 而不是GET /my-path/some-resource那样手动执行在后端某处点击 GraphQL 查询。

Of course, if I misinterpreted anything, please let me know!当然,如果我误解了什么,请告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM