Skip to content

Commit f48334b

Browse files
committed
feat(bruno): add support for setting http headers
1 parent b61c681 commit f48334b

File tree

5 files changed

+62
-30
lines changed

5 files changed

+62
-30
lines changed

.changeset/twenty-years-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@labdigital/graphql-codegen-bruno": minor
3+
---
4+
5+
Add support for setting http headers

packages/bruno/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"typescript": "^5.7.2"
4343
},
4444
"peerDependencies": {
45-
"graphql": "^16.9.0"
45+
"graphql": ">= 16"
4646
},
4747
"pnpm": {
4848
"overrides": {

packages/bruno/src/__output__/queries/GetCustomer.bru

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
meta {
32
name: GetCustomer
43
type: graphql
@@ -53,5 +52,4 @@ body:graphql:vars {
5352
"authMethod": "EMAIL"
5453
}
5554
}
56-
}
57-
55+
}

packages/bruno/src/bruno.ts

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
11
import prettier from "prettier";
22
import type { FileContent } from "./operations";
33

4+
export interface BrunoPluginConfig {
5+
defaults: Record<string, unknown>;
6+
headers: Record<string, string>;
7+
clean: boolean;
8+
}
9+
410
export const asBruno = async (
511
operation: FileContent,
6-
defaults: Record<string, unknown>,
12+
config: BrunoPluginConfig,
713
) => {
814
const formattedContent = await prettier.format(operation.content, {
915
parser: "graphql",
1016
});
1117

12-
const vars = mergeDefaults(operation.vars, defaults);
18+
const vars = mergeDefaults(operation.vars, config.defaults);
1319

14-
return `
15-
meta {
16-
name: ${operation.name}
17-
type: graphql
18-
}
20+
const file = new FileCreator();
21+
file.addElement("meta", [`name: ${operation.name}`, "type: graphql"]);
22+
file.addElement("post", [
23+
"url: {{graphql-gateway}}/graphql",
24+
"body: graphql",
25+
"auth: none",
26+
]);
27+
file.addElement(
28+
"headers",
29+
Object.entries(config.headers ?? {}).map(
30+
([key, value]) => `${key}: "${value}"`,
31+
),
32+
);
1933

20-
post {
21-
url: {{graphql-gateway}}/graphql
22-
body: graphql
23-
auth: none
24-
}
34+
file.addElement("body:graphql", formattedContent.split("\n"));
2535

26-
body:graphql {
27-
${formattedContent.split("\n").join("\n ")}
28-
}
36+
file.addElement(
37+
"body:graphql:vars",
38+
JSON.stringify(vars, null, 2).split("\n"),
39+
);
2940

30-
body:graphql:vars {
31-
${JSON.stringify(vars, null, 2).split("\n").join("\n ")}
32-
}
33-
`;
41+
return file.toString();
3442
};
3543

3644
const mergeDefaults = (
@@ -51,3 +59,29 @@ const mergeDefaults = (
5159

5260
return mergedVars;
5361
};
62+
63+
class FileCreator {
64+
elements: string[];
65+
66+
constructor() {
67+
this.elements = [];
68+
}
69+
70+
addElement(name: string, lines: string[]) {
71+
if (lines.length === 0) {
72+
return;
73+
}
74+
const snippet: string[] = [];
75+
snippet.push(`${name} {`);
76+
77+
for (const line of lines) {
78+
snippet.push(` ${line}`);
79+
}
80+
snippet.push("}");
81+
this.elements.push(snippet.join("\n"));
82+
}
83+
84+
toString() {
85+
return this.elements.join("\n\n");
86+
}
87+
}

packages/bruno/src/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ import path from "node:path";
22
import type { PluginFunction, Types } from "@graphql-codegen/plugin-helpers";
33
import fs from "fs-extra";
44
import type { GraphQLSchema } from "graphql";
5-
import { asBruno } from "./bruno";
5+
import { type BrunoPluginConfig, asBruno } from "./bruno";
66
import { extractOperations } from "./operations";
77

8-
export interface BrunoPluginConfig {
9-
defaults: Record<string, unknown>;
10-
clean: boolean;
11-
}
12-
138
export const plugin: PluginFunction<BrunoPluginConfig> = async (
149
schema: GraphQLSchema,
1510
documents: Types.DocumentFile[],
@@ -31,7 +26,7 @@ export const plugin: PluginFunction<BrunoPluginConfig> = async (
3126
const fileName = `${operation.name}.bru`;
3227
const outputPath = path.join(outputDir, subpath, fileName);
3328

34-
const formattedContent = await asBruno(operation, config.defaults);
29+
const formattedContent = await asBruno(operation, config);
3530

3631
fs.outputFileSync(outputPath, formattedContent);
3732
result[operation.name] = {

0 commit comments

Comments
 (0)