aboutsummaryrefslogtreecommitdiff
path: root/source/library/graphql/operations.ts
diff options
context:
space:
mode:
Diffstat (limited to 'source/library/graphql/operations.ts')
-rw-r--r--source/library/graphql/operations.ts56
1 files changed, 56 insertions, 0 deletions
diff --git a/source/library/graphql/operations.ts b/source/library/graphql/operations.ts
new file mode 100644
index 0000000..b34aeee
--- /dev/null
+++ b/source/library/graphql/operations.ts
@@ -0,0 +1,56 @@
+
+
+
+/*** IMPORT ------------------------------------------- ***/
+
+import { parse } from "graphql";
+
+/*** EXPORT ------------------------------------------- ***/
+
+export type OperationInfo = {
+ name: string | null;
+ type: "mutation" | "query" | "subscription";
+};
+
+export function deriveTitle(query: string, ops: OperationInfo[]): string {
+ const first = ops[0];
+
+ if (first && first.name)
+ return first.name;
+
+ if (first)
+ return first.type;
+
+ const trimmed = query.trim();
+
+ if (!trimmed)
+ return "untitled";
+
+ return trimmed.slice(0, 20);
+}
+
+export function parseOperations(query: string): OperationInfo[] {
+ const trimmed = query.trim();
+
+ if (!trimmed)
+ return [];
+
+ try {
+ const doc = parse(trimmed);
+ const ops: OperationInfo[] = [];
+
+ for (const def of doc.definitions) {
+ if (def.kind !== "OperationDefinition")
+ continue;
+
+ ops.push({
+ name: def.name?.value ?? null,
+ type: def.operation
+ });
+ }
+
+ return ops;
+ } catch {
+ return [];
+ }
+}