Skip to content

Commit

Permalink
feat(bruno): add support for setting http headers
Browse files Browse the repository at this point in the history
  • Loading branch information
mvantellingen committed Dec 19, 2024
1 parent b61c681 commit f48334b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-years-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/graphql-codegen-bruno": minor
---

Add support for setting http headers
2 changes: 1 addition & 1 deletion packages/bruno/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"typescript": "^5.7.2"
},
"peerDependencies": {
"graphql": "^16.9.0"
"graphql": ">= 16"
},
"pnpm": {
"overrides": {
Expand Down
4 changes: 1 addition & 3 deletions packages/bruno/src/__output__/queries/GetCustomer.bru
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

meta {
name: GetCustomer
type: graphql
Expand Down Expand Up @@ -53,5 +52,4 @@ body:graphql:vars {
"authMethod": "EMAIL"
}
}
}

}
72 changes: 53 additions & 19 deletions packages/bruno/src/bruno.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
import prettier from "prettier";
import type { FileContent } from "./operations";

export interface BrunoPluginConfig {
defaults: Record<string, unknown>;
headers: Record<string, string>;
clean: boolean;
}

export const asBruno = async (
operation: FileContent,
defaults: Record<string, unknown>,
config: BrunoPluginConfig,
) => {
const formattedContent = await prettier.format(operation.content, {
parser: "graphql",
});

const vars = mergeDefaults(operation.vars, defaults);
const vars = mergeDefaults(operation.vars, config.defaults);

return `
meta {
name: ${operation.name}
type: graphql
}
const file = new FileCreator();
file.addElement("meta", [`name: ${operation.name}`, "type: graphql"]);
file.addElement("post", [
"url: {{graphql-gateway}}/graphql",
"body: graphql",
"auth: none",
]);
file.addElement(
"headers",
Object.entries(config.headers ?? {}).map(
([key, value]) => `${key}: "${value}"`,
),
);

post {
url: {{graphql-gateway}}/graphql
body: graphql
auth: none
}
file.addElement("body:graphql", formattedContent.split("\n"));

body:graphql {
${formattedContent.split("\n").join("\n ")}
}
file.addElement(
"body:graphql:vars",
JSON.stringify(vars, null, 2).split("\n"),
);

body:graphql:vars {
${JSON.stringify(vars, null, 2).split("\n").join("\n ")}
}
`;
return file.toString();
};

const mergeDefaults = (
Expand All @@ -51,3 +59,29 @@ const mergeDefaults = (

return mergedVars;
};

class FileCreator {
elements: string[];

constructor() {
this.elements = [];
}

addElement(name: string, lines: string[]) {
if (lines.length === 0) {
return;
}
const snippet: string[] = [];
snippet.push(`${name} {`);

for (const line of lines) {
snippet.push(` ${line}`);
}
snippet.push("}");
this.elements.push(snippet.join("\n"));
}

toString() {
return this.elements.join("\n\n");
}
}
9 changes: 2 additions & 7 deletions packages/bruno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ import path from "node:path";
import type { PluginFunction, Types } from "@graphql-codegen/plugin-helpers";
import fs from "fs-extra";
import type { GraphQLSchema } from "graphql";
import { asBruno } from "./bruno";
import { type BrunoPluginConfig, asBruno } from "./bruno";
import { extractOperations } from "./operations";

export interface BrunoPluginConfig {
defaults: Record<string, unknown>;
clean: boolean;
}

export const plugin: PluginFunction<BrunoPluginConfig> = async (
schema: GraphQLSchema,
documents: Types.DocumentFile[],
Expand All @@ -31,7 +26,7 @@ export const plugin: PluginFunction<BrunoPluginConfig> = async (
const fileName = `${operation.name}.bru`;
const outputPath = path.join(outputDir, subpath, fileName);

const formattedContent = await asBruno(operation, config.defaults);
const formattedContent = await asBruno(operation, config);

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

0 comments on commit f48334b

Please sign in to comment.