Manage Circular Imports Hell in GraphQL-Modules
Tags: graphql, graphqlmodules, di
Published: 2021-07-29 09:54:49

> This article was published on Monday, March 18, 2019 by [Arda Tanrikulu](https://twitter.com/ardatanrikulu) @ [The Guild Blog](https://the-guild.dev/)

Designing and building modular GraphQL API may not look straight-forward the first time you start.
It is hard to keep a perfect modularity with standalone and encapsulated modules.It is really easy to appeal to the circular imports, but that's exactly what you shouldn't do in any
case. You might say while reading this; ***I DON'T HAVE ANY WAY OF NOT CREATING CIRCULAR IMPORTS***!In a previous versions of GraphQL-Modules, we used to allow users to have circular imports in their
GraphQL-Modules applications. However, it created a lot of extra logic which slows down the initial
schema generation speed, because we always need to check if there is circular imports between
modules.Then, if GraphQL-Modules found some, it would **merge** all the members of this **circular import**
into a one **LARGE MODULE** which was **breaking almost every principle of encapsulation and
modularity** we mentioned in previous blog-posts of our GraphQL-Modules series.Finally, we decided to **remove this** support; then **force** people to have **strict modularity**
in their projects./blog/modular-encapsulation-graphql-modulesForcing people out of a way of developing is always hard, and we've got
[questions from you](https://github.com/Urigo/graphql-modules/issues/317) about how to solve some
specific issues — so in this blog post and new doc section we will help you understand why this was
a bad practice and how to migrate from it with different use cases.## The ProblemLet's assume we have 3 different entities in our database;*   User
*   Post
*   CommentThen, if we create three different modules for these three entities;```graphql
type User {
  id: ID
  name: String
  username: String
  email: String
  ## Some other fields
  posts: [Post]
  comments: [Comment]
}
``````graphql
type Post {
  id: ID
  title: String
  content: String
  user: User
  comments: [Comment]
}
``````graphql
type Comment {
  id: ID
  content: String
  user: User
  post: Post
}
```As you can see above, every module imports other modules; and this creates a circular dependency.You might ask if this is the only way to implement modules for these entities; because it looks like
there is no point to have different modules for those schemas. Having circular dependency is the
same situtation with having a single large module.<Video src="/medium/ae9bf1b1b35e64c8839b3508064f3115.webm" />## How to SolveLet's see what we have in terms of relationship; - `User` doesn't depend on `Post` and `Comment` -
`Post` doesn't depend on `Comment`.*   `Comment` depends on `User` and `Post`, because it has `userId` and `postId` fields - `Post` also
    depends on `User` because it has `userId` fieldSo let's create modules in that way,```graphql
type User {
  id: ID
  name: String
  username: String
  email: String
  ## Some other fields
}
``````graphql
type Post {
  id: ID
  title: String
  content: String
  user: User
}
extend type User {
  posts: [Post]
}
``````graphql
type Comment {
  id: ID
  content: String
  user: User
  post: Post
}
extend type Post {
  comments: [Comment]
}
extend type User {
  comments: [Comment]
}
```> Using this approach, you will have standalone modules; otherwise will create a big module which
> contains all of them like we used to handle circular deps in this way (**merging all circular
> imports**).Also `extend` says that it needs a main definition from imported modules which makes the connection
more readable in terms of entity relations.***## All Posts about GraphQL Modules*   [GraphQL Modules — Feature based GraphQL Modules at scale](/blog/graphql-modules)
*   [Why is True Modular Encapsulation So Important in Large-Scale GraphQL Projects?](/blog/modular-encapsulation-graphql-modules)
*   [Why did we implement our own Dependency Injection library for GraphQL-Modules?](/blog/dependency-injection-library-in-graphql-modules)
*   [Scoped Providers in GraphQL-Modules Dependency Injection](/blog/graphql-modules-scoped-providers)
*   [Writing a GraphQL TypeScript project w/ GraphQL-Modules and GraphQL-Code-Generator](/blog/graphql-typescript-modules-codegen)
*   [Authentication and Authorization in GraphQL (and how GraphQL-Modules can help)](/blog/graphql-modules-auth)
*   [Authentication with AccountsJS & GraphQL Modules](/blog/accountsjs-graphql-modules)
*   [Manage Circular Imports Hell with GraphQL-Modules](/blog/manage-circular-imports-hell-in-graphql-modules)