这个错误通常出现在使用Apollo和Angular时,没有正确地配置Apollo client。解决方法是确保在应用程序的模块中正确注入Apollo client。以下是一个示例代码:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { ApolloModule, Apollo } from 'apollo-angular';
import { HttpLinkModule, HttpLink } from 'apollo-angular-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
ApolloModule,
HttpLinkModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
constructor(
apollo: Apollo,
httpLink: HttpLink,
private http: HttpClient
) {
apollo.create({
link: httpLink.create({ uri: 'http://localhost:3000/graphql' }),
cache: new InMemoryCache(),
defaultOptions: {
watchQuery: {
fetchPolicy: 'network-only',
errorPolicy: 'all',
},
query: {
fetchPolicy: 'network-only',
errorPolicy: 'all',
},
mutate: {
fetchPolicy: 'no-cache',
errorPolicy: 'all'
}
}
});
}
}
在这个示例中,我们在AppModule中注入了Apollo和HttpLink,并创建了一个新的Apollo client,在uri中传入了GraphQL的API地址。此外,我们还设置了默认的选项,包括fetchPolicy和errorPolicy。
确保正确配置了Apollo client后,问题就应该得到解决。