【问题标题】:Graphql array response to ReasonReactGraphql 数组对 ReasonReact 的响应
【发布时间】:2017-11-11 04:42:22
【问题描述】:

试图以合理的方式处理数据。我有这个 graphql 查询返回数据并记录它。问题是如何访问以下组件中的数据。

let component = ReasonReact.statelessComponent("Home");

let make = (_) => {
  ...component,
  render: (_self) =>
    <View>
      <Hello message="Hello from home component" />
      <FetchEpisodes>
        (
          (response) => {
            Js.log(response);
            /* let episodeItems =
               Array.of_list(
                 List.map(
                   (response) =>
                     <Episode
                       key=response##data##allEpisodes##id
                       episode=response##data##allEpisodes##episode
                     />,
                 )
               ); */
            <div> <h1> (ReasonReact.stringToElement("Episodes!")) </h1> </div>
            /* (ReasonReact.arrayToElement(episodeItems)) */
          }
        )
      </FetchEpisodes>
    </View>
};

这是查询响应:

来自 JS,我一直想用一些类似 response.data 的方式记录 allEpisodes...这显然在这里不起作用

组件的要点:episode componenthome.re component

如果我取消注释并运行,它会产生以下错误:

```

FAILED: src/pages/home.mlast
/usr/local/lib/node_modules/bs-platform/bin/bsc.exe -pp "/usr/local/lib/node_modules/bs-platform/bin/refmt3.exe --print binary" -ppx '/usr/local/lib/node_modules/bs-platform/bin/reactjs_jsx_ppx_2.exe'   -w -30-40+6+7+27+32..39+44+45+101 -nostdlib -I '/Users/shingdev/code/REASON/with-reason-apollo-master/node_modules/bs-platform/lib/ocaml' -bs-super-errors -no-alias-deps -color always -c -o src/pages/home.mlast -bs-syntax-only -bs-binary-ast -impl /Users/shingdev/code/REASON/with-reason-apollo-master/src/pages/home.re
File "/Users/shingdev/code/REASON/with-reason-apollo-master/src/pages/home.re", line 53, characters 17-18:
Error: 2806: <UNKNOWN SYNTAX ERROR>

  We've found a bug for you!
  /Users/shingdev/code/REASON/with-reason-apollo-master/src/pages/home.re

  There's been an error running Reason's refmt parser on a file.
  This was the command:

  /usr/local/lib/node_modules/bs-platform/bin/refmt3.exe --print binary '/Users/shingdev/code/REASON/with-reason-apollo-master/src/pages/home.re' > /var/folders/qx/xhwh5zfj7z36_bjh5187svx00000gn/T/ocamlpp530e68

```

我不明白在返回数组时如何处理响应对象。谢谢。

根据@glennsl 的建议进行更新:

```
let make = (_) => {
  ...component,
  render: (_self) =>
    <View>
      <Hello message="Hello from home component" />
      /* <Greeting name="Tony" /> */
      <FetchEpisodes>
        (
          (response) => {
            let episodeItems =
              response##data##allEpisodes
              |> Array.map((episode) => <Episode key=episode##id title=episode##title />);
            <div> <h1> (ReasonReact.stringToElement("Episodes!")) </h1> </div>(
              ReasonReact.arrayToElement(episodeItems)
            )
          }
        )
      </FetchEpisodes>
    </View>
};
```

这会产生以下错误:

我正在考虑它的到来,因为类型没有被传递给 episode.re

```
let component = ReasonReact.statelessComponent("Episode");

let make = (~style=?, ~episode, _children) => {
  ...component,
  render: (_self) => <View ?style> <h1> (ReasonReact.stringToElement(episode)) </h1> </View>
};
```

我应该在某处传递列表(剧集)吗?

更新 2:感谢@glennsl

```

let make = (_) => {
  ...component,
  render: (_self) =>
    <View>
      <Hello message="Hello from home component" />
      /* <Greeting name="Tony" /> */
      <FetchEpisodes>
        (
          (response) => {
            let episodeItems =
              response##data##allEpisodes
              |> Array.map((episode) => <Episode key=episode##id episode=episode##episode />);
            <div>
              <h1> (ReasonReact.stringToElement("Episodes!")) </h1>
              (ReasonReact.arrayToElement(episodeItems))
            </div>
          }
        )
      </FetchEpisodes>
    </View>
};
```

【问题讨论】:

  • 这对我来说并不明显为什么这显然不起作用。您是否收到错误消息,还是无法按预期工作?
  • @glennsl 显然,这意味着有问题,因此我问了一个问题。我的错。我更新了它产生的错误。感谢您的观看。
  • 好的,我认为您即使只是记录响应也有问题。语法错误只是因为List.map 中有一个悬空的,,但是您也缺少第二个参数并且那里也有很多类型错误。让我为您准备一个建议作为答案。

标签: ocaml graphql reason bucklescript reason-react


【解决方案1】:

我认为这应该可行:

type episode = {. "id": string, "title": string, "episode": string};
type data = {. "allEpisodes": array(episode)};

...

(response) => {
  let episodeItems =
    response##data##allEpisodes
    |> Array.map((episode) =>
       <Episode key=episode##id
                episode=episode##episode />);

  <div>
    <h1> (ReasonReact.stringToElement("Episodes!")) </h1>
    (ReasonReact.arrayToElement(episodeItems))
  </div>
}

如果没有,请告诉我,或者如果其中有任何让您感到困惑,我会很乐意解释。

编辑:您还输入了data##allEpisodes 作为list(episode),而实际上它是array(episode)。更新了上面的代码块。 listarray不一样,前者是链表类型,后者相当于JavaScript数组。

编辑 2:修复 JSX

【讨论】:

  • 所以如果这是 episode.relet component = ReasonReact.statelessComponent("Episode"); let make = (~style=?, ~episode, _children) =&gt; { ...component, render: (_self) =&gt; &lt;View ?style&gt; &lt;h1&gt; (ReasonReact.stringToElement(episode)) &lt;/h1&gt; &lt;/View&gt; }; 我必须通过标题类型来制作,对吗?
  • 标题类型?如果您想使用该 Episode 组件打印标题,则必须将 title 属性的值传递给它:&lt;Episode key=episode##id episode=episode##title /&gt;
  • 我仍在产生错误。这是演示回购。 github.com/idkjs/reason-arrays-demo。另外,这个概念是什么命名的,所以我可以在文档中查找它?
  • 可以直接回ReasonReact.arrayToElement(episodeItems),可以。如果您有更多后续问题,最好在 Discord 上给我发私信。这个评论线程有点长:)
  • 您还应该将 SO 问题保留在单个特定问题上,并且在遇到新问题时不要更新它们。 SO 打算成为一个知识库,而不是一般的调试/问题修复服务。 Discord 是提供此类帮助的更好论坛。
【解决方案2】:
response##data##allEpisodes
|> Array.map((episode) =>

这不起作用,因为response##data##allEpisodes 返回未定义。我相信这是因为在 [reason-apollo] 中:https://github.com/Gregoirevda/reason-apollo/blob/master/src/ReasonApollo.re#L29 data 被输入为字符串。

【讨论】:

  • 抱歉添加答案。我没有足够的积分来发表评论。
  • 遇到同样的错误。奇怪,因为以前,在修复 jsx 之前,它会返回并登录到控制台。现在它只是不等待它。如何合理地处理异步?
猜你喜欢
  • 2021-02-17
  • 2018-10-25
  • 2016-04-09
  • 2021-04-10
  • 2020-04-08
  • 2020-06-10
  • 2016-12-18
  • 2016-07-27
  • 1970-01-01
相关资源
最近更新 更多