我有一个来自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)
如何解决此类错误?
谢谢!
答案 0 :(得分:2)
您似乎将response##listPeople
定义为一个选项,因此您无法直接在其上调用response##listPeople##items
。在阅读response##listPeople
之前,您应该对items
进行模式匹配。
答案 1 :(得分: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);
那就是它。