@envelop/apollo-federation 0.2.1 → 0.2.3-alpha-33ac2d1.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.
Files changed (3) hide show
  1. package/index.js +24 -19
  2. package/index.mjs +25 -20
  3. package/package.json +4 -3
package/index.js CHANGED
@@ -43,14 +43,17 @@ const useApolloFederation = (options) => {
43
43
  const documentSourceMap = new WeakMap();
44
44
  return {
45
45
  onPluginInit({ setSchema }) {
46
- if (!gateway.schema) {
47
- throw new Error(`ApolloGateway doesn't have the schema loaded. Please make sure ApolloGateway is loaded with .load() method.`);
46
+ if (gateway.schema) {
47
+ setSchema(gateway.schema);
48
48
  }
49
- setSchema(gateway.schema);
50
- gateway.onSchemaLoadOrUpdate(({ apiSchema }) => setSchema(apiSchema));
51
- },
52
- onSchemaChange({ schema }) {
53
- schemaHash = graphql.printSchema(schema);
49
+ else {
50
+ 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
+ gateway.load();
52
+ }
53
+ gateway.onSchemaLoadOrUpdate(({ apiSchema, coreSupergraphSdl = graphql.printSchema(apiSchema) }) => {
54
+ setSchema(apiSchema);
55
+ schemaHash = (coreSupergraphSdl || graphql.printSchema(apiSchema));
56
+ });
54
57
  },
55
58
  onParse({ params: { source } }) {
56
59
  const key = source instanceof graphql.Source ? source.body : source;
@@ -61,25 +64,27 @@ const useApolloFederation = (options) => {
61
64
  };
62
65
  },
63
66
  onExecute({ args, setExecuteFn }) {
67
+ var _a;
68
+ let documentStr = documentSourceMap.get(args.document);
69
+ if (documentStr == null) {
70
+ documentStr = graphql.print(args.document);
71
+ documentSourceMap.set(args.document, documentStr);
72
+ }
73
+ const operation = graphql.getOperationAST(args.document, (_a = args.operationName) !== null && _a !== void 0 ? _a : undefined);
74
+ if (!operation) {
75
+ throw new Error(`Operation ${args.operationName || ''} cannot be found in ${documentStr}`);
76
+ }
64
77
  setExecuteFn(function federationExecutor() {
65
- var _a, _b, _c, _d;
66
- const documentStr = documentSourceMap.get(args.document);
67
- if (!documentStr) {
68
- throw new Error(`Parse error!`);
69
- }
70
- const operation = graphql.getOperationAST(args.document, (_a = args.operationName) !== null && _a !== void 0 ? _a : undefined);
71
- if (!operation) {
72
- throw new Error(`Operation ${args.operationName || ''} cannot be found in ${documentStr}`);
73
- }
78
+ var _a, _b, _c;
74
79
  return gateway.executor({
75
80
  document: args.document,
76
81
  request: {
77
82
  query: documentStr,
78
- operationName: (_b = args.operationName) !== null && _b !== void 0 ? _b : undefined,
79
- variables: (_c = args.variableValues) !== null && _c !== void 0 ? _c : undefined,
83
+ operationName: (_a = args.operationName) !== null && _a !== void 0 ? _a : undefined,
84
+ variables: (_b = args.variableValues) !== null && _b !== void 0 ? _b : undefined,
80
85
  },
81
86
  overallCachePolicy,
82
- operationName: (_d = args.operationName) !== null && _d !== void 0 ? _d : null,
87
+ operationName: (_c = args.operationName) !== null && _c !== void 0 ? _c : null,
83
88
  cache,
84
89
  context: args.contextValue,
85
90
  queryHash: documentStr,
package/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { printSchema, Source, getOperationAST } from 'graphql';
1
+ import { printSchema, Source, print, getOperationAST } from 'graphql';
2
2
  import { InMemoryLRUCache } from 'apollo-server-caching';
3
3
  import { CacheScope } from 'apollo-server-types';
4
4
 
@@ -39,14 +39,17 @@ const useApolloFederation = (options) => {
39
39
  const documentSourceMap = new WeakMap();
40
40
  return {
41
41
  onPluginInit({ setSchema }) {
42
- if (!gateway.schema) {
43
- throw new Error(`ApolloGateway doesn't have the schema loaded. Please make sure ApolloGateway is loaded with .load() method.`);
42
+ if (gateway.schema) {
43
+ setSchema(gateway.schema);
44
44
  }
45
- setSchema(gateway.schema);
46
- gateway.onSchemaLoadOrUpdate(({ apiSchema }) => setSchema(apiSchema));
47
- },
48
- onSchemaChange({ schema }) {
49
- schemaHash = printSchema(schema);
45
+ else {
46
+ 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.`);
47
+ gateway.load();
48
+ }
49
+ gateway.onSchemaLoadOrUpdate(({ apiSchema, coreSupergraphSdl = printSchema(apiSchema) }) => {
50
+ setSchema(apiSchema);
51
+ schemaHash = (coreSupergraphSdl || printSchema(apiSchema));
52
+ });
50
53
  },
51
54
  onParse({ params: { source } }) {
52
55
  const key = source instanceof Source ? source.body : source;
@@ -57,25 +60,27 @@ const useApolloFederation = (options) => {
57
60
  };
58
61
  },
59
62
  onExecute({ args, setExecuteFn }) {
63
+ var _a;
64
+ let documentStr = documentSourceMap.get(args.document);
65
+ if (documentStr == null) {
66
+ documentStr = print(args.document);
67
+ documentSourceMap.set(args.document, documentStr);
68
+ }
69
+ const operation = getOperationAST(args.document, (_a = args.operationName) !== null && _a !== void 0 ? _a : undefined);
70
+ if (!operation) {
71
+ throw new Error(`Operation ${args.operationName || ''} cannot be found in ${documentStr}`);
72
+ }
60
73
  setExecuteFn(function federationExecutor() {
61
- var _a, _b, _c, _d;
62
- const documentStr = documentSourceMap.get(args.document);
63
- if (!documentStr) {
64
- throw new Error(`Parse error!`);
65
- }
66
- const operation = getOperationAST(args.document, (_a = args.operationName) !== null && _a !== void 0 ? _a : undefined);
67
- if (!operation) {
68
- throw new Error(`Operation ${args.operationName || ''} cannot be found in ${documentStr}`);
69
- }
74
+ var _a, _b, _c;
70
75
  return gateway.executor({
71
76
  document: args.document,
72
77
  request: {
73
78
  query: documentStr,
74
- operationName: (_b = args.operationName) !== null && _b !== void 0 ? _b : undefined,
75
- variables: (_c = args.variableValues) !== null && _c !== void 0 ? _c : undefined,
79
+ operationName: (_a = args.operationName) !== null && _a !== void 0 ? _a : undefined,
80
+ variables: (_b = args.variableValues) !== null && _b !== void 0 ? _b : undefined,
76
81
  },
77
82
  overallCachePolicy,
78
- operationName: (_d = args.operationName) !== null && _d !== void 0 ? _d : null,
83
+ operationName: (_c = args.operationName) !== null && _c !== void 0 ? _c : null,
79
84
  cache,
80
85
  context: args.contextValue,
81
86
  queryHash: documentStr,
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@envelop/apollo-federation",
3
- "version": "0.2.1",
3
+ "version": "0.2.3-alpha-33ac2d1.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "@apollo/gateway": "^0.41.0 || ^0.42.0 || ^0.43.0",
7
- "@envelop/core": "^1.6.1",
7
+ "@envelop/core": "1.6.6-alpha-33ac2d1.0",
8
8
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
9
9
  },
10
10
  "dependencies": {
@@ -32,6 +32,7 @@
32
32
  "./*": {
33
33
  "require": "./*.js",
34
34
  "import": "./*.mjs"
35
- }
35
+ },
36
+ "./package.json": "./package.json"
36
37
  }
37
38
  }