-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow custom serialize/deserialize redis messages (#3037)
- Loading branch information
Showing
4 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphql-yoga/redis-event-target': minor | ||
--- | ||
|
||
Added `serializeMessage` and `deserializeMessage` options to `createRedisEventTarget` |
68 changes: 68 additions & 0 deletions
68
packages/event-target/redis-event-target/__tests__/serializer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import Redis from 'ioredis-mock'; | ||
import { CustomEvent } from '@whatwg-node/events'; | ||
import { createRedisEventTarget } from '../src'; | ||
|
||
describe('createRedisEventTarget: serializer arg', () => { | ||
it('uses native JSON by default', done => { | ||
const eventTarget = createRedisEventTarget({ | ||
publishClient: new Redis({}), | ||
subscribeClient: new Redis({}), | ||
}); | ||
|
||
eventTarget.addEventListener('a', (event: CustomEvent) => { | ||
// Serialized by JSON | ||
expect(event.detail).toEqual({ | ||
someNumber: 1, | ||
someBoolean: true, | ||
someText: 'hi', | ||
}); | ||
done(); | ||
}); | ||
|
||
const event = new CustomEvent('a', { | ||
detail: { | ||
someNumber: 1, | ||
someBoolean: true, | ||
someText: 'hi', | ||
}, | ||
}); | ||
eventTarget.dispatchEvent(event); | ||
}); | ||
|
||
it('can use a custom serializer', done => { | ||
const eventTarget = createRedisEventTarget({ | ||
publishClient: new Redis({}), | ||
subscribeClient: new Redis({}), | ||
serializer: { | ||
stringify: message => `__CUSTOM__${JSON.stringify(message)}`, | ||
parse: (message: string) => { | ||
const result = JSON.parse(message.replace(/^__CUSTOM__/, '')); | ||
for (const key in result) { | ||
if (typeof result[key] === 'number') { | ||
result[key]++; | ||
} | ||
} | ||
return result; | ||
}, | ||
}, | ||
}); | ||
|
||
eventTarget.addEventListener('b', (event: CustomEvent) => { | ||
expect(event.detail).toEqual({ | ||
someNumber: 2, | ||
someBoolean: true, | ||
someText: 'hi', | ||
}); | ||
done(); | ||
}); | ||
|
||
const event = new CustomEvent('b', { | ||
detail: { | ||
someNumber: 1, | ||
someBoolean: true, | ||
someText: 'hi', | ||
}, | ||
}); | ||
eventTarget.dispatchEvent(event); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters