Graphql 数组对 ReasonReact 的响应

Graphql array response to ReasonReact

试图合理地处理数据。我有这个 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 之类的东西记录所有的剧集……显然这里行不通

组件要点:episode component, home.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

,这段代码可以在 JSX 上运行
```

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>
};
```

我认为这应该可行:

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

response##data##allEpisodes
|> Array.map((episode) =>

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