get-directives.js 4.4 KB

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