16
32%
BSD-2-Clause
neglectedBuckleScript interface to create GraphQL Custom Scalar types

bs-graphql-scalar

version downloads license

BuckleScript utilities for creating new GraphQL scalar types.

Getting Started

To create a new scalar using the graphql JavaScript libary, you need three functions:

  • serialize - Used when serializing results to send back to the client.
  • parseValue - Used to parse input that was provided as variables by the client.
  • parseLiteral - Used to parse input that was provided inline in the query.

Example

  • RE
  • ML
let serializeDateTime = (value) =>
  value |> Js.Date.getTime |> Js.Float.toString |> Js.Nullable.return;

let parseDateTimeValue = (str) =>
  str |> Js.Date.fromString |> Js.Nullable.return;

let parseDateTimeLiteral = (ast) => {
  open Language;
  let kind = getAstKind(ast);
  if (kind === intKind) {
    ast |> getAstValue |> TextUtils.parseInt_(10) |> Js.Nullable.return
  } else {
    Js.Nullable.null
  }
};

let resolvers = {
  "DateTime":
    makeScalar({
      "name": "DateTime",
      "description": "DateTime custom scalar Type",
      "serialize": serializeDateTime,
      "parseValue": parseDateTimeValue,
      "parseLiteral": parseDateTimeLiteral
    }),
};
let serializeDateTime value =
  ((value |> Js.Date.getTime) |> Js.Float.toString) |> Js.Nullable.return
let parseDateTimeValue str =
  (str |> Js.Date.fromString) |> Js.Nullable.return
let parseDateTimeLiteral ast =
  let open Language in
    let kind = getAstKind ast in
    if kind == intKind
    then
      ((ast |> getAstValue) |> (TextUtils.parseInt_ 10)) |>
        Js.Nullable.return
    else Js.Nullable.null
let resolvers =
  [%bs.obj
    {
      DateTime =
        (makeScalar
           ([%bs.obj
              {
                name = "DateTime";
                description = "DateTime custom scalar Type";
                serialize = serializeDateTime;
                parseValue = parseDateTimeValue;
                parseLiteral = parseDateTimeLiteral
              }]))
    }]

License

BSD 2-Clause