0

I have got an graphql query coming back from aws's appsync service. This is the json that comes back from the query.

https://github.com/idkjs/reason-apollo-question/blob/600584c454ffb2efd08b8e42e3adca0eb151ba60/scratch/persons.json#L1-L27

    {
  "data": {
    "listPeople": {
      "items": [
        {
          "age": 23,
          "id": "123",
          "name": "BobTest",
          "__typename": "Person"
        },
        {
          "age": 24,
          "id": "124",
          "name": "JoeTest",
          "__typename": "Person"
        },
        {
          "age": 25,
          "id": "125",
          "name": "JimTest",
          "__typename": "Person"
        }
      ],
      "__typename": "PersonConnection"
    }
  }
}

This is what it looks like logged to the console. enter image description here

This is the query response in apollo-dev-tools: enter image description here

Error accessing items array.

Trying to log the items values to console by running https://github.com/idkjs/reason-apollo-question/blob/d38e7fbc33f02ab718c7227958d88f02adba1696/src/Persons.re#L66-L70

| Data(response) =>
               Js.log(response##listPeople);
               let listPeople = response##listPeople;
               let items = response##listPeople##items;
Js.log(items);

produces this error:

      We've found a bug for you!
  /Users/prisc_000/code/REASON/APOLLO/reason-apollo-question/src/Persons.re 69:32-51

  67 ┆ Js.log(response##listPeople);
  68 ┆ let listPeople = response##listPeople;
  69 ┆ let items = response##listPeople##items;
  70 ┆ Js.log(items);
  71 ┆

  This has type:
    option({. "items": option(Js.Array.t(option(
      {. "age": int, "id": string, "name":  string})))
      })

   But somewhere wanted:
    Js.t('a)

    ninja: build stopped: subcommand failed.
    >>>> Finish compiling(exit: 1)

How do I resolve this type error?

Thank you!

4

2 回答 2

2

您似乎将其定义response##listPeople为一个选项,因此您不能response##listPeople##items直接调用它。response##listPeople您应该在读取之前进行模式匹配items

于 2018-05-29T13:36:18.883 回答
1

解决方案:

https://github.com/idkjs/reason-apollo-question/blob/2924b1eb928cf0e4de57d5659c5da1bed4dd981c/src/Persons.re#L31-L33

基本上,我还没有摸索 Some/None 变量。

当您看到编译器告诉您它正在寻找上面错误中指出的选项时,您已经通过将值包装在Some.

所以这:

let name = item##name;
let id = item##id;
let age = item##age;

不得不改成这样:

let name = Some(item##name);
let id = Some(item##id);
let age = Some(item##age);

而已。

于 2018-05-29T13:35:39.483 回答