首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >石墨烯-Django文件处理公约

石墨烯-Django文件处理公约
EN

Stack Overflow用户
提问于 2017-07-20 22:48:48
回答 2查看 802关注 0票数 0

我正在重建一个以前的Django REST项目,作为一个GraphQL项目。我现在有疑问&突变正常工作。

我的大部分学习来自于查看现有的Graphene-Django & Graphene-Python代码示例。他们之间似乎有很多矛盾之处。

在一些建议中,GraphQL查询应该放在schema.py中,而突变应该放在mutation.py中。

我认为更有意义的是让这两个文件保存各自的代码:- queries.py - mutations.py

虽然我对Django和Python还比较陌生,但我希望确保我没有违反任何约定。

对你的想法感兴趣!

罗伯特

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-21 06:00:40

目前还没有任何约定,因为GraphQL是一种替代REST的相当新方法。因此,“约定”是在我们说话的时候创造出来的。

但是,由于schema是通用定义的术语,所以可以将其重命名为queries

这是我的项目结构:

代码语言:javascript
复制
django_proj/
    manage.py
    requirements.txt
    my_app/
        __init__.py
        migrations/
        admin.py
        schema/
            __init__.py
            schema.py     # holds the class Query. The GraphQL endpoints, if you like
            types.py      # holds the DjangoObjectType classes
            inputs.py     # holds the graphene.InputObjectType classes (for defining input to a query or mutation)
            mutations.py  # holds the mutations (what else?!)

因此,如果您愿意,可以将schema.py (__init__)重命名为queries.py。这两个词之间没有太大的区别。

票数 1
EN

Stack Overflow用户

发布于 2017-09-29 21:14:48

我非常喜欢nik_m的答案,所以我编写了一些代码,从Django shell内部生成模板结构。当我一次又一次地创建这些文件时,我想要执行一些一致性。我把代码放在这里,以防别人发现它有用。

代码语言:javascript
复制
import os

from django.conf import settings


def schema_setup(app_name):
    """
    Sets up a default schema file structure.
    """
    SCHEMA_DIRECTORY_NAME = 'schema'
    app_directory = os.path.join(settings.PROJECT_DIR, app_name)
    if not os.path.exists(app_directory):
        raise Exception("Can't find app directory {}".format(app_directory))

    schema_directory = os.path.join(app_directory, SCHEMA_DIRECTORY_NAME)
    if os.path.exists(schema_directory):
        raise Exception("Schema directory {} already exists.".format(schema_directory))

    os.makedirs(schema_directory)
    mutation_class = "{}Mutation".format(app_name.title())
    query_class = "{}Query".format(app_name.title())

    init_txt = "from .mutations import {}\nfrom .queries import {}\n".format(mutation_class, query_class)
    fields_txt = "# Insert common fields here.\nimport graphene\n"
    inputs_txt = "# Insert graphene.InputObjectType classes.\nimport graphene\n"
    mutations_txt = "# Insert graphql mutations here.\nimport graphene\n\nclass {}(graphene.AbstractType):\n    pass\n".format(mutation_class)
    queries_txt = "# Insert graphql queries here.\nimport graphene\n\nclass {}(graphene.AbstractType):\n    pass\n".format(query_class)
    types_txt = "# Insert DjangoObjectType classes here.\nimport graphene\nfrom graphene_django.types import DjangoObjectType\n"

    for fname, file_text in [("__init__.py", init_txt),
                             ("fields.py", fields_txt),
                             ("inputs.py", inputs_txt),
                             ("mutations.py", mutations_txt),
                             ("queries.py", queries_txt),
                             ("types.py", types_txt),
                             ]:
        with open(os.path.join(schema_directory, fname), "w") as output_file:
            output_file.write(file_text)
        print("Created {}".format(fname))

从Django shell中运行,像schema_setup("my_app")一样运行

注意:

  • 这假定您在设置中设置了PROJECT_DIR,如PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
  • 在顶层模式中,导入类似于from my_app.schema import MyAppQuery, MyAppMutation
  • 我反复讨论过“查询”与“查询”和“变异”与“突变”--到目前为止,石墨烯文档并不一致。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45226398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档