简体   繁体   English

在客户端解析器中导入类型时,如何避免使用 Apollo 客户端和 `graphql-codegen` 的角度项目中的循环依赖?

[英]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.

相关问题 为什么 apollo client:codegen 不是生成类型? - Why apollo client:codegen is not generate types? 在 Angular 应用程序中使用 graphql-codegen 时出现错误“对象类型未知” - Error "Object is of type unknown" when using graphql-codegen in the Angular application 如何使用 apollo client:codegen 从 schema.graphql 中提取打字稿接口 - How to extract a typescript interfaces from a schema.graphql with apollo client:codegen 如何使用graphql为angular2 + apollo-client创建服务 - How to create service for angular2 + apollo-client with graphql 如何使用Apollo客户端库创建带有angular参数的graphql查询 - How to create a graphql query with arguments in angular using apollo client library angular 中的apollo client for graphql 如何处理报错? - How to handle error using apollo client in angular for graphql? 测试Angular Effect + graphql apollo客户端 - Testing Angular Effect + graphql apollo client 使用 graphql 和 apollo 客户端刷新 angular 的令牌 - refresh token for angular using graphql and apollo client Graphql - 使用 Apollo 客户端 Angular 的突变操作不起作用 - Graphql - Mutation operation with Apollo Client Angular not working 使用 GraphQl 的 Apollo 客户端时如何在 watchQuery 中使用加载属性 - How to use the loading property in a watchQuery when using the Apollo client for GraphQl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM