【问题标题】:Type error when processing graphql result处理graphql结果时出现类型错误
【发布时间】:2018-09-17 08:00:40
【问题描述】:

我刚开始使用 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);并在控制台中得到了这个

【问题讨论】:

  • 查询中没有id?

标签: ocaml graphql reason bucklescript ppx


【解决方案1】:

我很确定这里缺少一些上下文,并且您的文件顶部有类似 open Belt 的内容。

类型错误说response##matchesarray中的option,而且数组元素本身在options中,这非常奇怪。所以我的理论是graphql-ppx 生成的代码使用数组索引之类的东西,它转换为对Array.get 的调用,如果超出范围,标准库中会抛出异常,但在Belt 和许多其他标准中库替换,返回option

这是使用ppxs 的众多问题之一。它生成的代码不是孤立的,并且可以与您的其余代码进行奇怪的交互,并且不容易调试。

【讨论】:

  • 我没有在任何地方使用 Belt,我在问题中添加了 Js.log 值以显示 graphql 查询的结果。
  • 查询的结果无关紧要(至少此时不重要)。这是一个类型错误,而不是运行时错误。您是否打开此文件中的任何其他模块?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-30
  • 2017-06-23
  • 2020-02-26
  • 2020-10-11
相关资源
最近更新 更多