繁体   English   中英

石墨烯-Django文件命名约定

[英]Graphene-Django Filenaming Conventions

我正在将以前的Django REST API项目重建为GraphQL项目。 我现在有查询和变异正常工作。

我的大部分学习来自查看现有的Graphene-Django和Graphene-Python代码示例。 它们之间似乎有很多不一致之处。

在某些建议中,应将GraphQL查询放置在schema.py而将突变放置在mutation.py

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

我对Django和Python比较陌生,因此请确保我没有违反任何约定。

对您的想法感兴趣!

罗伯特

目前还没有任何约定,因为GraphQL是REST的一种相当新的替代方法。 因此,在我们发言的那一刻就产生了“惯例”。

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

这是我的项目结构:

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如果你喜欢。 这两个词之间没有太大区别。

我非常喜欢nik_m的答案,所以我写了一些代码从Django shell内部生成模板结构。 我想一遍又一遍地创建这些文件时要保持一定的一致性。 我将代码放在这里,以防其他人发现它有用。

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例如from my_app.schema import MyAppQuery, MyAppMutation
  • 我已经在“查询”与“查询”和“突变”与“突变”之间来回走了,截至目前,石墨烯文档还不一致

暂无
暂无

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

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