GraphQL Scalar resolvers in Vue-Apollo

288 views Asked by At

I'm trying to write a Resolver for Date Scalar in my Vue-Apollo project.

In my Apollo server, I declared scalar Date and made a custom resolver for that type. Each time the server sends a Date object, a proper serialize function auto cast that type into an integer (which complied with JSON format) and vice versa.

However, in my Vue.js client side, I couldn't find any way to auto cast-back my Custom Scalars in Vue. I need to manually cast those timestamps (int) into Date type foreach query I made.

Here is an example for the server-side, taken from the Apollo's official website:

const resolvers = {
  Date: new GraphQLScalarType({
    name: 'Date',
    description: 'Date custom scalar type',
    parseValue(value) {
      return new Date(value); // value from the client
    },
    serialize(value) {
      return value.getTime(); // value sent to the client
    },
    parseLiteral(ast) {
      if (ast.kind === Kind.INT) {
        return parseInt(ast.value, 10); // ast value is always in string format
      }
      return null;
    },
  }),
};

Thanks for your time (no pun intended).

0

There are 0 answers