Skip to content

Instantly share code, notes, and snippets.

@gtkatakura
Forked from sciyoshi/esbuild-relay.js
Created February 24, 2021 14:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Esbuild plugin for compiling relay queries
import { promises } from "fs";
import crypto from "crypto";
import path from "path";
import { print, parse } from "graphql";
const plugin = {
name: "relay",
setup: build => {
build.onLoad({ filter: /\.tsx$/, namespace: "" }, async args => {
let contents = await promises.readFile(args.path, "utf8");
if (contents.includes("graphql`")) {
let imports = [];
contents = contents.replaceAll(/graphql`([\s\S]*?)`/gm, (match, query) => {
const formatted = print(parse(query));
const name = /(fragment|mutation|query) (\w+)/.exec(formatted)[2];
// const hash = crypto.createHash("md5").update(formatted, "utf8").digest("hex");
let id = `graphql__${crypto.randomBytes(10).toString("hex")}`;
imports.push(`import ${id} from "./__generated__/${name}.graphql.ts";`);
return id;
});
contents = imports.join("\n") + contents;
}
return {
contents: contents,
loader: "tsx",
};
});
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment