Circular import solution
まだ誰も着手していません。
評価
調査の方向性
まず graphene/types/schema.py の construct_fields_for_type 周辺と、utils の get_type/import_string メソッドを確認し、次にリンクされたディスカッションの循環インポートの例を再現します。schema を渡すことで既存の型解決を壊さずにその例を解決できるかどうかを判断し、その結果の決定を文書化できれば完了です。
索引モデルが issue の本文から書いたものです。
説明
I would like to contribute a solution to the circular import problem. As far as I can see, currently this problem has some workaround through the Dynamic type:
https://github.com/graphql-python/graphene/issues/522#issuecomment-626075061
From my point of view strategy described in this link is pretty much elegant, since it uses type argument from Scheme constructor, which has relevant description:
types (List[GraphQLType], optional): List of any types to include in schema that
may not be introspected through root types.
But instead of creating new types and making syntax even more complicated and non-readable, I would like to propose a solution with schema pushing down to the import_string method from utils.
Apparently, it was not difficult task:
I made fast and simple changes in interfaces, in order to do this:
Changed type method interface of Field class:
def type(self, schema):
return get_type(self._type, schema)
Then reducer invocation here :
def construct_fields_for_type(self, map, type, is_input_type=False):
fields = OrderedDict()
for name, field in type._meta.fields.items():
if isinstance(field, Dynamic):
field = get_field_as(field.get_type(self.schema), _as=Field)
if not field:
continue
map = self.reducer(map, field.type(self.schema))
field_type = self.get_field_type(map, field.type(self.schema))
Small changes for get_type method:
def get_type(_type, schema=None):
if isinstance(_type, string_types):
return import_string(_type, schema=schema)
def import_string(type_name, dotted_attributes=None, schema=None):
if schema:
try:
scheme_type = next(type_ for type_ in schema.types if type_._meta.name == type_name)
return scheme_type
except StopIteration:
pass
....
At the end of a day I was able to run code from link without problems.
from graphene import ObjectType, List
class FooBarBaz(ObjectType):
foo_bars = List('FooBar', required= True)
It is a small fix, I made them with respect to my first impression of graphene structure.
Please, give me feedback, if this solution is okay for you, I will make a pull request.
If not, please, describe why and how can I then avoid circular import
- 主要言語
- Python
- スター
- 8.2k
- フォーク
- 818
- PR マージ指標
- 30日以内にマージされた PR はありません
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
graphql-python/graphene のほかの issue
-
Tutorial mistakes オープン🐛 bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
graphql-python/graphene#1389 · コメント 5 件 · リアクション 2 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 68/100
graphql-python/graphene#1606 ·
-
Python 3.14 support オープン✨ enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 38/100
graphql-python/graphene#1601 · コメント 2 件 ·
-
✨ enhancement
難易度 4/5 3〜5日 初心者へのやさしさ 42/100
graphql-python/graphene#1600 ·
-
🐛 bug
難易度 2/5 1〜3時間 初心者へのやさしさ 55/100
graphql-python/graphene#1593 ·
graphql-python/graphene の issue をすべて見る
似ている issue
-
agent-ready documentation needs-triage
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
-
documentation
難易度 1/5 1時間未満 初心者へのやさしさ 91/100
-
workflow-status page template still says reusable workflows are "triggered only by workflow_call:" オープン
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
-
instance instance add
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
searxng/searx-instances#939 · コメント 1 件 ·
-
area-deployment area-integrations triage:bot-seen
難易度 2/5 半日 初心者へのやさしさ 86/100