question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for custom event serialization (Was: Serialization is breaking Dates)

See original GitHub issue

I’m using the Date type as in this documentation page https://www.apollographql.com/docs/graphql-tools/scalars.html

  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;
    },
  }),

I’m doing a findOne with Mongoose, and pass the returned document to the pubsub like this:

pubsub.publish('bookUpdated', { bookUpdated: book });

In my network tab, in the websocket, I’m getting the following error:

value.getTime is not a function in ["bookUpdated", "events", 1, "timestamp"]

The same code works without problem with the in-memory PubSub. This bug only affects RedisPubSub. Certainly because of the use of JSON.stringify and JSON.parse. See https://stackoverflow.com/questions/4511705/how-to-parse-json-to-receive-a-date-object-in-javascript

So I’m not sure if this can be considered a graphql-redis-subscriptions bug, but both PubSub are supposed to give the same result, I think.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
awwong1commented, Mar 15, 2018

I thought about this some more and made this a configurable option instead. Probably don’t want to make all users suffer a performance hit. Not setting the reviver defaults to existing functionality.

Took 10939.090882ms	 for 2000000 * JSON.parse(val)
Took 1097.195139ms	 for 200000 * JSON.parse(val)
Took 117.38736ms	 for 20000 * JSON.parse(val)
Took 14.088721ms	 for 2000 * JSON.parse(val)
Took 1.155448ms		 for 200 * JSON.parse(val)
Took 0.164362ms		 for 20 * JSON.parse(val)
Took 0.03583ms		 for 2 * JSON.parse(val)
Took 18939.705402ms	 for 2000000 * JSON.parse(val, dateRevier)
Took 1946.931366ms	 for 200000 * JSON.parse(val, dateRevier)
Took 200.24652ms	 for 20000 * JSON.parse(val, dateRevier)
Took 18.73888ms		 for 2000 * JSON.parse(val, dateRevier)
Took 2.133128ms		 for 200 * JSON.parse(val, dateRevier)
Took 0.377002ms		 for 20 * JSON.parse(val, dateRevier)
Took 0.069415ms		 for 2 * JSON.parse(val, dateRevier)

I think, in the future, better serialization and deserialization support is ideal (flattening the json object, handling custom constructors, etc.) but this works for our use case right now and has minimal impact.

0reactions
davidyahacommented, Apr 15, 2018

Hey @awwong1! Sorry for the delay, just published graphql-redis-subscriptions@1.5.0 Let me know if there are any problems with the new release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CA2229: Implement serialization constructors (code analysis)
A type supports custom serialization if it implements the ISerializable interface. The serialization constructor is required to deserialize, or ...
Read more >
json - Jackson SerializationFeature ...
Date fields from being serialised into timestamps when converting to JSON responses in my @RestController. However I cannot get it to work. All...
Read more >
Solved: event serialization using custom code
Solved: I am new to event serialization and could use some helpful tips from the community. I read that to implement event serialization,...
Read more >
Sitecore Serialization Guide
For more information about using event handlers, see the Serializing Items Using Event. Handlers section. •. You can use the Sitecore service page...
Read more >
Everything You Need to Know About Java Serialization ...
Customizing Serialization and Deserialization With writeObject and readObject Methods. The JVM has full control for serializing the object in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found