我正在尝试在我的 django 项目中探索 ariadne。但是,我感觉创建文件夹结构非常困难,因为我没有看到太多主要关注于此的示例。我也没有找到任何单独的教程。任何地方都以相同的方式完成,即将每个代码都放在 schema.py
中。
这是一个例子
from ariadne import QueryType, make_executable_schema
type_defs = """
type Query {
hello: String!
}
"""
query = QueryType()
@query.field("hello")
def resolve_hello(*_):
return "Hello world!"
schema = make_executable_schema(type_defs, query)
你会如何在一个大型 django 应用程序中设计你的文件夹,其中有超过 10、15 个应用程序,例如帐户、产品、评论等?如果我们使用普通的 django 那么它已经给出了以下结构
app_name
views.py
urls.py
models.py
但是如果我们想在 django 中使用 ariadne 并考虑每个应用程序的 CRUD 功能,您现在将如何设计您的项目?
最佳答案
我有同样的问题,最终在我的 Django 应用程序中拆分了我的架构和解析器,如下所示:
project
app_1
models.py
resolvers.py
schema.graphql
app_2
models.py
resolvers.py
schema.graphql # graphql types relevant to this app
project
wsgi.py
urls.py
settings.py
graphql_config.py # here I tie together all my schemas and resolvers
schema.py # this schema file has my root Query and Mutation types
在上面的示例中,我的 graphq_config.py
如下所示:
from ariadne import QueryType, make_executable_schema, load_schema_from_path,
import app_1.resolvers
import app_2.resolvers
type_defs = [
load_schema_from_path("project/schema.graphql"),
load_schema_from_path("app_1/schema.graphql"),
load_schema_from_path("app_2/schema.graphql"),
]
query = QueryType()
query.set_field("type_1", app_1.resolvers.type_1_resolver)
query.set_field("type_2", app_2.resolvers.type_2_resolver)
schema = make_executable_schema(type_defs, query)
无论如何,我在博客文章中更详细地写了它:https://perandrestromhaug.com/posts/guide-to-schema-first-graphql-with-django-and-ariadne/
关于python - 在 django 中使用 ariadne 时的文件夹设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59509069/