简体   繁体   English

如何使用 GraphQL-JS 定义日期标量?

[英]How to define a Date Scalar using GraphQL-JS?

I am trying to define a custom scalar in GraphQL so I can query & process the Dates in my MongoDB collections.我正在尝试在 GraphQL 中定义一个自定义标量,以便我可以查询和处理我的 MongoDB 集合中的日期。 I am not sure I understand 100% what a scalar is or does, but it seems to be a sort of type that I define myself.我不确定我是否 100% 理解标量是什么或做什么,但它似乎是我自己定义的一种类型。 All the examples & tutorials I found were using Apollo or some other type of notation, but I would like to see a solution using GraphQL-JS我发现的所有示例和教程都使用 Apollo 或其他类型的符号,但我希望看到使用GraphQL-JS的解决方案

So far, I have defined my scalar like so:到目前为止,我已经像这样定义了我的标量:

const Date = new GraphQLScalarType({
  name: "Date",
    serialize: (value) => {
    return value; //is it correct, to just return the value? Do I need to parse it or turn it into a Date first?
  },
  parseValue: () => {
    return "serialise"; //I am just returning this string here, because I do not know what this function is for
  },
  parseLiteral(ast) {
    return null; //I am just returning null here, because I do not know what this function is for
  },
});

I am not sure I understand what each of these functions are supposed to do.我不确定我是否理解这些功能中的每一个应该做什么。 And wouldn't there also have to be a deserialize function?不是也必须有deserialize功能吗?

When I query now against my graphql endpoint I do get back something like:当我现在查询我的 graphql 端点时,我确实得到了类似的信息:

{
  "myDate": "2020-07-15T00:00:00.000Z"
}

I guess that my serialise function is at play here?我猜我的serialise功能在这里起作用了? The Date is certainly correct, but I am not sure if I should do anything else with the data before returning it from serialize ?日期当然是正确的,但我不确定在从serialize返回数据之前是否应该对数据做任何其他事情? Right now I just return whatever I get from my MongoDB database.现在我只返回我从我的 MongoDB 数据库中得到的任何东西。

Urigo, from The Guild, created graphql-scalars that contains definitions for multiple common scalars used in GraphQL来自 The Guild 的graphql-scalars创建了graphql-scalars ,其中包含 GraphQL 中使用的多个常见标量的定义

//is it correct, to just return the value? //是否正确,只返回值? Do I need to parse it or turn it into a Date first?我是否需要先解析它或将其转换为日期?

It would be wise to validate that value is a Date before returning it.在返回它之前验证该value是一个Date是明智的。 And yes, just return the value.是的,只需返回值。

//I am just returning null here, because I do not know what this function is for //我这里只是返回null,因为我不知道这个函数是干什么用的

This is the entry from the abstract-syntax-tree (ast).这是来自抽象语法树 (ast) 的条目。 See Urigo 's code below to see how the ast object is accessed请参阅下面的Urigo代码以了解如何访问ast对象

  • ast.kind
  • ast.value

Additionally, take a look at this SO post that describes the difference between parseValue and parseLiteral另外,看看这个 SO post,它描述了parseValueparseLiteral之间的区别

Take a look at localDate and that may provide you the example you need to answer your question :)看看localDate ,它可能为您提供回答问题所需的示例:)

export const GraphQLLocalDate = /*#__PURE__*/ new GraphQLScalarType({
  name: 'LocalDate',
  description:
    'A local date string (i.e., with no associated timezone) in `YYYY-MM-DD` format, e.g. `2020-01-01`.',

  serialize(value) {
    // value sent to client as string
    return validateLocalDate(value);
  },

  parseValue(value) {
    // value from client as json
    return validateLocalDate(value);
  },

  parseLiteral(ast) {
    // value from client in ast
    if (ast.kind !== Kind.STRING) {
      throw new GraphQLError(
        `Can only validate strings as local dates but got a: ${ast.kind}`,
      );
    }

    return validateLocalDate(ast.value);
  },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM