question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Circular dependencies

See original GitHub issue

Python is not able to resolve circular dependencies for schemas with recursive types.

e.g

  # category_type.py
  from .post_type import Post
  class Category(graphene.ObjectType):
    Post = graphene.List(Post)

  # post_type.py
  from .category_type import Category
  class Post(graphene.ObjectType):
    category = graphene.Field(Category)

At runtime, the above fails as the types haven’t yet loaded before being used in field definitions. A better option would be to wrap the field definitions in a lambda to be called at resolve time and after the types have been declared.

e.g

  # category_type.py
-  from .post_type import Post
  class Category(graphene.ObjectType):
-   Post = graphene.List(Post)
+   Post = lambda: graphene.List(Post)

+ from .post_type import Post

  # post_type.py
- from .category_type import Category
  class Post(graphene.ObjectType):
-   category = graphene.Field(Category)
+   category = lambda: graphene.Field(Category)

+ from .category_type import Category

cc @Globegitter

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:19 (4 by maintainers)

github_iconTop GitHub Comments

30reactions
sanfilippopablocommented, Feb 18, 2018

Looking at some unrelated docs here it seems like you can do this:

graphene.List(lambda: MyCircularType)
graphene.Field(lambda: MyCircularType)

I just tested it and it works.

11reactions
syrusakbarycommented, Feb 5, 2016

Hi @imranolas,

You can achieve this by using String references (ala Django):

# category_type.py
@schema.register
class Category(graphene.ObjectType):
    Post = graphene.List('Post')

# post_type.py
@schema.register
class Post(graphene.ObjectType):
    category = graphene.Field('Category')
Read more comments on GitHub >

github_iconTop Results From Across the Web

Circular dependency - Wikipedia
In software engineering, a circular dependency is a relation between two or more modules which either directly or indirectly depend on each other...
Read more >
What is a circular dependency and how can I solve it?
A circular dependency is where Project A depends on something in Project B and project B depends on something in Project A. This...
Read more >
Circular Dependencies in Spring - Baeldung
But with a circular dependency, Spring cannot decide which of the beans should be created first since they depend on one another. In...
Read more >
What is a circular dependency? - CoConstruct
This is the more common and straightforward type of circular dependency. A circular dependency occurs when a task is dependent on itself.
Read more >
Exploring how to solve circular dependencies | Pulumi Blog
Therefore, we can't express a circular dependency in this way. The simplest workaround for this error is to get a reference to our...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found