sainthkh / reasonql

Type-safe and simple GraphQL library for ReasonML developers.
MIT License
96 stars 5 forks source link

Fetched data is not converted into a record. #18

Open hodatorabi opened 4 years ago

hodatorabi commented 4 years ago

I'm running a simple hello world example similar to the one that is used as an example in this repo. Here is the code for it:

let query =
  ReasonQL.gql({|
  query AppQuery {
    hello {
      message
    }
  }
|});

module Request =
  ReasonQL.MakeRequest(
    AppQuery,
    {
      let url = "http://localhost:4000";
      let headers = Js.Obj.empty();
    },
  );

let onClick = () => {
    Js.log("Loading data...");
    Request.send(Js.Dict.empty())
    ->Request.finished((data: AppQuery.queryResult) => {
        Js.log2("fetched", data.hello);
      });
  };

Problem is when I print the data, data is an array with the value of my hello field and data.hello returns undefined. I've also run the hello world example that is included in the project, printing data results in the same output but actually accessing data.hello seems to work as expected. I also checked the generated AppQuery.re and the decoding of the response is like below:

[%%raw {|
var decodeHello = function (res) {
  return [
    res.message,
  ]
}

var decodeQueryResult = function (res) {
  return [
    decodeHello(res.hello),
  ]
}
|}]

Seeing as this is returning an array, it seems reasonable that the data after the request finished is also an array. I just don't get how the record is being correctly made in the hello world example in this project and how to get the expected behavior which is getting my result as records. Since these examples and package haven't been updated in a while maybe there has been some breaking change which I can't seem to figure out.

hodatorabi commented 4 years ago

Update: I now know my log prints an array due to this: https://bucklescript.github.io/docs/en/better-data-structures-printing-debug-mode but still don't know why my data.hello is empty.

Update 2: Apparently that's not the case anymore: https://twitter.com/bobzhang1988/status/1196556310122135552 so this might actually be the reason why my example doesn't work.