Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Add type declerations for generator options #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions codegen/plugin.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Plugin } from "vite";
export default function levelupViteCodegen({ schema, out, gPath, debug, }: {
schema: any;
out: any;
gPath: any;
export interface GQueryGenerateOptions {
schema: string;
output: string;
gPath: string;
debug?: boolean;
}): Plugin;
}
export default function levelupViteCodegen({ schema, output, gPath, debug }: GQueryGenerateOptions): Plugin;
14 changes: 7 additions & 7 deletions codegen/plugin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion codegen/plugin.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

188 changes: 90 additions & 98 deletions codegen/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,113 +8,105 @@ const filterExt = /\.(graphqls?|gql)$/i;

// Delete all gq (generated) files
async function cleanGQ({ debug = false }) {
debug && console.log("🧹 removing all .gq files");
// Find and remove all .gq files
await execaCommand(
"find ./ -path '*.gq.ts' -type f -prune -print -exec rm -f '{}' +; ",
{
stdio: debug ? "inherit" : "ignore",
shell: true,
}
);
debug && console.log("🧹 removing all .gq files");
// Find and remove all .gq files
await execaCommand("find ./ -path '*.gq.ts' -type f -prune -print -exec rm -f '{}' +; ", {
stdio: debug ? "inherit" : "ignore",
shell: true,
});
}

export interface GQueryGenerateOptions {
schema: string;
output: string;
gPath: string;
debug?: boolean;
}

// Runs graphql codegen
async function gQueryGenerate({ schema, out, gPath, debug = false }) {
debug && console.log("🤖 starting codegen");
async function gQueryGenerate({ schema, output, gPath, debug = false }: GQueryGenerateOptions) {
debug && console.log("🤖 starting codegen");

// the actual codegen process.
await generate(
{
schema,
documents: "./src/**/*.graphql",
generates: {
// * Generates the types for your schema
[`${process.cwd()}/${out}/types.gq.ts`]: {
plugins: ["typescript"],
// the actual codegen process.
await generate(
{
schema,
documents: "./src/**/*.graphql",
generates: {
// * Generates the types for your schema
[`${process.cwd()}/${output}/types.gq.ts`]: {
plugins: ["typescript"],
},
// * Generates near file .ts files for your fetch functions
[`${process.cwd()}/${output}`]: {
config: {
useTypeImports: true,
gPath,
importDocumentNodeExternallyFrom: "near-operation-file",
inlineFragmentTypes: "combine",
},
preset: "near-operation-file",
presetConfig: {
extension: ".gq.ts",
folder: "./",
baseTypesPath: `types.gq.ts`,
},
plugins: [
"typescript-operations", // operations, gets you types for operations (queries and mutations)
"@leveluptuts/g-query/codegen-plugin", // g-query codegen plugin. ./codegen.ts
],
},
},
},
// * Generates near file .ts files for your fetch functions
[`${process.cwd()}/${out}`]: {
config: {
useTypeImports: true,
gPath,
importDocumentNodeExternallyFrom: "near-operation-file",
inlineFragmentTypes: "combine",
},
preset: "near-operation-file",
presetConfig: {
extension: ".gq.ts",
folder: "./",
baseTypesPath: `types.gq.ts`,
},
plugins: [
"typescript-operations", // operations, gets you types for operations (queries and mutations)
"@leveluptuts/g-query/codegen-plugin", // g-query codegen plugin. ./codegen.ts
],
},
},
},
true
);
true
);
}

export default function levelupViteCodegen({
schema,
out,
gPath,
debug = false,
}): Plugin {
if (!schema) {
throw new Error("No schema provided");
}
if (!out) {
throw new Error("No output directory specified for types.");
}
if (!gPath) {
throw new Error(
"No gPath directory specified. gPath is where you've initialized the 'g' client"
);
}
export default function levelupViteCodegen({ schema, output, gPath, debug = false }: GQueryGenerateOptions): Plugin {
if (!schema) {
throw new Error("No schema provided");
}
if (!output) {
throw new Error("No output directory specified for types.");
}
if (!gPath) {
throw new Error("No gPath directory specified. gPath is where you've initialized the 'g' client");
}

return {
name: "g-query-codegen",
async buildStart() {
console.log("build start");
try {
await cleanGQ({ debug });
await gQueryGenerate({ schema, out, gPath, debug });
return {
name: "g-query-codegen",
async buildStart() {
console.log("build start");
try {
await cleanGQ({ debug });
await gQueryGenerate({ schema, output, gPath, debug });

return;
} catch (e) {
// TODO - These errors aren't good enough
console.log(
"❓ gFetch Error - Something Happened - Here is the error and some things to consider.",
e
);
console.log("❓ gFetch Error - Make sure `.graphql` are files found.");
console.log(
"❓ gFetch Warning - If you would like the gQuery generator to work (we reccomend you do)."
);
console.log(
"❓ gFetch Warning - If you would like to add them, you will need to restart SvelteKit."
);
}
return;
},
return;
} catch (e) {
// TODO - These errors aren't good enough
console.log("❓ gFetch Error - Something Happened - Here is the error and some things to consider.", e);
console.log("❓ gFetch Error - Make sure `.graphql` are files found.");
console.log(
"❓ gFetch Warning - If you would like the gQuery generator to work (we reccomend you do)."
);
console.log("❓ gFetch Warning - If you would like to add them, you will need to restart SvelteKit.");
}
return;
},

configureServer(server) {
const listener = async (absolutePath = "") => {
if (!filterExt.test(absolutePath)) return null;
try {
await cleanGQ({ debug });
await gQueryGenerate({ schema, out, gPath });
} catch (error) {
console.log("Something went wrong. Please save the file again.");
}
};
configureServer(server) {
const listener = async (absolutePath = "") => {
if (!filterExt.test(absolutePath)) return null;
try {
await cleanGQ({ debug });
await gQueryGenerate({ schema, output, gPath });
} catch (error) {
console.log("Something went wrong. Please save the file again.");
}
};

server.watcher.on("add", listener);
server.watcher.on("change", listener);
},
};
server.watcher.on("add", listener);
server.watcher.on("change", listener);
},
};
}
2 changes: 1 addition & 1 deletion docs/src/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ vite: {
// schema: 'http://localhost:3001/graphql' // this can also be a url to a graphql api
schema: './src/lib/graphql/schema.graphql', // path to schema, schema is required
output: './src/lib/graphql', // Where you want the general schema types to output
gPath: '$lib/config/g' // Path to g, created in step 1.
gPath: '$lib/config/g', // Path to g, created in step 1.
// Optional
debug: false // boolean, this adds logging for gq files deleted and on codegen
})
Expand Down