get-directives.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getDirective = exports.getDirectives = exports.getDirectiveInExtensions = exports.getDirectivesInExtensions = void 0;
  4. const getArgumentValues_js_1 = require("./getArgumentValues.js");
  5. function getDirectivesInExtensions(node, pathToDirectivesInExtensions = ['directives']) {
  6. return pathToDirectivesInExtensions.reduce((acc, pathSegment) => (acc == null ? acc : acc[pathSegment]), node === null || node === void 0 ? void 0 : node.extensions);
  7. }
  8. exports.getDirectivesInExtensions = getDirectivesInExtensions;
  9. function _getDirectiveInExtensions(directivesInExtensions, directiveName) {
  10. const directiveInExtensions = directivesInExtensions.filter(directiveAnnotation => directiveAnnotation.name === directiveName);
  11. if (!directiveInExtensions.length) {
  12. return undefined;
  13. }
  14. return directiveInExtensions.map(directive => { var _a; return (_a = directive.args) !== null && _a !== void 0 ? _a : {}; });
  15. }
  16. function getDirectiveInExtensions(node, directiveName, pathToDirectivesInExtensions = ['directives']) {
  17. const directivesInExtensions = pathToDirectivesInExtensions.reduce((acc, pathSegment) => (acc == null ? acc : acc[pathSegment]), node === null || node === void 0 ? void 0 : node.extensions);
  18. if (directivesInExtensions === undefined) {
  19. return undefined;
  20. }
  21. if (Array.isArray(directivesInExtensions)) {
  22. return _getDirectiveInExtensions(directivesInExtensions, directiveName);
  23. }
  24. // Support condensed format by converting to longer format
  25. // The condensed format does not preserve ordering of directives when repeatable directives are used.
  26. // See https://github.com/ardatan/graphql-tools/issues/2534
  27. const reformattedDirectivesInExtensions = [];
  28. for (const [name, argsOrArrayOfArgs] of Object.entries(directivesInExtensions)) {
  29. if (Array.isArray(argsOrArrayOfArgs)) {
  30. for (const args of argsOrArrayOfArgs) {
  31. reformattedDirectivesInExtensions.push({ name, args });
  32. }
  33. }
  34. else {
  35. reformattedDirectivesInExtensions.push({ name, args: argsOrArrayOfArgs });
  36. }
  37. }
  38. return _getDirectiveInExtensions(reformattedDirectivesInExtensions, directiveName);
  39. }
  40. exports.getDirectiveInExtensions = getDirectiveInExtensions;
  41. function getDirectives(schema, node, pathToDirectivesInExtensions = ['directives']) {
  42. const directivesInExtensions = getDirectivesInExtensions(node, pathToDirectivesInExtensions);
  43. if (directivesInExtensions != null && directivesInExtensions.length > 0) {
  44. return directivesInExtensions;
  45. }
  46. const schemaDirectives = schema && schema.getDirectives ? schema.getDirectives() : [];
  47. const schemaDirectiveMap = schemaDirectives.reduce((schemaDirectiveMap, schemaDirective) => {
  48. schemaDirectiveMap[schemaDirective.name] = schemaDirective;
  49. return schemaDirectiveMap;
  50. }, {});
  51. let astNodes = [];
  52. if (node.astNode) {
  53. astNodes.push(node.astNode);
  54. }
  55. if ('extensionASTNodes' in node && node.extensionASTNodes) {
  56. astNodes = [...astNodes, ...node.extensionASTNodes];
  57. }
  58. const result = [];
  59. for (const astNode of astNodes) {
  60. if (astNode.directives) {
  61. for (const directiveNode of astNode.directives) {
  62. const schemaDirective = schemaDirectiveMap[directiveNode.name.value];
  63. if (schemaDirective) {
  64. result.push({ name: directiveNode.name.value, args: (0, getArgumentValues_js_1.getArgumentValues)(schemaDirective, directiveNode) });
  65. }
  66. }
  67. }
  68. }
  69. return result;
  70. }
  71. exports.getDirectives = getDirectives;
  72. function getDirective(schema, node, directiveName, pathToDirectivesInExtensions = ['directives']) {
  73. const directiveInExtensions = getDirectiveInExtensions(node, directiveName, pathToDirectivesInExtensions);
  74. if (directiveInExtensions != null) {
  75. return directiveInExtensions;
  76. }
  77. const schemaDirective = schema && schema.getDirective ? schema.getDirective(directiveName) : undefined;
  78. if (schemaDirective == null) {
  79. return undefined;
  80. }
  81. let astNodes = [];
  82. if (node.astNode) {
  83. astNodes.push(node.astNode);
  84. }
  85. if ('extensionASTNodes' in node && node.extensionASTNodes) {
  86. astNodes = [...astNodes, ...node.extensionASTNodes];
  87. }
  88. const result = [];
  89. for (const astNode of astNodes) {
  90. if (astNode.directives) {
  91. for (const directiveNode of astNode.directives) {
  92. if (directiveNode.name.value === directiveName) {
  93. result.push((0, getArgumentValues_js_1.getArgumentValues)(schemaDirective, directiveNode));
  94. }
  95. }
  96. }
  97. }
  98. if (!result.length) {
  99. return undefined;
  100. }
  101. return result;
  102. }
  103. exports.getDirective = getDirective;