簡體   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