[英]How to avoid circular dependency in angular project with Apollo client and `graphql-codegen` when importing types in client resolver?
My objective is to use generated types when writing client-side resolvers with the Apollo client.我的目标是在使用 Apollo 客户端编写客户端解析器时使用生成的类型。
Everything in the client-side resolvers is working as described by Apollo here .客户端解析器中的所有内容都按照Apollo 此处的描述工作。 However, in the project, we are also using generated types and GQL services as described in this blog .但是,在该项目中,我们还使用了本博客中所述的生成类型和 GQL 服务。 And here comes the issue.问题来了。 If I import a type from the generated types in the client-side resolvers, I get the error:如果我从客户端解析器中生成的类型导入类型,则会收到错误消息:
WARNING in Circular dependency detected:
eddy.module.ts ->
...graphql.module.ts ->
...resolvers.ts ->
...client-specific-recommendation-resolver.ts ->
...get-top-right-corner-state.ts ->
...graphql.ts -> // (generated types)
...eddy.module.ts
So essentially in the get-top-right-corner-state.ts
file, because I import from the generated types like this:所以基本上在get-top-right-corner-state.ts
文件中,因为我从生成的类型导入,如下所示:
import { Something } from '../../../../../generated/graphql';
Notes笔记
Our application loads apollo in a module called graphql.module
, used by a feature module called "eddy".我们的应用程序将 apollo 加载到名为graphql.module
的模块中,该模块由名为“eddy”的功能模块使用。 We have configured this with the ngModule
property in the codegen.yml
file.我们已经使用codegen.yml
文件中的ngModule
属性对其进行了配置。
What can I do to prevent this?我能做些什么来防止这种情况? I would very much like to utilize the types generated by graphql-codegen
, also in my client-side resolvers.我非常想在我的客户端解析器中使用由graphql-codegen
生成的类型。
After reading and experimenting with this issue, I found a solution.在阅读并尝试解决此问题后,我找到了解决方案。 The setup is actually pretty simple, but a tiny bit more complicated than just providing the generated services in the root of Angular.设置实际上非常简单,但比仅在 Angular 的根目录中提供生成的服务要复杂一些。
The feature module does the Apollo setup and creates the client-side resolvers.功能模块执行 Apollo 设置并创建客户端解析器。 But instead of providing the generated services here, they are provided in another module, here called lazy-loaded.module.ts.但是这里没有提供生成的服务,而是在另一个模块中提供它们,这里称为lazy-loaded.module.ts。 The codegen.yml now looks like this, and there are no circular dependencies: codegen.yml 现在看起来像这样,并且没有循环依赖:
generates:
<path-to-feature-module>/graphql.ts:
schema: <path-to-client-schema>.ts
documents: ./src/**/*.graphql
config:
ngModule: <path-to-lazy-loaded-module>#LazyLoadedModule
The lazy loaded module is then imported by the feature module providing Apollo, and voila, problem solved 😇延迟加载的模块然后由提供 Apollo 的功能模块导入,瞧,问题解决了 😇
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.