我有一个从 aws 的 appsync 服务返回的 graphql 查询。这是从查询返回的 json。
{
"data": {
"listPeople": {
"items": [
{
"age": 23,
"id": "123",
"name": "BobTest",
"__typename": "Person"
},
{
"age": 24,
"id": "124",
"name": "JoeTest",
"__typename": "Person"
},
{
"age": 25,
"id": "125",
"name": "JimTest",
"__typename": "Person"
}
],
"__typename": "PersonConnection"
}
}
}
items
数组时出错。尝试items
通过运行https://github.com/idkjs/reason-apollo-question/blob/d38e7fbc33f02ab718c7227958d88f02adba1696/src/Persons.re#L66-L70将值记录到控制台
| Data(response) =>
Js.log(response##listPeople);
let listPeople = response##listPeople;
let items = response##listPeople##items;
Js.log(items);
产生这个错误:
We've found a bug for you!
/Users/prisc_000/code/REASON/APOLLO/reason-apollo-question/src/Persons.re 69:32-51
67 ┆ Js.log(response##listPeople);
68 ┆ let listPeople = response##listPeople;
69 ┆ let items = response##listPeople##items;
70 ┆ Js.log(items);
71 ┆
This has type:
option({. "items": option(Js.Array.t(option(
{. "age": int, "id": string, "name": string})))
})
But somewhere wanted:
Js.t('a)
ninja: build stopped: subcommand failed.
>>>> Finish compiling(exit: 1)
如何解决此类型错误?
谢谢!
解决方案:
基本上,我还没有了解 Some/None 变量。
当您看到编译器告诉您它正在寻找上述错误中指出的选项时,您已经通过将值包装在Some
.
所以这:
let name = item##name;
let id = item##id;
let age = item##age;
不得不改成这样:
let name = Some(item##name);
let id = Some(item##id);
let age = Some(item##age);
就是这样。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句