0

Just trying to generally understand what is going on here. Does this make sense to explain ReasonApolloTypes.gql as an example of using Externals.

This is the bs.module code

[@bs.module] external gql : ReasonApolloTypes.gql = "graphql-tag";

bs.module tells buckelscript that we want to use a FFI.

external tells bs the name of the FII we want to use and we set its value to ReasonApolloTypes.gql which is a globally available Reason module that we installed when we added reason-apollo in bsconfig's bs-dependencies array, and to package.json. If you open node_modules/reason-apollo/src you will see defined Reason modules that are globally available like any other.

ReasonApolloTypes.re is listed there and contains a defined type named gql. So ReasonApolloType.gql is the named module we are accessing with external gql. In ReasonApolloType.gql there is a defined type, type gql = [@bs] (string => queryString);. This tell bucklescript to assign a type of string to the gql type and the assign the value to querystring, so type querystring is of type string. Then set ReasonApolloTypes.gql to use the "graphql-tag" node library to resolve ReasonApolloTypes.gql.

Am I missing an concepts here? Is this expressed correctly? The Bucklescript/Reason docs are above my head for Externals at this point. Thanks.

4

1 回答 1

3

这个问题在 SO 上确实不太合适(请参阅此处的主题内容),也许应该在 Discord 上提出。但这就是它的意思:

  • external告诉编译器我们正在定义“外部”的东西,即。我们要使用 FFI。

  • [@bs.module]告诉 BuckleScript 我们引用的是一个模块,并且在使用它时需要发出require调用。

  • gql是我们将用于在原因方面引用它的名称。

  • :意味着我们正在指定外部的类型。ReasonApolloTypes.gqlReasonApolloTypes模块中定义的类型。

  • = "graphql-tag"告诉编译器我们引用的内容是graphql-tag在 JavaScript 端命名的。

此外,type gql = [@bs] (string => queryString);指定它是一个 from stringto的函数queryString,并且[@bs]意味着它在调用这个函数时应该使用 uncurried 调用约定。

因此,在 ML 方面,gql是一个接受 astring并返回 a的函数queryString。调用时:

let qs = [@bs] gql("some string"); /* [@bs] because it's uncurried */

BuckleScript 将生成如下内容:

var qs = require("graphql-tag")("some string");

PS:一般来说,我建议你从一些更简单的东西开始尝试理解 FFI 系统。试图一次理解所有事情会导致非常陡峭的学习曲线,并大大增加小误解的成本,因为它们往往会累积。

于 2017-11-12T12:38:51.840 回答