Skip to content

Commit

Permalink
Use Promise helpers (#8431)
Browse files Browse the repository at this point in the history
* Use Promise helpers

* chore(dependencies): updated changesets for modified dependencies

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ardatan and github-actions[bot] authored Feb 25, 2025
1 parent 4d9624a commit 3f5e0a1
Show file tree
Hide file tree
Showing 76 changed files with 805 additions and 588 deletions.
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_apollo-link-8431-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/apollo-link": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_cache-cfw-kv-8431-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/cache-cfw-kv": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_cache-redis-8431-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/cache-redis": patch
---
dependencies updates:
- Removed dependency [`@graphql-mesh/utils@^0.103.19` ↗︎](https://www.npmjs.com/package/@graphql-mesh/utils/v/0.103.19) (from `dependencies`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/cache-upstash-redis": patch
---
dependencies updates:
- Removed dependency [`@graphql-mesh/utils@^0.103.19` ↗︎](https://www.npmjs.com/package/@graphql-mesh/utils/v/0.103.19) (from `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_cli-8431-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/cli": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_compose-cli-8431-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/compose-cli": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/plugin-deduplicate-request": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/plugin-http-cache": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_plugin-newrelic-8431-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/plugin-newrelic": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/plugin-operation-headers": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/plugin-rate-limit": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/plugin-response-cache": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_runtime-8431-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/runtime": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_transport-grpc-8431-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/transport-grpc": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/transport-sqlite": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_utils-8431-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/utils": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@omnigraph_odata-8431-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@omnigraph/odata": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@omnigraph_soap-8431-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@omnigraph/soap": patch
---
dependencies updates:
- Added dependency [`@whatwg-node/promise-helpers@^1.0.0` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.0.0) (to `dependencies`)
1 change: 1 addition & 0 deletions packages/cache/cfw-kv/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"@graphql-mesh/types": "^0.103.19",
"@graphql-mesh/utils": "^0.103.19",
"@whatwg-node/promise-helpers": "^1.0.0",
"tslib": "^2.4.0"
},
"devDependencies": {
Expand Down
31 changes: 15 additions & 16 deletions packages/cache/cfw-kv/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="@cloudflare/workers-types" />
import type { KeyValueCache, Logger } from '@graphql-mesh/types';
import { mapMaybePromise } from '@graphql-mesh/utils';
import { handleMaybePromise } from '@whatwg-node/promise-helpers';

export default class CFWorkerKVCache implements KeyValueCache {
private kvNamespace?: KVNamespace;
Expand All @@ -21,13 +21,16 @@ export default class CFWorkerKVCache implements KeyValueCache {
}

getKeysByPrefix(prefix: string) {
return mapMaybePromise(this.kvNamespace?.list({ prefix }), result => {
if (!result) {
return [];
}
return handleMaybePromise(
() => this.kvNamespace?.list({ prefix }),
result => {
if (!result) {
return [];
}

return result.keys.map(keyEntry => keyEntry.name);
});
return result.keys.map(keyEntry => keyEntry.name);
},
);
}

set(key: string, value: any, options?: { ttl?: number }): Promise<void> {
Expand All @@ -37,14 +40,10 @@ export default class CFWorkerKVCache implements KeyValueCache {
}

delete(key: string) {
try {
return mapMaybePromise(
this.kvNamespace?.delete(key),
() => true,
() => false,
);
} catch (e) {
return false;
}
return handleMaybePromise(
() => this.kvNamespace?.delete(key),
() => true,
() => false,
);
}
}
16 changes: 5 additions & 11 deletions packages/cache/localforage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
MeshPubSub,
YamlConfig,
} from '@graphql-mesh/types';
import { mapMaybePromise } from '@graphql-mesh/utils';

export default class LocalforageCache<V = any> implements KeyValueCache<V> {
private localforage: LocalForage;
Expand All @@ -26,21 +25,17 @@ export default class LocalforageCache<V = any> implements KeyValueCache<V> {
}

get(key: string) {
const getItem = () => {
return this.localforage.getItem<V>(key.toString());
};
return mapMaybePromise(this.localforage.getItem<number>(`${key}.expiresAt`), expiresAt => {
const getItem = () => this.localforage.getItem<V>(key.toString());
return this.localforage.getItem<number>(`${key}.expiresAt`).then(expiresAt => {
if (expiresAt && Date.now() > expiresAt) {
return mapMaybePromise(this.localforage.removeItem(key), getItem);
return this.localforage.removeItem(key).then(() => getItem());
}
return getItem();
});
}

getKeysByPrefix(prefix: string) {
return mapMaybePromise(this.localforage.keys(), keys =>
keys.filter(key => key.startsWith(prefix)),
);
return this.localforage.keys().then(keys => keys.filter(key => key.startsWith(prefix)));
}

set(key: string, value: V, options?: KeyValueCacheSetOptions) {
Expand All @@ -52,8 +47,7 @@ export default class LocalforageCache<V = any> implements KeyValueCache<V> {
}

delete(key: string) {
return mapMaybePromise(
this.localforage.removeItem(key),
return this.localforage.removeItem(key).then(
() => true,
() => false,
);
Expand Down
1 change: 0 additions & 1 deletion packages/cache/redis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"@graphql-mesh/cross-helpers": "^0.4.10",
"@graphql-mesh/string-interpolation": "0.5.8",
"@graphql-mesh/types": "^0.103.19",
"@graphql-mesh/utils": "^0.103.19",
"@whatwg-node/disposablestack": "^0.0.5",
"ioredis": "^5.3.2",
"ioredis-mock": "^8.8.3",
Expand Down
35 changes: 12 additions & 23 deletions packages/cache/redis/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type {
MeshPubSub,
YamlConfig,
} from '@graphql-mesh/types';
import { mapMaybePromise } from '@graphql-mesh/utils';
import { DisposableSymbols } from '@whatwg-node/disposablestack';

function interpolateStrWithEnv(str: string): string {
Expand Down Expand Up @@ -109,37 +108,27 @@ export default class RedisCache<V = string> implements KeyValueCache<V>, Disposa
}

get(key: string): Promise<V | undefined> {
return mapMaybePromise(this.client.get(key), value => {
return value != null ? JSON.parse(value) : undefined;
});
return this.client.get(key).then(value => (value != null ? JSON.parse(value) : undefined));
}

getKeysByPrefix(prefix: string): Promise<string[]> {
return scanPatterns(this.client, `${prefix}*`);
}

delete(key: string): PromiseLike<boolean> | boolean {
try {
return mapMaybePromise(
this.client.del(key),
value => value > 0,
() => false,
);
} catch (e) {
return false;
}
return this.client.del(key).then(
value => value > 0,
() => false,
);
}
}

function scanPatterns(redis: Redis, pattern: string, cursor: string = '0', keys: string[] = []) {
return mapMaybePromise(
redis.scan(cursor, 'MATCH', pattern, 'COUNT', '10'),
([nextCursor, nextKeys]) => {
keys.push(...nextKeys);
if (nextCursor === '0') {
return keys;
}
return scanPatterns(redis, pattern, nextCursor, keys);
},
);
return redis.scan(cursor, 'MATCH', pattern, 'COUNT', '10').then(([nextCursor, nextKeys]) => {
keys.push(...nextKeys);
if (nextCursor === '0') {
return keys;
}
return scanPatterns(redis, pattern, nextCursor, keys);
});
}
1 change: 0 additions & 1 deletion packages/cache/upstash-redis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"dependencies": {
"@graphql-mesh/cross-helpers": "^0.4.9",
"@graphql-mesh/types": "^0.103.19",
"@graphql-mesh/utils": "^0.103.19",
"@upstash/redis": "^1.34.3",
"@whatwg-node/disposablestack": "^0.0.5",
"tslib": "^2.4.0"
Expand Down
3 changes: 1 addition & 2 deletions packages/cache/upstash-redis/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { process } from '@graphql-mesh/cross-helpers';
import type { KeyValueCache } from '@graphql-mesh/types';
import { mapMaybePromise } from '@graphql-mesh/utils';
import { Redis, type RedisConfigNodejs } from '@upstash/redis';
import { DisposableSymbols } from '@whatwg-node/disposablestack';

Expand Down Expand Up @@ -33,7 +32,7 @@ export default class UpstashRedisCache implements KeyValueCache {
}

delete(key: string) {
return mapMaybePromise(this.redis.del(key), num => num > 0);
return this.redis.del(key).then(num => num > 0);
}

async getKeysByPrefix(prefix: string): Promise<string[]> {
Expand Down
1 change: 1 addition & 0 deletions packages/compose-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@graphql-tools/schema": "^10.0.5",
"@graphql-tools/utils": "^10.8.0",
"@whatwg-node/fetch": "^0.10.4",
"@whatwg-node/promise-helpers": "^1.0.0",
"commander": "^13.0.0",
"dotenv": "^16.3.1"
},
Expand Down
Loading

0 comments on commit 3f5e0a1

Please sign in to comment.