Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new KeyValueCache and friends from @apollo/utils.keyvaluecache #6522

Merged
merged 11 commits into from Jun 7, 2022
2 changes: 2 additions & 0 deletions .prettierignore
Expand Up @@ -10,3 +10,5 @@ docs/.cache/

# Don't format generated files!
**/generated/**

.volta
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -86,7 +86,7 @@ The version headers in this history reflect the versions of Apollo Server itself
new ApolloServer({
documentStore: new InMemoryLRUCache<DocumentNode>({
maxSize: Math.pow(2, 20) * approximateDocumentStoreMiB,
sizeCalculator: InMemoryLRUCache.jsonBytesSizeCalculator,
sizeCalculator: InMemoryLRUCache.sizeCalculator,
}),
...moreOptions,
})
Expand Down
1 change: 1 addition & 0 deletions cspell-dict.txt
Expand Up @@ -79,6 +79,7 @@ iteratees
josephg
jsdelivr
keyv
keyvaluecache
KHTML
Kubernetes
linearizability
Expand Down
4 changes: 2 additions & 2 deletions docs/source/api/apollo-server.mdx
Expand Up @@ -209,12 +209,12 @@ To use `InMemoryLRUCache` but change its size to an amount `approximateDocumentS
<div style="max-width: 400px;">

```typescript
import { InMemoryLRUCache } from 'apollo-server-caching';
import { InMemoryLRUCache } from '@apollo/utils.keyvaluecache';
import type { DocumentNode } from 'graphql';
new ApolloServer({
documentStore: new InMemoryLRUCache<DocumentNode>({
maxSize: Math.pow(2, 20) * approximateDocumentStoreMiB,
sizeCalculator: InMemoryLRUCache.jsonBytesSizeCalculator,
sizeCalculation: InMemoryLRUCache.sizeCalculation,
}),
// ...
})
Expand Down
59 changes: 47 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/apollo-datasource-rest/package.json
Expand Up @@ -18,8 +18,8 @@
"node": ">=12.0"
},
"dependencies": {
"@apollo/utils.keyvaluecache": "^1.0.1",
"apollo-datasource": "file:../apollo-datasource",
"apollo-server-caching": "file:../apollo-server-caching",
"apollo-server-env": "file:../apollo-server-env",
"apollo-server-errors": "file:../apollo-server-errors",
"http-cache-semantics": "^4.1.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-datasource-rest/src/HTTPCache.ts
Expand Up @@ -6,7 +6,7 @@ import {
KeyValueCache,
InMemoryLRUCache,
PrefixingKeyValueCache,
} from 'apollo-server-caching';
} from '@apollo/utils.keyvaluecache';
import type { CacheOptions } from './RESTDataSource';

export class HTTPCache {
Expand Down
13 changes: 10 additions & 3 deletions packages/apollo-datasource-rest/src/__tests__/HTTPCache.test.ts
Expand Up @@ -352,8 +352,11 @@ describe('HTTPCache', () => {

await httpCache.fetch(new Request('https://api.example.com/people/1'));

expect(storeSet.mock.calls[0][2]).toEqual({ ttl: 30 });

expect(storeSet).toHaveBeenCalledWith(
expect.any(String),
expect.any(String),
{ ttl: 30 },
);
storeSet.mockRestore();
});

Expand All @@ -367,7 +370,11 @@ describe('HTTPCache', () => {

await httpCache.fetch(new Request('https://api.example.com/people/1'));

expect(storeSet.mock.calls[0][2]).toEqual({ ttl: 60 });
expect(storeSet).toHaveBeenCalledWith(
expect.any(String),
expect.any(String),
{ ttl: 60 },
);

storeSet.mockRestore();
});
Expand Down
@@ -1,14 +1,11 @@
import type {
KeyValueCache,
KeyValueCacheSetOptions,
} from 'apollo-server-caching';
import type { KeyValueCache } from '@apollo/utils.keyvaluecache';

export class MapKeyValueCache<V> implements KeyValueCache<V> {
store = new Map<string, V>();
async get(key: string) {
return this.store.get(key);
}
async set(key: string, value: V, _?: KeyValueCacheSetOptions) {
async set(key: string, value: V) {
this.store.set(key, value);
}
async delete(key: string) {
Expand Down
1 change: 0 additions & 1 deletion packages/apollo-datasource-rest/tsconfig.json
Expand Up @@ -8,7 +8,6 @@
"exclude": ["**/__tests__"],
"references": [
{ "path": "../apollo-datasource" },
{ "path": "../apollo-server-caching" },
{ "path": "../apollo-server-errors" },
]
}
2 changes: 1 addition & 1 deletion packages/apollo-datasource/package.json
Expand Up @@ -18,7 +18,7 @@
"node": ">=12.0"
},
"dependencies": {
"apollo-server-caching": "file:../apollo-server-caching",
"@apollo/utils.keyvaluecache": "^1.0.1",
"apollo-server-env": "file:../apollo-server-env"
}
}
2 changes: 1 addition & 1 deletion packages/apollo-datasource/src/index.ts
@@ -1,4 +1,4 @@
import type { KeyValueCache } from 'apollo-server-caching';
import type { KeyValueCache } from '@apollo/utils.keyvaluecache';

