schemaHash.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334
  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.generateSchemaHash = void 0;
  7. const language_1 = require("graphql/language");
  8. const execution_1 = require("graphql/execution");
  9. const utilities_1 = require("graphql/utilities");
  10. const fast_json_stable_stringify_1 = __importDefault(require("fast-json-stable-stringify"));
  11. const createSHA_1 = __importDefault(require("./createSHA"));
  12. function generateSchemaHash(schema) {
  13. const introspectionQuery = utilities_1.getIntrospectionQuery();
  14. const documentAST = language_1.parse(introspectionQuery);
  15. const result = execution_1.execute(schema, documentAST);
  16. if (result &&
  17. typeof result.then === 'function') {
  18. throw new Error([
  19. 'The introspection query is resolving asynchronously; execution of an introspection query is not expected to return a `Promise`.',
  20. '',
  21. 'Wrapped type resolvers should maintain the existing execution dynamics of the resolvers they wrap (i.e. async vs sync) or introspection types should be excluded from wrapping by checking them with `graphql/type`s, `isIntrospectionType` predicate function prior to wrapping.',
  22. ].join('\n'));
  23. }
  24. if (!result || !result.data || !result.data.__schema) {
  25. throw new Error('Unable to generate server introspection document.');
  26. }
  27. const introspectionSchema = result.data.__schema;
  28. const stringifiedSchema = fast_json_stable_stringify_1.default(introspectionSchema);
  29. return createSHA_1.default('sha512')
  30. .update(stringifiedSchema)
  31. .digest('hex');
  32. }
  33. exports.generateSchemaHash = generateSchemaHash;
  34. //# sourceMappingURL=schemaHash.js.map