处理graphql结果时出现类型错误

时间:2018-09-15 19:07:39

标签: ocaml graphql reason bucklescript ppx

我刚刚开始使用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)))

我添加了Js.log(response ## matches);并在控制台中得到了
Js.log

1 个答案:

答案 0 :(得分:1)

我很确定这里缺少某些上下文,并且文件顶部有UPDATE NoOneEverNamesTheirTableInSqlQuestions SET filled = amount, left = 0; 之类的东西。

类型错误表明open Beltresponse##matches中的array,而且数组元素本身位于option s中,这很奇怪。所以我的理论是option生成的代码使用了类似于数组索引的功能,它转换为对graphql-ppx的调用,如果超出范围,则在标准库中会引发异常,但在{{1}中},以及许多其他标准库的替代品,返回Array.get

这是使用Belt的许多问题之一。它生成的代码不是孤立的,并且可以与其余代码怪异地交互,并且调试起来并不容易。