gQuery/codegen/plugin.js

62 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-10-06 21:41:44 +00:00
import { generate } from "@graphql-codegen/cli";
2021-10-26 19:13:50 +00:00
const fileRegex = /\.(graphql)$/; // not used, will be for the vite rerun
2021-10-06 21:41:44 +00:00
export default function levelupViteCodegen(options) {
if (!options.schema) {
throw new Error("No schema provided");
}
if (!options.output) {
throw new Error("No output directory specified");
}
2021-10-13 17:06:31 +00:00
if (!options.gPath) {
throw new Error("No gPath directory specified. gPath is where you've initialized the 'g' client");
}
const { schema, output, gPath } = options;
2021-10-26 19:13:50 +00:00
console.log("running plugin");
2021-10-06 21:41:44 +00:00
return {
2021-10-26 19:13:50 +00:00
name: "g-query-codegen",
2021-10-06 21:41:44 +00:00
async buildStart() {
try {
2021-11-15 23:10:40 +00:00
// *1. Remove all .gq files
// TODO: Find and remove all .gq files
2021-10-26 19:13:50 +00:00
// *2. Generate
await generate({
2021-10-06 21:41:44 +00:00
schema,
documents: "./src/**/*.graphql",
generates: {
2021-10-26 19:13:50 +00:00
// * Generates the types for your schema
2021-11-15 23:10:40 +00:00
[`${process.cwd()}/${output}/types.gq.ts`]: {
2021-10-06 21:41:44 +00:00
plugins: ["typescript"],
},
2021-10-26 19:13:50 +00:00
// * Generates near file .ts files for your fetch functions
2021-10-13 17:06:31 +00:00
[`${process.cwd()}/${output}`]: {
config: {
2021-10-26 19:13:50 +00:00
useTypeImports: true,
2021-10-13 17:06:31 +00:00
gPath,
2021-10-26 19:13:50 +00:00
importDocumentNodeExternallyFrom: "near-operation-file",
inlineFragmentTypes: "combine",
2021-10-13 17:06:31 +00:00
},
preset: "near-operation-file",
presetConfig: {
2021-11-15 23:10:40 +00:00
extension: ".gq.ts",
2021-10-13 17:06:31 +00:00
folder: "./",
2021-11-15 23:10:40 +00:00
baseTypesPath: `types.gq.ts`,
2021-10-13 17:06:31 +00:00
},
plugins: [
"typescript-operations",
2021-10-26 19:13:50 +00:00
"@leveluptuts/g-query/codegen-plugin", // g-query codegen plugin.
2021-10-13 17:06:31 +00:00
],
},
2021-10-06 21:41:44 +00:00
},
}, true);
}
catch (e) {
console.log("❓ gFetch Warning - No `.graphql` 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;
},
transform(src, id) { },
};
}
//# sourceMappingURL=plugin.js.map