apollo-server-integrations / apollo-server-integration-cloudflare-workers

An integration to use Cloudflare Workers as a hosting service with Apollo Server
https://github.com/kimyvgy/worker-apollo-server-template
MIT License
13 stars 0 forks source link

Custom scalar resolvers #132

Open simplenotezy opened 1 week ago

simplenotezy commented 1 week ago

How can we go about creating custom scalar resolvers, e.g. for a DateTime scalar?

simplenotezy commented 1 week ago

I got away with this in the resolver:

  DateTime: new GraphQLScalarType({
    name: 'DateTime',
    description: 'A valid date-time value',
    parseValue(value: any) {
      return new Date(value); // value from the client input variables
    },
    serialize(value: any) {
      return value.toISOString(); // value sent to the client
    },
    parseLiteral(ast) {
      if (ast.kind === Kind.STRING) {
        return new Date(ast.value); // value from the client query
      }
      return null;
    },
  }),

Remember to add graphql to your package json.