export interface DataSourceConfig<TContext> {
context: TContext;
Expand Down
4 changes: 1 addition & 3 deletions packages/apollo-datasource/tsconfig.json
Expand Up @@ -6,7 +6,5 @@
},
"include": ["src/**/*"],
"exclude": ["**/__tests__"],
"references": [
{ "path": "../apollo-server-caching" },
]
"references": []
}
1 change: 1 addition & 0 deletions packages/apollo-server-cache-memcached/package.json
Expand Up @@ -18,6 +18,7 @@
"node": ">=12.0"
},
"dependencies": {
"@apollo/utils.keyvaluecache": "^1.0.1",
"apollo-server-caching": "file:../apollo-server-caching",
"apollo-server-env": "file:../apollo-server-env",
"memcached": "^2.2.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-cache-memcached/src/index.ts
@@ -1,7 +1,7 @@
import type {
KeyValueCache,
KeyValueCacheSetOptions,
} from 'apollo-server-caching';
} from '@apollo/utils.keyvaluecache';
import Memcached from 'memcached';
import { promisify } from 'util';

Expand Down
4 changes: 1 addition & 3 deletions packages/apollo-server-cache-memcached/tsconfig.json
Expand Up @@ -6,7 +6,5 @@
},
"include": ["src/**/*"],
"exclude": ["**/__tests__"],
"references": [
{ "path": "../apollo-server-caching" },
]
"references": []
}
2 changes: 1 addition & 1 deletion packages/apollo-server-cache-redis/package.json
Expand Up @@ -18,7 +18,7 @@
"node": ">=12.0"
},
"dependencies": {
"apollo-server-caching": "file:../apollo-server-caching",
"@apollo/utils.keyvaluecache": "^1.0.1",
"apollo-server-env": "file:../apollo-server-env",
"dataloader": "^2.0.0",
"ioredis": "^4.17.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-cache-redis/src/BaseRedisCache.ts
@@ -1,7 +1,7 @@
import type {
KeyValueCache,
KeyValueCacheSetOptions,
} from 'apollo-server-caching';
} from '@apollo/utils.keyvaluecache';
import DataLoader from 'dataloader';

interface BaseRedisClient {
Expand Down
4 changes: 1 addition & 3 deletions packages/apollo-server-cache-redis/tsconfig.json
Expand Up @@ -6,7 +6,5 @@
},
"include": ["src/**/*"],
"exclude": ["**/__tests__"],
"references": [
{ "path": "../apollo-server-caching" },
]
"references": []
}
2 changes: 1 addition & 1 deletion packages/apollo-server-core/package.json
Expand Up @@ -25,6 +25,7 @@
"node": ">=12.0"
},
"dependencies": {
"@apollo/utils.keyvaluecache": "^1.0.1",
"@apollo/utils.logger": "^1.0.0",
"@apollo/utils.usagereporting": "^1.0.0",
"@apollographql/apollo-tools": "^0.5.3",
Expand All @@ -34,7 +35,6 @@
"@josephg/resolvable": "^1.0.0",
"apollo-datasource": "file:../apollo-datasource",
"apollo-reporting-protobuf": "file:../apollo-reporting-protobuf",
"apollo-server-caching": "file:../apollo-server-caching",
"apollo-server-env": "file:../apollo-server-env",
"apollo-server-errors": "file:../apollo-server-errors",
"apollo-server-plugin-base": "file:../apollo-server-plugin-base",
Expand Down