graphql-python / graphql-python/graphene
Circular import solution
- Vorherrschende Sprache
- Python
- Sterne
- 8.2k
- Forks
- 818
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
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:
```python
def type(self, schema):
return get_type(self._type, schema)
```
Then reducer invocation [here](https://github.com/graphql-python/graphene/blob/e24ac547d670d90931cc2392d2330787747c041f/graphene/types/schema.py#L299) :
```python
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:
```python
def get_type(_type, schema=None):
if isinstance(_type, string_types):
return import_string(_type, schema=schema)
```
```python
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](https://github.com/graphql-python/graphene/issues/522#issuecomment-626075061) without problems.
```python
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
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Rechercherichtung
Beginne damit, graphene/types/schema.py im Bereich von construct_fields_for_type sowie die get_type/import_string-Methoden in utils zu prüfen, und reproduziere anschließend das Beispiel für einen zirkulären Import aus der verlinkten Diskussion. Als abgeschlossen gilt die Aufgabe, wenn festgestellt wurde, ob die Übergabe des Schemas das Beispiel behebt, ohne die bestehende Typauflösung zu beeinträchtigen, und die daraus resultierende Entscheidung dokumentiert wurde.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- api
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100