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.
Why is True Modular Encapsulation So Important in Large-Scale GraphQL Projects?
How GraphQL Modules helps you encapsulate the different parts of your code, so it will be easier to manage when your…
Forcing people out of a way of developing is always hard and we’ve got questions from you 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.
Let’s assume we have 3 different entities in our database;
Then, if we create three different modules for these three entities;
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.
How To Solve
Let’s see what we have in terms of relationship;
User doesn’t depend on
Post doesn’t depend on
Comment depends on
Post, because it has
Post also depends on
User because it has
So let’s create modules in that way,
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).
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
- Why is True Modular Encapsulation So Important in Large-Scale GraphQL Projects?
- Why did we implement our own Dependency Injection library for GraphQL-Modules?
- Scoped Providers in GraphQL-Modules Dependency Injection
- Writing a GraphQL TypeScript project w/ GraphQL-Modules and GraphQL-Code-Generator
- Authentication and Authorization in GraphQL (and how GraphQL-Modules can help)
- Authentication with AccountsJS & GraphQL Modules
- Manage Circular Imports Hell with GraphQL-Modules