@envelop/apollo-federation 3.0.0-alpha-36a1267.0 → 3.0.0-alpha-5b6bfa8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -10
- package/{index.js → cjs/index.js} +14 -47
- package/cjs/new-cache-policy.js +35 -0
- package/cjs/package.json +1 -0
- package/{index.mjs → esm/index.js} +5 -38
- package/esm/new-cache-policy.js +31 -0
- package/package.json +34 -13
- /package/{index.d.ts → typings/index.d.ts} +0 -0
- /package/{newCachePolicy.d.ts → typings/new-cache-policy.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -11,27 +11,27 @@ yarn add @envelop/apollo-federation
|
|
|
11
11
|
## Usage Example
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
|
-
import { envelop } from '@envelop/core'
|
|
15
|
-
import { ApolloGateway } from '@apollo/gateway'
|
|
16
|
-
import { useApolloFederation } from '@envelop/apollo-federation'
|
|
14
|
+
import { envelop } from '@envelop/core'
|
|
15
|
+
import { ApolloGateway } from '@apollo/gateway'
|
|
16
|
+
import { useApolloFederation } from '@envelop/apollo-federation'
|
|
17
17
|
|
|
18
18
|
// Initialize the gateway
|
|
19
19
|
const gateway = new ApolloGateway({
|
|
20
20
|
serviceList: [
|
|
21
21
|
{ name: 'accounts', url: 'http://localhost:4001' },
|
|
22
|
-
{ name: 'products', url: 'http://localhost:4002' }
|
|
22
|
+
{ name: 'products', url: 'http://localhost:4002' }
|
|
23
23
|
// ...additional subgraphs...
|
|
24
|
-
]
|
|
25
|
-
})
|
|
24
|
+
]
|
|
25
|
+
})
|
|
26
26
|
|
|
27
27
|
// Make sure all services are loaded
|
|
28
|
-
await gateway.load()
|
|
28
|
+
await gateway.load()
|
|
29
29
|
|
|
30
30
|
// Then pass it to the plugin configuration
|
|
31
31
|
const getEnveloped = envelop({
|
|
32
32
|
plugins: [
|
|
33
33
|
// ... other plugins ...
|
|
34
|
-
useApolloFederation({ gateway })
|
|
35
|
-
]
|
|
36
|
-
})
|
|
34
|
+
useApolloFederation({ gateway })
|
|
35
|
+
]
|
|
36
|
+
})
|
|
37
37
|
```
|
|
@@ -1,44 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
// Taken from https://github.com/apollographql/apollo-server/blob/main/packages/apollo-server-core/src/cachePolicy.ts
|
|
10
|
-
function newCachePolicy() {
|
|
11
|
-
return {
|
|
12
|
-
maxAge: undefined,
|
|
13
|
-
scope: undefined,
|
|
14
|
-
restrict(hint) {
|
|
15
|
-
if (hint.maxAge !== undefined && (this.maxAge === undefined || hint.maxAge < this.maxAge)) {
|
|
16
|
-
this.maxAge = hint.maxAge;
|
|
17
|
-
}
|
|
18
|
-
if (hint.scope !== undefined && this.scope !== apolloServerTypes.CacheScope.Private) {
|
|
19
|
-
this.scope = hint.scope;
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
|
-
replace(hint) {
|
|
23
|
-
if (hint.maxAge !== undefined) {
|
|
24
|
-
this.maxAge = hint.maxAge;
|
|
25
|
-
}
|
|
26
|
-
if (hint.scope !== undefined) {
|
|
27
|
-
this.scope = hint.scope;
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
policyIfCacheable() {
|
|
31
|
-
var _a;
|
|
32
|
-
if (this.maxAge === undefined || this.maxAge === 0) {
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
return { maxAge: this.maxAge, scope: (_a = this.scope) !== null && _a !== void 0 ? _a : apolloServerTypes.CacheScope.Public };
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useApolloFederation = void 0;
|
|
4
|
+
const graphql_1 = require("@graphql-tools/graphql");
|
|
5
|
+
const apollo_server_caching_1 = require("apollo-server-caching");
|
|
6
|
+
const new_cache_policy_js_1 = require("./new-cache-policy.js");
|
|
7
|
+
const compat_1 = require("@graphql-tools/compat");
|
|
40
8
|
const useApolloFederation = (options) => {
|
|
41
|
-
const { gateway, cache = new
|
|
9
|
+
const { gateway, cache = new apollo_server_caching_1.InMemoryLRUCache(), logger = console, metrics = Object.create(null), overallCachePolicy = (0, new_cache_policy_js_1.newCachePolicy)(), } = options;
|
|
42
10
|
let schemaHash;
|
|
43
11
|
const documentSourceMap = new WeakMap();
|
|
44
12
|
return {
|
|
@@ -50,13 +18,13 @@ const useApolloFederation = (options) => {
|
|
|
50
18
|
logger.warn(`ApolloGateway doesn't have the schema loaded. Please make sure ApolloGateway is loaded with .load() method. Otherwise this plugin might not work consistently, especially if you are using ApolloServer.`);
|
|
51
19
|
gateway.load();
|
|
52
20
|
}
|
|
53
|
-
gateway.onSchemaLoadOrUpdate(({ apiSchema, coreSupergraphSdl =
|
|
21
|
+
gateway.onSchemaLoadOrUpdate(({ apiSchema, coreSupergraphSdl = (0, graphql_1.printSchema)(apiSchema) }) => {
|
|
54
22
|
setSchema(apiSchema);
|
|
55
|
-
schemaHash = (coreSupergraphSdl ||
|
|
23
|
+
schemaHash = (coreSupergraphSdl || (0, graphql_1.printSchema)(apiSchema));
|
|
56
24
|
});
|
|
57
25
|
},
|
|
58
26
|
onParse({ params: { source } }) {
|
|
59
|
-
const key = source instanceof
|
|
27
|
+
const key = source instanceof graphql_1.Source ? source.body : source;
|
|
60
28
|
return ({ result }) => {
|
|
61
29
|
if (!result || result instanceof Error)
|
|
62
30
|
return;
|
|
@@ -67,10 +35,10 @@ const useApolloFederation = (options) => {
|
|
|
67
35
|
var _a;
|
|
68
36
|
let documentStr = documentSourceMap.get(args.document);
|
|
69
37
|
if (documentStr == null) {
|
|
70
|
-
documentStr =
|
|
38
|
+
documentStr = (0, graphql_1.print)(args.document);
|
|
71
39
|
documentSourceMap.set(args.document, documentStr);
|
|
72
40
|
}
|
|
73
|
-
const operation =
|
|
41
|
+
const operation = (0, graphql_1.getOperationAST)(args.document, (_a = args.operationName) !== null && _a !== void 0 ? _a : undefined);
|
|
74
42
|
if (!operation) {
|
|
75
43
|
throw new Error(`Operation ${args.operationName || ''} cannot be found in ${documentStr}`);
|
|
76
44
|
}
|
|
@@ -92,12 +60,11 @@ const useApolloFederation = (options) => {
|
|
|
92
60
|
metrics,
|
|
93
61
|
source: documentStr,
|
|
94
62
|
operation,
|
|
95
|
-
schema: args.schema,
|
|
63
|
+
schema: (0, compat_1.compatSchema)(args.schema),
|
|
96
64
|
schemaHash,
|
|
97
65
|
});
|
|
98
66
|
});
|
|
99
67
|
},
|
|
100
68
|
};
|
|
101
69
|
};
|
|
102
|
-
|
|
103
70
|
exports.useApolloFederation = useApolloFederation;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.newCachePolicy = void 0;
|
|
4
|
+
// Taken from https://github.com/apollographql/apollo-server/blob/main/packages/apollo-server-core/src/cachePolicy.ts
|
|
5
|
+
const apollo_server_types_1 = require("apollo-server-types");
|
|
6
|
+
function newCachePolicy() {
|
|
7
|
+
return {
|
|
8
|
+
maxAge: undefined,
|
|
9
|
+
scope: undefined,
|
|
10
|
+
restrict(hint) {
|
|
11
|
+
if (hint.maxAge !== undefined && (this.maxAge === undefined || hint.maxAge < this.maxAge)) {
|
|
12
|
+
this.maxAge = hint.maxAge;
|
|
13
|
+
}
|
|
14
|
+
if (hint.scope !== undefined && this.scope !== apollo_server_types_1.CacheScope.Private) {
|
|
15
|
+
this.scope = hint.scope;
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
replace(hint) {
|
|
19
|
+
if (hint.maxAge !== undefined) {
|
|
20
|
+
this.maxAge = hint.maxAge;
|
|
21
|
+
}
|
|
22
|
+
if (hint.scope !== undefined) {
|
|
23
|
+
this.scope = hint.scope;
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
policyIfCacheable() {
|
|
27
|
+
var _a;
|
|
28
|
+
if (this.maxAge === undefined || this.maxAge === 0) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return { maxAge: this.maxAge, scope: (_a = this.scope) !== null && _a !== void 0 ? _a : apollo_server_types_1.CacheScope.Public };
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
exports.newCachePolicy = newCachePolicy;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -1,39 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getOperationAST, print, printSchema, Source } from '@graphql-tools/graphql';
|
|
2
2
|
import { InMemoryLRUCache } from 'apollo-server-caching';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
function newCachePolicy() {
|
|
7
|
-
return {
|
|
8
|
-
maxAge: undefined,
|
|
9
|
-
scope: undefined,
|
|
10
|
-
restrict(hint) {
|
|
11
|
-
if (hint.maxAge !== undefined && (this.maxAge === undefined || hint.maxAge < this.maxAge)) {
|
|
12
|
-
this.maxAge = hint.maxAge;
|
|
13
|
-
}
|
|
14
|
-
if (hint.scope !== undefined && this.scope !== CacheScope.Private) {
|
|
15
|
-
this.scope = hint.scope;
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
replace(hint) {
|
|
19
|
-
if (hint.maxAge !== undefined) {
|
|
20
|
-
this.maxAge = hint.maxAge;
|
|
21
|
-
}
|
|
22
|
-
if (hint.scope !== undefined) {
|
|
23
|
-
this.scope = hint.scope;
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
policyIfCacheable() {
|
|
27
|
-
var _a;
|
|
28
|
-
if (this.maxAge === undefined || this.maxAge === 0) {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
return { maxAge: this.maxAge, scope: (_a = this.scope) !== null && _a !== void 0 ? _a : CacheScope.Public };
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const useApolloFederation = (options) => {
|
|
3
|
+
import { newCachePolicy } from './new-cache-policy.js';
|
|
4
|
+
import { compatSchema } from '@graphql-tools/compat';
|
|
5
|
+
export const useApolloFederation = (options) => {
|
|
37
6
|
const { gateway, cache = new InMemoryLRUCache(), logger = console, metrics = Object.create(null), overallCachePolicy = newCachePolicy(), } = options;
|
|
38
7
|
let schemaHash;
|
|
39
8
|
const documentSourceMap = new WeakMap();
|
|
@@ -88,12 +57,10 @@ const useApolloFederation = (options) => {
|
|
|
88
57
|
metrics,
|
|
89
58
|
source: documentStr,
|
|
90
59
|
operation,
|
|
91
|
-
schema: args.schema,
|
|
60
|
+
schema: compatSchema(args.schema),
|
|
92
61
|
schemaHash,
|
|
93
62
|
});
|
|
94
63
|
});
|
|
95
64
|
},
|
|
96
65
|
};
|
|
97
66
|
};
|
|
98
|
-
|
|
99
|
-
export { useApolloFederation };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Taken from https://github.com/apollographql/apollo-server/blob/main/packages/apollo-server-core/src/cachePolicy.ts
|
|
2
|
+
import { CacheScope } from 'apollo-server-types';
|
|
3
|
+
export function newCachePolicy() {
|
|
4
|
+
return {
|
|
5
|
+
maxAge: undefined,
|
|
6
|
+
scope: undefined,
|
|
7
|
+
restrict(hint) {
|
|
8
|
+
if (hint.maxAge !== undefined && (this.maxAge === undefined || hint.maxAge < this.maxAge)) {
|
|
9
|
+
this.maxAge = hint.maxAge;
|
|
10
|
+
}
|
|
11
|
+
if (hint.scope !== undefined && this.scope !== CacheScope.Private) {
|
|
12
|
+
this.scope = hint.scope;
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
replace(hint) {
|
|
16
|
+
if (hint.maxAge !== undefined) {
|
|
17
|
+
this.maxAge = hint.maxAge;
|
|
18
|
+
}
|
|
19
|
+
if (hint.scope !== undefined) {
|
|
20
|
+
this.scope = hint.scope;
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
policyIfCacheable() {
|
|
24
|
+
var _a;
|
|
25
|
+
if (this.maxAge === undefined || this.maxAge === 0) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
return { maxAge: this.maxAge, scope: (_a = this.scope) !== null && _a !== void 0 ? _a : CacheScope.Public };
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/apollo-federation",
|
|
3
|
-
"version": "3.0.0-alpha-
|
|
3
|
+
"version": "3.0.0-alpha-5b6bfa8.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"@apollo/gateway": "^0.41.0 || ^0.42.0 || ^0.43.0",
|
|
7
|
-
"@envelop/core": "
|
|
8
|
-
"graphql": "
|
|
7
|
+
"@envelop/core": "3.0.0-alpha-5b6bfa8.0",
|
|
8
|
+
"@graphql-tools/graphql": "0.1.0-alpha-33fb2950.0"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"apollo-server-caching": "^3.1.0",
|
|
@@ -13,26 +13,47 @@
|
|
|
13
13
|
},
|
|
14
14
|
"repository": {
|
|
15
15
|
"type": "git",
|
|
16
|
-
"url": "https://github.com/
|
|
16
|
+
"url": "https://github.com/n1ru4l/envelop.git",
|
|
17
17
|
"directory": "packages/plugins/apollo-federation"
|
|
18
18
|
},
|
|
19
19
|
"author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
|
|
20
20
|
"license": "MIT",
|
|
21
|
-
"main": "index.js",
|
|
22
|
-
"module": "index.
|
|
23
|
-
"typings": "index.d.ts",
|
|
21
|
+
"main": "cjs/index.js",
|
|
22
|
+
"module": "esm/index.js",
|
|
23
|
+
"typings": "typings/index.d.ts",
|
|
24
24
|
"typescript": {
|
|
25
|
-
"definition": "index.d.ts"
|
|
25
|
+
"definition": "typings/index.d.ts"
|
|
26
26
|
},
|
|
27
|
+
"type": "module",
|
|
27
28
|
"exports": {
|
|
28
29
|
".": {
|
|
29
|
-
"require":
|
|
30
|
-
|
|
30
|
+
"require": {
|
|
31
|
+
"types": "./typings/index.d.ts",
|
|
32
|
+
"default": "./cjs/index.js"
|
|
33
|
+
},
|
|
34
|
+
"import": {
|
|
35
|
+
"types": "./typings/index.d.ts",
|
|
36
|
+
"default": "./esm/index.js"
|
|
37
|
+
},
|
|
38
|
+
"default": {
|
|
39
|
+
"types": "./typings/index.d.ts",
|
|
40
|
+
"default": "./esm/index.js"
|
|
41
|
+
}
|
|
31
42
|
},
|
|
32
43
|
"./*": {
|
|
33
|
-
"require":
|
|
34
|
-
|
|
44
|
+
"require": {
|
|
45
|
+
"types": "./typings/*.d.ts",
|
|
46
|
+
"default": "./cjs/*.js"
|
|
47
|
+
},
|
|
48
|
+
"import": {
|
|
49
|
+
"types": "./typings/*.d.ts",
|
|
50
|
+
"default": "./esm/*.js"
|
|
51
|
+
},
|
|
52
|
+
"default": {
|
|
53
|
+
"types": "./typings/*.d.ts",
|
|
54
|
+
"default": "./esm/*.js"
|
|
55
|
+
}
|
|
35
56
|
},
|
|
36
57
|
"./package.json": "./package.json"
|
|
37
58
|
}
|
|
38
|
-
}
|
|
59
|
+
}
|
|
File without changes
|
|
File without changes
|