determineApolloConfig.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.determineApolloConfig = void 0;
  7. const createSHA_1 = __importDefault(require("./utils/createSHA"));
  8. function determineApolloConfig(input, engine, logger) {
  9. var _a, _b;
  10. if (input && engine !== undefined) {
  11. throw Error('Cannot pass both `apollo` and `engine`');
  12. }
  13. const apolloConfig = {};
  14. const { APOLLO_KEY, APOLLO_GRAPH_REF, APOLLO_GRAPH_ID, APOLLO_GRAPH_VARIANT, ENGINE_API_KEY, ENGINE_SCHEMA_TAG, } = process.env;
  15. if (input === null || input === void 0 ? void 0 : input.key) {
  16. apolloConfig.key = input.key.trim();
  17. }
  18. else if (typeof engine === 'object' && engine.apiKey) {
  19. apolloConfig.key = engine.apiKey.trim();
  20. }
  21. else if (APOLLO_KEY) {
  22. if (ENGINE_API_KEY) {
  23. logger.warn('Using `APOLLO_KEY` since `ENGINE_API_KEY` (deprecated) is also set in the environment.');
  24. }
  25. apolloConfig.key = APOLLO_KEY.trim();
  26. }
  27. else if (ENGINE_API_KEY) {
  28. logger.warn('[deprecated] The `ENGINE_API_KEY` environment variable has been renamed to `APOLLO_KEY`.');
  29. apolloConfig.key = ENGINE_API_KEY.trim();
  30. }
  31. if (((_b = (_a = input === null || input === void 0 ? void 0 : input.key) !== null && _a !== void 0 ? _a : APOLLO_KEY) !== null && _b !== void 0 ? _b : ENGINE_API_KEY) !== apolloConfig.key) {
  32. logger.warn('The provided API key has unexpected leading or trailing whitespace. ' +
  33. 'Apollo Server will trim the key value before use.');
  34. }
  35. if (apolloConfig.key) {
  36. assertValidHeaderValue(apolloConfig.key);
  37. }
  38. if (apolloConfig.key) {
  39. apolloConfig.keyHash = createSHA_1.default('sha512')
  40. .update(apolloConfig.key)
  41. .digest('hex');
  42. }
  43. if (input === null || input === void 0 ? void 0 : input.graphRef) {
  44. apolloConfig.graphRef = input.graphRef;
  45. }
  46. else if (APOLLO_GRAPH_REF) {
  47. apolloConfig.graphRef = APOLLO_GRAPH_REF;
  48. }
  49. if (input === null || input === void 0 ? void 0 : input.graphId) {
  50. apolloConfig.graphId = input.graphId;
  51. }
  52. else if (APOLLO_GRAPH_ID) {
  53. apolloConfig.graphId = APOLLO_GRAPH_ID;
  54. }
  55. if (input === null || input === void 0 ? void 0 : input.graphVariant) {
  56. apolloConfig.graphVariant = input.graphVariant;
  57. }
  58. else if (typeof engine === 'object' && engine.graphVariant) {
  59. if (engine.schemaTag) {
  60. throw new Error('Cannot set more than one of apollo.graphVariant, ' +
  61. 'engine.graphVariant, and engine.schemaTag. Please use apollo.graphVariant.');
  62. }
  63. apolloConfig.graphVariant = engine.graphVariant;
  64. }
  65. else if (typeof engine === 'object' && engine.schemaTag) {
  66. logger.warn('[deprecated] The `engine.schemaTag` option has been renamed to `apollo.graphVariant` ' +
  67. '(or you may set it with the `APOLLO_GRAPH_VARIANT` environment variable).');
  68. apolloConfig.graphVariant = engine.schemaTag;
  69. }
  70. else if (APOLLO_GRAPH_VARIANT) {
  71. if (ENGINE_SCHEMA_TAG) {
  72. throw new Error('`APOLLO_GRAPH_VARIANT` and `ENGINE_SCHEMA_TAG` (deprecated) environment variables must not both be set.');
  73. }
  74. apolloConfig.graphVariant = APOLLO_GRAPH_VARIANT;
  75. }
  76. else if (ENGINE_SCHEMA_TAG) {
  77. logger.warn('[deprecated] The `ENGINE_SCHEMA_TAG` environment variable has been renamed to `APOLLO_GRAPH_VARIANT`.');
  78. apolloConfig.graphVariant = ENGINE_SCHEMA_TAG;
  79. }
  80. if (apolloConfig.graphRef) {
  81. if (apolloConfig.graphId) {
  82. throw new Error('Cannot specify both graph ref and graph ID. Please use ' +
  83. '`apollo.graphRef` or `APOLLO_GRAPH_REF` without also setting the graph ID.');
  84. }
  85. if (apolloConfig.graphVariant) {
  86. throw new Error('Cannot specify both graph ref and graph variant. Please use ' +
  87. '`apollo.graphRef` or `APOLLO_GRAPH_REF` without also setting the graph ID.');
  88. }
  89. const at = apolloConfig.graphRef.indexOf('@');
  90. if (at === -1) {
  91. apolloConfig.graphId = apolloConfig.graphRef;
  92. apolloConfig.graphVariant = 'current';
  93. }
  94. else {
  95. apolloConfig.graphId = apolloConfig.graphRef.substring(0, at);
  96. apolloConfig.graphVariant = apolloConfig.graphRef.substring(at + 1);
  97. }
  98. }
  99. else {
  100. if (!apolloConfig.graphId && apolloConfig.key) {
  101. const parts = apolloConfig.key.split(':', 2);
  102. if (parts[0] === 'service') {
  103. apolloConfig.graphId = parts[1];
  104. }
  105. else {
  106. throw Error('You have specified an API key in `apollo.key` or `APOLLO_KEY`, ' +
  107. 'but you have not specified your graph ref or graph ID and the key ' +
  108. 'does not start with `service:`. Please specify your graph ref; for ' +
  109. 'example, set `APOLLO_GRAPH_REF` to `my-graph-id@my-graph-variant`.');
  110. }
  111. }
  112. if (!apolloConfig.graphVariant) {
  113. apolloConfig.graphVariant = 'current';
  114. }
  115. if (apolloConfig.graphId) {
  116. apolloConfig.graphRef = `${apolloConfig.graphId}@${apolloConfig.graphVariant}`;
  117. }
  118. }
  119. return apolloConfig;
  120. }
  121. exports.determineApolloConfig = determineApolloConfig;
  122. function assertValidHeaderValue(value) {
  123. const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/g;
  124. if (invalidHeaderCharRegex.test(value)) {
  125. const invalidChars = value.match(invalidHeaderCharRegex);
  126. throw new Error(`The API key provided to Apollo Server contains characters which are invalid as HTTP header values. The following characters found in the key are invalid: ${invalidChars.join(', ')}. Valid header values may only contain ASCII visible characters. If you think there is an issue with your key, please contact Apollo support.`);
  127. }
  128. }
  129. //# sourceMappingURL=determineApolloConfig.js.map