Skip to content

Commit 40ea262

Browse files
committed
format GraphQL query
1 parent b30ec28 commit 40ea262

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

plugins/wpgraphql-logging/assets/js/view/wp-graphql-logging-view.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,127 @@ jQuery(document).ready(function($) {
44
timeFormat: "HH:mm:ss"
55
});
66
});
7+
8+
document.addEventListener("DOMContentLoaded", function () {
9+
function formatGraphQLQuery(query, indentSize = 2) {
10+
if (!query || typeof query !== "string") return "";
11+
12+
try {
13+
let formatted = query.trim().replace(/\s+/g, " ");
14+
let indentLevel = 0,
15+
result = "",
16+
inString = false,
17+
stringChar = null,
18+
afterClosingParen = false;
19+
const operationTypes = ["query", "mutation", "subscription", "fragment"];
20+
const indent = () => "\n" + " ".repeat(indentLevel * indentSize);
21+
22+
for (let i = 0; i < formatted.length; i++) {
23+
const char = formatted[i],
24+
prev = formatted[i - 1],
25+
next = formatted[i + 1];
26+
27+
// Track string literals
28+
if ((char === '"' || char === "'") && prev !== "\\") {
29+
if (!inString) {
30+
inString = true;
31+
stringChar = char;
32+
} else if (char === stringChar) {
33+
inString = false;
34+
stringChar = null;
35+
}
36+
}
37+
38+
if (inString) {
39+
result += char;
40+
continue;
41+
}
42+
43+
// Handle braces and parentheses
44+
if (char === "{" || char === "(") {
45+
result = result.trimEnd() + (char === "{" ? " {" : "(");
46+
if (next !== (char === "{" ? "}" : ")")) {
47+
indentLevel++;
48+
result += indent();
49+
}
50+
afterClosingParen = false;
51+
} else if (char === "}" || char === ")") {
52+
if (prev !== (char === "}" ? "{" : "(")) {
53+
indentLevel--;
54+
result = result.trimEnd() + indent();
55+
}
56+
result += char;
57+
afterClosingParen = char === ")";
58+
} else if (char === ",") {
59+
result += "," + indent();
60+
afterClosingParen = false;
61+
} else if (char === " ") {
62+
const trimmed = result.trimEnd();
63+
const lastWord =
64+
trimmed
65+
.split(/[\s\n{}(),]/)
66+
.filter((w) => w)
67+
.pop() || "";
68+
const lastChar = trimmed.slice(-1);
69+
70+
if (
71+
(afterClosingParen && next === "@") ||
72+
trimmed.endsWith("...") ||
73+
lastWord === "on" ||
74+
operationTypes.includes(lastWord) ||
75+
lastChar === ":" ||
76+
lastChar === "@" ||
77+
trimmed.match(/@\w+$/)
78+
) {
79+
result += char;
80+
} else {
81+
const isBetweenFields =
82+
lastChar &&
83+
/[a-zA-Z0-9_]/.test(lastChar) &&
84+
next &&
85+
/[a-zA-Z_]/.test(next) &&
86+
!["{", "}", "(", ")", ",", ":", "\n", "@", "."].includes(next);
87+
88+
if (isBetweenFields) {
89+
result = result.trimEnd() + indent();
90+
afterClosingParen = false;
91+
} else if (!["\n", " "].includes(result.slice(-1))) {
92+
result += char;
93+
}
94+
}
95+
} else {
96+
if (
97+
afterClosingParen &&
98+
char !== "@" &&
99+
char !== "{" &&
100+
char !== "}"
101+
) {
102+
result = result.trimEnd() + " ";
103+
}
104+
result += char;
105+
afterClosingParen = false;
106+
}
107+
}
108+
109+
return result
110+
.split("\n")
111+
.map((line) => line.trimEnd())
112+
.join("\n");
113+
} catch (error) {
114+
console.error("GraphQL formatting error:", error);
115+
return query;
116+
}
117+
}
118+
119+
const queryElements = document.querySelectorAll(
120+
"pre.wpgraphql-logging-query"
121+
);
122+
123+
if (queryElements.length > 0) {
124+
queryElements.forEach(function (element) {
125+
const rawQuery = element.textContent;
126+
const formattedQuery = formatGraphQLQuery(rawQuery);
127+
element.textContent = formattedQuery;
128+
});
129+
}
130+
});

0 commit comments

Comments
 (0)