【发布时间】:2018-10-19 02:19:00
【问题描述】:
我是 GraphQL 的新手。
我想做的是在文本输入上的每个 onBlur 事件上调用 GraphQL 服务器。
问题是:
- 只成功调用一次。
- 在第二次调用时,我收到此错误:
ERROR Error: Apollo has been already created.
这可能很明显,但我找不到解决方案。
有没有办法在每个连续发生的onBlur 事件上成功地进行新的 graphql 调用?
onBlurZIP(zip) {
const zipQuery = gql`
query {
getAddressByPostalCode(postalCode: "${zip}") {
country
city
street
streettype
state
stateInitials
quarter
}
}
`;
const httpLink = createHttpLink({
uri: environment.endpoint
});
const link = setContext((_, { headers }) => {
return {
headers: {
// headers object...
}
}
});
this._apollo.create({
link: link.concat(httpLink),
cache: new InMemoryCache()
});
this.querySubscription = this._apollo.watchQuery<any>({
query: zipQuery,
variables: { zip },
fetchPolicy: 'network-only'
})
.valueChanges
.subscribe(
res => {
// do stuff...
},
err => console.log(err)
);
}
HTML:
<input formControlName="zip" (blur)="onBlurZIP($event.target.value)" type="text" class="form-control" name="zip" id="zip" required>
【问题讨论】:
标签: angular apollo graphql-js apollo-client