【问题标题】:Angular - Apollo: Client has not been defined yetAngular - Apollo:尚未定义客户端
【发布时间】:2018-08-21 19:08:35
【问题描述】:

我正在为 graphql 使用 apollo 客户端。我在 AppApolloModule 中设置了我在 AppModule 中导入的客户端。我正在一项服务中进行查询,该服务也直接在 AppModule 中导入。虽然服务在 AppApolloModule 运行之前运行,因此在进行查询时未初始化 apollo,我收到此错误

Error: Client has not been defined yet

AppApolloModule

imports ....

export class AppApolloModule {

    constructor(
        apollo: Apollo,
        httpLink: HttpLink,
        private userService: UserService
    ) {
        console.log("apollo module")
        apollo.create({
            link: httpLink.create({ uri: `${environment.apiBase}/graphql?${this.myService.token}`}),
            cache: new InMemoryCache()
        })
    }

}

应用模块

import { AppApolloModule } from './app.apollo.module';
import { MyService } from './services/my.service';

export class AppModule {
      constructor() {
        console.log("app module")
      }
}

我没有得到两个控制台应用程序模块和 apollo 模块,因为服务首先运行,它没有找到任何已初始化的 apollo 应用程序,因此破坏了代码。

我怎样才能让 apollo 在服务或任何服务之前以高效和标准的方式运行?

【问题讨论】:

  • 您是否尝试过使用应用生命周期 OnInit ?尝试实现 OnInit 接口并将您的代码带入 ngOnInit 方法
  • 也许看看APP_INITIALIZERtoken
  • @e.m.b 服务没有 ngOnInit
  • 我的意思是将模块构造函数中的代码移动到模块中的 ngOnInit 而不是服务中
  • @e.m.b 这也无济于事,因为构造函数首先运行,将其放在 ngOnIt 上会使它们在更多时间后加载

标签: javascript angular graphql apollo graphql-js


【解决方案1】:

对我有用的是删除 .angular 文件夹并再次为应用程序提供服务。

【讨论】:

    【解决方案2】:

    我有同样的问题,Apollo 的文档帮助了我。转到“https://www.apollographql.com/docs/angular/basics/setup/”或复制以下内容:

    import { HttpClientModule } from "@angular/common/http";
    import { ApolloModule, APOLLO_OPTIONS } from "apollo-angular";
    import { HttpLinkModule, HttpLink } from "apollo-angular-link-http";
    import { InMemoryCache } from "apollo-cache-inmemory";
    
    @NgModule({
      imports: [
        BrowserModule,
        HttpClientModule,
        ApolloModule,
        HttpLinkModule
      ],
      providers: [{
        provide: APOLLO_OPTIONS,
        useFactory: (httpLink: HttpLink) => {
          return {
            cache: new InMemoryCache(),
            link: httpLink.create({
              uri: "https://o5x5jzoo7z.sse.codesandbox.io/graphql"
            })
          }
        },
        deps: [HttpLink]
      }],
    })
    export class AppModule {}
    

    【讨论】:

      【解决方案3】:

      @wendellmva 的回答对我不起作用。 做了工作的是这个 repo 中建议的解决方案:

      https://github.com/patricknazar/angular-lazy-loading-apollo-client

      这基本上是将 Apollo 初始化放在一个单独的共享模块中,并使用 forRoot() 将其包含在您的主应用模块中。

      【讨论】:

      • 此链接不再可用。
      • 能否请您更新存储库的链接?
      • @jowey 似乎所有者删除了它,不幸的是我当时没有考虑克隆它。我们的设置现在有点复杂,所以我不能在这里发布。也许官方文档已经更新,正如 raffaval 在下面写的那样?
      【解决方案4】:

      这将很好地解决问题:

      import {NgModule} from '@angular/core';
      import {HttpClientModule} from '@angular/common/http';
      import {ApolloModule, APOLLO_OPTIONS} from 'apollo-angular';
      import {HttpLink, HttpLinkModule} from 'apollo-angular-link-http';
      import {InMemoryCache} from 'apollo-cache-inmemory';
      
      export function createApollo(httpLink: HttpLink) {
        return {
          link: httpLink.create({uri: 'https://api.example.com/graphql'}),
          cache: new InMemoryCache(),
        };
      }
      
      @NgModule({
        imports: [HttpClientModule, ApolloModule, HttpLinkModule],
        providers: [
          {
            provide: APOLLO_OPTIONS,
            useFactory: createApollo,
            deps: [HttpLink],
          },
        ],
      })
      class AppModule {}
      

      【讨论】:

      • 耶!像这样好多了。太糟糕了,阿波罗文档没有把它作为第一个例子。
      • 你只是在 AppModule 中移动了 Apollo 的实例化吗?
      • 升级到 Apollo v3 后 - 这种方法再次给我一个问题“尚未定义客户端”
      • @DauleDK 你是怎么解决的?
      • 我们将堆栈切换到 Svelte/Sapper - 我们从未设法解决我们团队中的问题。
      猜你喜欢
      • 2022-12-29
      • 2020-08-31
      • 2019-06-30
      • 2019-06-10
      • 2019-06-17
      • 2020-05-03
      • 2020-02-27
      • 2021-04-16
      相关资源
      最近更新 更多