Graphql数组对ReasonReact的响应

时间:2017-11-10 13:16:17

标签: ocaml graphql reason bucklescript reason-react

试图理解合理使用数据。我有这个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>
};

这是查询响应: enter image description here

来自JS我一直想要记录allEpisodes和一些像response.data ...这在这里不起作用,显然

Gists to components: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>
};
```

这会产生以下错误:

enter image description here

我正在考虑它的到来,因为类型没有被传递给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>
};
```

2 个答案:

答案 0 :(得分: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)list。更新了上面的代码块。 array SELECT s.id FROM set s JOIN set_rel_item sri ON s.id = sri.set_id JOIN item i ON sri.item_id = i.id WHERE i.name in ('item1','item2','item3') GROUP BY s.id HAVING COUNT(DISTINCT i.name) >= 1 ORDER BY COUNT(DISTINCT i.name) DESC 不同,前者是链表类型,后者相当于JavaScript数组。

编辑2:修复了JSX

答案 1 :(得分:0)

sum()

这不会起作用,因为response##data##allEpisodes |> Array.map((episode) => 返回undefined。我相信它是因为在[reason-apollo]中:https://github.com/Gregoirevda/reason-apollo/blob/master/src/ReasonApollo.re#L29 response##data##allEpisodes被输入为字符串。