Skip to content

Commit fb6e935

Browse files
authored
Accept and support the upcoming Hive PubSub (#8716)
1 parent ad3cf28 commit fb6e935

File tree

20 files changed

+314
-59
lines changed

20 files changed

+314
-59
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@graphql-mesh/types": patch
3+
---
4+
dependencies updates:
5+
- Added dependency [`@graphql-hive/pubsub@next` ↗︎](https://www.npmjs.com/package/@graphql-hive/pubsub/v/next) (to `dependencies`)
6+
- Added dependency [`@repeaterjs/repeater@^3.0.6` ↗︎](https://www.npmjs.com/package/@repeaterjs/repeater/v/3.0.6) (to `dependencies`)
7+
- Added dependency [`@whatwg-node/disposablestack@^0.0.6` ↗︎](https://www.npmjs.com/package/@whatwg-node/disposablestack/v/0.0.6) (to `dependencies`)

.changeset/cute-ducks-brush.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@omnigraph/json-schema': patch
3+
'@graphql-mesh/cache-inmemory-lru': patch
4+
'@graphql-mesh/plugin-live-query': patch
5+
'@graphql-mesh/cache-localforage': patch
6+
'@graphql-mesh/transport-neo4j': patch
7+
'@graphql-mesh/transport-rest': patch
8+
'@omnigraph/neo4j': patch
9+
'@graphql-mesh/utils': patch
10+
'@graphql-mesh/cache-redis': patch
11+
---
12+
13+
Accept and support the upcoming and improved `HivePubSub` from `@graphql-hive/pubsub`

.changeset/hungry-suns-appear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-mesh/types': patch
3+
---
4+
5+
Export the upcoming and improved `HivePubSub` from `@graphql-hive/pubsub` together with a utility to detect and convert it to the legacy `MeshPubSub`

e2e/openapi-additional-resolvers/mesh.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ export const composeConfig = defineComposeConfig({
2323
});
2424

2525
export const gatewayConfig = defineGatewayConfig({
26+
transportEntries: {
27+
'*.rest': {
28+
headers: [['user-agent', 'hive-gateway/e2e']],
29+
},
30+
},
2631
additionalResolvers: {
2732
pageview_project: {
2833
banana() {

e2e/openapi-javascript-wiki/mesh.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export const composeConfig = defineComposeConfig({
2525
});
2626

2727
export const gatewayConfig = defineGatewayConfig({
28+
transportEntries: {
29+
'*.rest': {
30+
headers: [['user-agent', 'hive-gateway/e2e']],
31+
},
32+
},
2833
additionalResolvers: {
2934
Query: {
3035
async viewsInPastMonth(root, { project }, context: any, info) {

packages/cache/inmemory-lru/src/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
import type { KeyValueCache, KeyValueCacheSetOptions, MeshPubSub } from '@graphql-mesh/types';
1+
import {
2+
toMeshPubSub,
3+
type HivePubSub,
4+
type KeyValueCache,
5+
type KeyValueCacheSetOptions,
6+
type MeshPubSub,
7+
} from '@graphql-mesh/types';
28
import { createLruCache, type LRUCache } from '@graphql-mesh/utils';
39
import { DisposableSymbols } from '@whatwg-node/disposablestack';
410

511
export interface InMemoryLRUCacheOptions {
612
max?: number;
713
ttl?: number;
8-
pubsub?: MeshPubSub;
14+
pubsub?: MeshPubSub | HivePubSub;
915
}
1016

1117
export default class InMemoryLRUCache<V = any> implements KeyValueCache<V>, Disposable {
1218
private lru: LRUCache;
1319
private timeouts = new Map<string, ReturnType<typeof setTimeout>>();
1420
constructor(options?: InMemoryLRUCacheOptions) {
1521
this.lru = createLruCache(options?.max, options?.ttl);
16-
const subId = options?.pubsub?.subscribe?.('destroy', () => {
17-
options?.pubsub?.unsubscribe(subId);
22+
const pubsub = toMeshPubSub(options?.pubsub);
23+
const subId = pubsub?.subscribe?.('destroy', () => {
24+
pubsub.unsubscribe(subId);
1825
this[DisposableSymbols.dispose]();
1926
});
2027
}

packages/cache/localforage/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import LocalForage from 'localforage';
22
import InMemoryLRUCache from '@graphql-mesh/cache-inmemory-lru';
33
import type {
4+
HivePubSub,
45
KeyValueCache,
56
KeyValueCacheSetOptions,
67
MeshPubSub,
@@ -9,7 +10,7 @@ import type {
910

1011
export default class LocalforageCache<V = any> implements KeyValueCache<V> {
1112
private localforage: LocalForage;
12-
constructor(config?: YamlConfig.LocalforageConfig & { pubsub?: MeshPubSub }) {
13+
constructor(config?: YamlConfig.LocalforageConfig & { pubsub?: MeshPubSub | HivePubSub }) {
1314
const driverNames = config?.driver || ['INDEXEDDB', 'WEBSQL', 'LOCALSTORAGE'];
1415
if (driverNames.every(driverName => !LocalForage.supports(driverName))) {
1516
return new InMemoryLRUCache({ pubsub: config?.pubsub }) as any;

packages/cache/redis/src/index.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import Redis, { type Cluster } from 'ioredis';
22
import RedisMock from 'ioredis-mock';
33
import { process } from '@graphql-mesh/cross-helpers';
44
import { stringInterpolator } from '@graphql-mesh/string-interpolation';
5-
import type {
6-
KeyValueCache,
7-
KeyValueCacheSetOptions,
8-
Logger,
9-
MeshPubSub,
10-
YamlConfig,
5+
import {
6+
toMeshPubSub,
7+
type HivePubSub,
8+
type KeyValueCache,
9+
type KeyValueCacheSetOptions,
10+
type Logger,
11+
type MeshPubSub,
12+
type YamlConfig,
1113
} from '@graphql-mesh/types';
1214
import { DisposableSymbols } from '@whatwg-node/disposablestack';
1315

@@ -18,7 +20,9 @@ function interpolateStrWithEnv(str: string): string {
1820
export default class RedisCache<V = string> implements KeyValueCache<V>, Disposable {
1921
private client: Redis | Cluster;
2022

21-
constructor(options: YamlConfig.Cache['redis'] & { pubsub?: MeshPubSub; logger: Logger }) {
23+
constructor(
24+
options: YamlConfig.Cache['redis'] & { pubsub?: MeshPubSub | HivePubSub; logger: Logger },
25+
) {
2226
const lazyConnect = options.lazyConnect !== false;
2327
if ('startupNodes' in options) {
2428
const parsedUsername =
@@ -117,10 +121,11 @@ export default class RedisCache<V = string> implements KeyValueCache<V>, Disposa
117121
this.client = new RedisMock();
118122
}
119123
}
124+
const pubsub = toMeshPubSub(options.pubsub);
120125
// TODO: PubSub.destroy will no longer be needed after v0
121-
const id = options.pubsub?.subscribe('destroy', () => {
126+
const id = pubsub?.subscribe('destroy', () => {
122127
this.client.disconnect(false);
123-
options.pubsub.unsubscribe(id);
128+
pubsub.unsubscribe(id);
124129
});
125130
}
126131

packages/legacy/types/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@
3535
"graphql": "*"
3636
},
3737
"dependencies": {
38+
"@graphql-hive/pubsub": "next",
3839
"@graphql-mesh/store": "^0.104.7",
3940
"@graphql-tools/batch-delegate": "^9.0.10",
4041
"@graphql-tools/delegate": "^10.0.28",
4142
"@graphql-tools/utils": "^10.8.0",
4243
"@graphql-typed-document-node/core": "^3.2.0",
44+
"@repeaterjs/repeater": "^3.0.6",
45+
"@whatwg-node/disposablestack": "^0.0.6",
4346
"tslib": "^2.4.0"
4447
},
4548
"devDependencies": {

packages/legacy/types/src/index.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import type {
1414
import type { ExecutionRequest, Executor, IResolvers, MaybePromise } from '@graphql-tools/utils';
1515
import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
1616
import * as YamlConfig from './config.js';
17+
import type { MeshPubSub } from './pubsub.js';
18+
19+
export * from './pubsub.js';
1720

1821
export { jsonSchema } from './config-schema.js';
1922

@@ -65,25 +68,6 @@ export interface MeshHandlerLibrary<TConfig = any> {
6568
new (options: MeshHandlerOptions<TConfig>): MeshHandler;
6669
}
6770

68-
// Hooks
69-
export type AllHooks = {
70-
destroy: void;
71-
[key: string]: any;
72-
};
73-
export type HookName = keyof AllHooks & string;
74-
75-
export interface MeshPubSub {
76-
publish<THook extends HookName>(triggerName: THook, payload: AllHooks[THook]): void;
77-
subscribe<THook extends HookName>(
78-
triggerName: THook,
79-
onMessage: (data: AllHooks[THook]) => void,
80-
options?: any,
81-
): number;
82-
unsubscribe(subId: number): void;
83-
getEventNames(): Iterable<string>;
84-
asyncIterator<THook extends HookName>(triggers: THook): AsyncIterable<AllHooks[THook]>;
85-
}
86-
8771
export interface MeshTransformOptions<Config = any> {
8872
apiName: string;
8973
config: Config;

0 commit comments

Comments
 (0)