angular - 如何将参数从 UI 传递到 graphql 以获取数据
问题描述
我有一个graphQl url,我可以从后端获取数据。但是,目前的问题是我正在提供客户的硬编码 id 来获取数据。我想创建一个输入框,然后允许用户通过输入客户 ID 来获取任何客户记录。当前用于获取客户记录的硬编码代码:
ngOnInit() {
this.apollo
.watchQuery({
query: gql`
{
findCustomer(id: "UF2aLa1L687") {
id
customerType
}
}
`,
})
.valueChanges.subscribe(result => {
this.customer = result.data ;
this.loading = result.loading;
this.error = result.errors;
console.log('Customer Info', this.customer);
console.log('Loading', this.loading);
console.log('Error', this.error);
});
}
如何从 UI 传递值,我需要类似于使用 ngModel 的角度双向绑定的东西。我尝试了这段代码,但我收到了未终止的字符串错误。
searchByCustomerIdGraphQL() {
console.log(this.customerIdValue);
this.isLoadingResults = true;
this.apollo.watchQuery({
query: gql`
{
findCustomer(id : "` + this.customerIdValue + `") {
id
}
}
}`
}).valueChanges.subscribe( result => {
this.customerSearchRecord = result.data;
this.loading = result.loading;
this.error = result.errors;
});
}
有人可以帮我弄清楚如何传递值吗?
解决方案
推荐阅读
- angular - 错误:BrowserModule 已加载。, 改为导入 CommonModule
- php - MsSql 语法quoteString 不要自动添加'N'
- gerrit - Gerrit 代码审查:有没有办法从审查中过滤掉(排除)特定文件?
- android - Android 管理 API 和 Android TV
- codeigniter-4 - Codeigniter redirect()->to() 在 __construct() 中不起作用
- string - PowerShell - 问题:变量中的整个字符串被传递
- javascript - 选中单选按钮时在输入字段中添加必需的属性
- azure - Azure Logic App - 使用变量和使用循环时出现 ActionFailed 错误
- llvm - LLVM IR中的`select`和`phi`有什么区别?
- c# - 以比 C# 中的 Regex 更轻量级的方式验证 JWT 的构建