我刚刚开始使用reasonML和graphql,并构建了一个简单的react组件,该组件可以从世界杯API中检索数据。我的代码如下:
[@bs.module] external gql: ReasonApolloTypes.gql = "graphql-tag";
module GetMatches = [%graphql
{|
query getMatches($id: ID!){
matches(id: $id) {
date
homeTeam {name}
awayTeam {name}
homeScore
awayScore
goals {
scorer {
name
}
time
}
}
}
|}
];
module GetMatchesQuery = ReasonApollo.CreateQuery(GetMatches);
let component = ReasonReact.statelessComponent("Matches");
let make = _children => {
...component,
render: _self => {
let matchParam = GetMatches.make(~id="300331511", ());
<GetMatchesQuery variables=matchParam##variables>
...{
({result}) =>
switch (result) {
| Loading => <div> {ReasonReact.string("Loading")} </div>
| Error(error) =>
<div> {ReasonReact.string(error##message)} </div>
| Data(response) => <Matches matches=response##matches />
}
}
</GetMatchesQuery>;
},
};
匹配组件
let component = ReasonReact.statelessComponent("Matches");
let make = (~matches, _children) => {
...component,
render: _self =>
<div>
{
matches
|> Js.Array.map(match => <Match match key=match##id />)
|> ReasonReact.array
}
</div>,
};
但是我遇到了这个错误:
This has type:
option(Js.Array.t(option({. "awayScore": option(int),
"awayTeam": option({. "name": option(string)}),
"date": option(string),
"goals": option(Js.Array.t(option({. "scorer":
option(
{. "name":
option(
string)}),
"time":
option(
string)}))),
"homeScore": option(int),
"homeTeam": option({. "name": option(string)})})))
But somewhere wanted:
Js.Array.t(Js.t(({.. id: string} as 'a))) (defined as array(Js.t('a)))
答案 0 :(得分:1)
我很确定这里缺少某些上下文,并且文件顶部有UPDATE NoOneEverNamesTheirTableInSqlQuestions
SET filled = amount, left = 0;
之类的东西。
类型错误表明open Belt
是response##matches
中的array
,而且数组元素本身位于option
s中,这很奇怪。所以我的理论是option
生成的代码使用了类似于数组索引的功能,它转换为对graphql-ppx
的调用,如果超出范围,则在标准库中会引发异常,但在{{1}中},以及许多其他标准库的替代品,返回Array.get
。
这是使用Belt
的许多问题之一。它生成的代码不是孤立的,并且可以与其余代码怪异地交互,并且调试起来并不容易。