custom-scalar-plugin.ts 838 B

1234567891011121314151617181920212223242526272829303132
  1. import { CustomScalar, Resolver, Scalar } from '@nestjs/graphql';
  2. import { VendurePlugin } from '@vendure/core';
  3. import { Kind, ValueNode } from 'graphql';
  4. import gql from 'graphql-tag';
  5. @Resolver('MyDate')
  6. export class DateScalar implements CustomScalar<number, Date> {
  7. description = 'Date custom scalar type';
  8. parseValue(value: number): Date {
  9. return new Date(value); // value from the client
  10. }
  11. serialize(value: Date): number {
  12. return value.getTime(); // value sent to the client
  13. }
  14. parseLiteral(ast: ValueNode): Date | null {
  15. if (ast.kind === Kind.INT) {
  16. return new Date(ast.value);
  17. }
  18. return null;
  19. }
  20. }
  21. @VendurePlugin({
  22. providers: [DateScalar],
  23. shopApiExtensions: {
  24. resolvers: [DateScalar],
  25. },
  26. })
  27. export class CustomScalarPlugin {}