-1

I am trying to define a custom scalar in GraphQL so I can query & process the Dates in my MongoDB collections. 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. 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

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?

When I query now against my graphql endpoint I do get back something like:

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

I guess that my serialise function is at play here? The Date is certainly correct, but I am not sure if I should do anything else with the data before returning it from serialize? Right now I just return whatever I get from my MongoDB database.

antonwilhelm
  • 5,768
  • 4
  • 19
  • 45
  • https://stackoverflow.com/a/41513681/6124657 – xadm Jul 16 '21 at 18:10
  • Thanks, that explains the difference between these 2 functions. However, my question is a bit more broad and concerns scalars in GraphQL-JS - I wasn't able to find anything for that on here, so I really don't understand the downvote. – antonwilhelm Jul 17 '21 at 09:12
  • not a learning platform ... you can find explanations/tutorials ... https://moonhighway.com/creating-custom-scalars – xadm Jul 17 '21 at 10:05

1 Answers1

1

Urigo, from The Guild, created graphql-scalars that contains definitions for multiple common scalars used in 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. And yes, just return the value.

//I am just returning null here, because I do not know what this function is for

This is the entry from the abstract-syntax-tree (ast). See Urigo's code below to see how the ast object is accessed

  • ast.kind
  • ast.value

Additionally, take a look at this SO post that describes the difference between parseValue and parseLiteral

Take a look at localDate and that may provide you the example you need to answer your question :)

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);
  },
});
Manny
  • 369
  • 2
  • 7