validate-documents.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.createDefaultRules = exports.checkValidationErrors = exports.validateGraphQlDocuments = void 0;
  4. const graphql_1 = require("graphql");
  5. const AggregateError_js_1 = require("./AggregateError.js");
  6. async function validateGraphQlDocuments(schema, documentFiles, effectiveRules = createDefaultRules()) {
  7. const allFragmentMap = new Map();
  8. const documentFileObjectsToValidate = [];
  9. for (const documentFile of documentFiles) {
  10. if (documentFile.document) {
  11. const definitionsToValidate = [];
  12. for (const definitionNode of documentFile.document.definitions) {
  13. if (definitionNode.kind === graphql_1.Kind.FRAGMENT_DEFINITION) {
  14. allFragmentMap.set(definitionNode.name.value, definitionNode);
  15. }
  16. else {
  17. definitionsToValidate.push(definitionNode);
  18. }
  19. }
  20. documentFileObjectsToValidate.push({
  21. location: documentFile.location,
  22. document: {
  23. kind: graphql_1.Kind.DOCUMENT,
  24. definitions: definitionsToValidate,
  25. },
  26. });
  27. }
  28. }
  29. const allErrors = [];
  30. const allFragmentsDocument = {
  31. kind: graphql_1.Kind.DOCUMENT,
  32. definitions: [...allFragmentMap.values()],
  33. };
  34. await Promise.all(documentFileObjectsToValidate.map(async (documentFile) => {
  35. const documentToValidate = (0, graphql_1.concatAST)([allFragmentsDocument, documentFile.document]);
  36. const errors = (0, graphql_1.validate)(schema, documentToValidate, effectiveRules);
  37. if (errors.length > 0) {
  38. allErrors.push({
  39. filePath: documentFile.location,
  40. errors,
  41. });
  42. }
  43. }));
  44. return allErrors;
  45. }
  46. exports.validateGraphQlDocuments = validateGraphQlDocuments;
  47. function checkValidationErrors(loadDocumentErrors) {
  48. if (loadDocumentErrors.length > 0) {
  49. const errors = [];
  50. for (const loadDocumentError of loadDocumentErrors) {
  51. for (const graphQLError of loadDocumentError.errors) {
  52. const error = new Error();
  53. error.name = 'GraphQLDocumentError';
  54. error.message = `${error.name}: ${graphQLError.message}`;
  55. error.stack = error.message;
  56. if (graphQLError.locations) {
  57. for (const location of graphQLError.locations) {
  58. error.stack += `\n at ${loadDocumentError.filePath}:${location.line}:${location.column}`;
  59. }
  60. }
  61. errors.push(error);
  62. }
  63. }
  64. throw new AggregateError_js_1.AggregateError(errors, `GraphQL Document Validation failed with ${errors.length} errors;
  65. ${errors.map((error, index) => `Error ${index}: ${error.stack}`).join('\n\n')}`);
  66. }
  67. }
  68. exports.checkValidationErrors = checkValidationErrors;
  69. function createDefaultRules() {
  70. let ignored = ['NoUnusedFragmentsRule', 'NoUnusedVariablesRule', 'KnownDirectivesRule'];
  71. if (graphql_1.versionInfo.major < 15) {
  72. ignored = ignored.map(rule => rule.replace(/Rule$/, ''));
  73. }
  74. return graphql_1.specifiedRules.filter((f) => !ignored.includes(f.name));
  75. }
  76. exports.createDefaultRules = createDefaultRules;