初学者在这里。虽然使用执行“ng add apollo-angular”时使用的默认结构使用单个端点工作正常,但使用 APOLLO_NAMED_OPTIONS 添加第二个端点时,我收到两条神秘的错误消息。可能出了什么问题?
这是我的 graphql.module.ts 文件
import {APOLLO_OPTIONS, APOLLO_NAMED_OPTIONS} from 'apollo-angular';
import { InMemoryCache, ApolloLink } from '@apollo/client/core';
import {HttpLink} from 'apollo-angular/http';
const urium = 'https://some.url.here'; //changed for this post
const urips = 'https://some.other.one.here'; //changed for this post
export function createApollo(httpLink: HttpLink) {
return {
link: ApolloLink.from([httpLink.create({ uri: urium })]),
cache: new InMemoryCache()
}
}
export function createProjectspecApollo(httpLink: HttpLink) {
return {
name:'projectspec',
link: ApolloLink.from([httpLink.create({ uri: urips })]),
cache: new InMemoryCache()
}
}
@NgModule({
providers: [
{
provide: APOLLO_OPTIONS,
useFactory: createApollo,
deps: [HttpLink]
},{
provide: APOLLO_NAMED_OPTIONS,
useFactory: createProjectspecApollo,
deps: [HttpLink]
}
]
})
export class GraphQLModule {}
这是GqlApolloInterfaceService,我使用客户端
import { Apollo } from "apollo-angular";
@Injectable({
providedIn: 'root'
})
export class GqlApolloInterfaceService {
constructor(private apollo:Apollo) { }
}
最后,我的 package.json
{
"name": "frontend",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~11.0.0",
"@angular/cdk": "^11.0.1",
"@angular/common": "~11.0.0",
"@angular/compiler": "~11.0.0",
"@angular/core": "~11.0.0",
"@angular/flex-layout": "^11.0.0-beta.33",
"@angular/forms": "~11.0.0",
"@angular/material": "^11.0.1",
"@angular/platform-browser": "~11.0.0",
"@angular/platform-browser-dynamic": "~11.0.0",
"@angular/router": "~11.0.0",
"@apollo/client": "^3.0.0",
"apollo-angular": "^2.1.0",
"apollo-angular-link-http": "^1.11.0",
"apollo-link-context": "^1.0.20",
"graphql": "^15.0.0",
"rxjs": "~6.6.0",
"tslib": "^2.0.0",
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.1100.1",
"@angular/cli": "~11.0.1",
"@angular/compiler-cli": "~11.0.0",
"@types/jasmine": "~3.6.0",
"@types/node": "^12.11.1",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.6.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.1.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.0.3",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "^1.5.0",
"protractor": "~7.0.0",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~4.0.2"
}
}
我得到两个错误:第一个只显示在第一个,只要我在提供者列表中添加第二个 pbject:
错误错误:未捕获(承诺):不变违规:要初始化 Apollo 客户端,您必须在选项对象中指定“缓存”属性。
然后,如果我刷新页面,我会得到一个不同的页面:
ERROR 错误:未捕获(承诺中):错误:NG0200:DI 中检测到 GqlApolloInterfaceService 的循环依赖错误:NG0200:DI 中检测到 GqlApolloInterfaceService 的循环依赖
我刚开始使用 Apollo Client,所以它可能非常简单,但是,我得到的印象是文档非常模糊,并且没有付出太多努力来制作它。可能会发生什么,最重要的是,我怎样才能让它发挥作用?
问候!