UNPKG

4.27 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getDirectiveExtensions = void 0;
4const graphql_1 = require("graphql");
5const getArgumentValues_js_1 = require("./getArgumentValues.js");
6const memoize_js_1 = require("./memoize.js");
7function getDirectiveExtensions(directableObj, schema, pathToDirectivesInExtensions = ['directives']) {
8 const directiveExtensions = {};
9 if (directableObj.extensions) {
10 let directivesInExtensions = directableObj.extensions;
11 for (const pathSegment of pathToDirectivesInExtensions) {
12 directivesInExtensions = directivesInExtensions?.[pathSegment];
13 }
14 if (directivesInExtensions != null) {
15 for (const directiveNameProp in directivesInExtensions) {
16 const directiveObjs = directivesInExtensions[directiveNameProp];
17 const directiveName = directiveNameProp;
18 if (Array.isArray(directiveObjs)) {
19 for (const directiveObj of directiveObjs) {
20 let existingDirectiveExtensions = directiveExtensions[directiveName];
21 if (!existingDirectiveExtensions) {
22 existingDirectiveExtensions = [];
23 directiveExtensions[directiveName] = existingDirectiveExtensions;
24 }
25 existingDirectiveExtensions.push(directiveObj);
26 }
27 }
28 else {
29 let existingDirectiveExtensions = directiveExtensions[directiveName];
30 if (!existingDirectiveExtensions) {
31 existingDirectiveExtensions = [];
32 directiveExtensions[directiveName] = existingDirectiveExtensions;
33 }
34 existingDirectiveExtensions.push(directiveObjs);
35 }
36 }
37 }
38 }
39 const memoizedStringify = (0, memoize_js_1.memoize1)(obj => JSON.stringify(obj));
40 const astNodes = [];
41 if (directableObj.astNode) {
42 astNodes.push(directableObj.astNode);
43 }
44 if (directableObj.extensionASTNodes) {
45 astNodes.push(...directableObj.extensionASTNodes);
46 }
47 for (const astNode of astNodes) {
48 if (astNode.directives?.length) {
49 for (const directive of astNode.directives) {
50 const directiveName = directive.name.value;
51 let existingDirectiveExtensions = directiveExtensions[directiveName];
52 if (!existingDirectiveExtensions) {
53 existingDirectiveExtensions = [];
54 directiveExtensions[directiveName] = existingDirectiveExtensions;
55 }
56 const directiveInSchema = schema?.getDirective(directiveName);
57 let value = {};
58 if (directiveInSchema) {
59 value = (0, getArgumentValues_js_1.getArgumentValues)(directiveInSchema, directive);
60 }
61 if (directive.arguments) {
62 for (const argNode of directive.arguments) {
63 const argName = argNode.name.value;
64 if (value[argName] == null) {
65 const argInDirective = directiveInSchema?.args.find(arg => arg.name === argName);
66 if (argInDirective) {
67 value[argName] = (0, graphql_1.valueFromAST)(argNode.value, argInDirective.type);
68 }
69 }
70 if (value[argName] == null) {
71 value[argName] = (0, graphql_1.valueFromASTUntyped)(argNode.value);
72 }
73 }
74 }
75 if (astNodes.length > 0 && existingDirectiveExtensions.length > 0) {
76 const valStr = memoizedStringify(value);
77 if (existingDirectiveExtensions.some(val => memoizedStringify(val) === valStr)) {
78 continue;
79 }
80 }
81 existingDirectiveExtensions.push(value);
82 }
83 }
84 }
85 return directiveExtensions;
86}
87exports.getDirectiveExtensions = getDirectiveExtensions;