gQuery/codegen/codegen.js

104 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-10-13 17:06:31 +00:00
import { visit, concatAST, Kind, } from "graphql";
import "@graphql-codegen/plugin-helpers";
2021-10-26 19:13:50 +00:00
import { ClientSideBaseVisitor, } from "@graphql-codegen/visitor-plugin-common";
import pascalCase from "just-pascal-case";
2021-10-13 17:06:31 +00:00
export const plugin = (schema, documents, config) => {
2021-10-26 19:13:50 +00:00
console.log("🛠️ Codegen Starting");
2021-10-13 17:06:31 +00:00
const allAst = concatAST(documents.map((d) => d.document));
const allFragments = [
...allAst.definitions.filter((d) => d.kind === Kind.FRAGMENT_DEFINITION).map((fragmentDef) => ({
node: fragmentDef,
name: fragmentDef.name.value,
onType: fragmentDef.typeCondition.name.value,
isExternal: false,
})),
...(config.externalFragments || []),
];
2021-10-26 19:13:50 +00:00
const visitor = new ClientSideBaseVisitor(schema, allFragments, config, { documentVariableSuffix: "Doc" }, documents);
2021-10-13 17:06:31 +00:00
const visitorResult = visit(allAst, { leave: visitor });
const operations = allAst.definitions.filter((d) => d.kind === Kind.OPERATION_DEFINITION);
const defaultTypes = `
2021-10-06 21:41:44 +00:00
type FetchWrapperArgs<T> = {
fetch: typeof fetch,
variables?: T,
}
type SubscribeWrapperArgs<T> = {
variables?: T,
}
2021-10-13 17:06:31 +00:00
2021-10-26 19:13:50 +00:00
2021-10-13 17:06:31 +00:00
interface CacheFunctionOptions {
update?: boolean
}
2021-10-06 21:41:44 +00:00
`;
2021-10-13 17:06:31 +00:00
const ops = operations
.map((o) => {
var _a;
if (o) {
2021-10-26 19:13:50 +00:00
console.log("o", o);
2021-10-13 17:06:31 +00:00
const name = ((_a = o === null || o === void 0 ? void 0 : o.name) === null || _a === void 0 ? void 0 : _a.value) || "";
const op = `${pascalCase(name)}${pascalCase(o.operation)}`;
const pascalName = pascalCase(name);
2021-10-13 17:06:31 +00:00
const opv = `${op}Variables`;
let operations = "";
if (o.operation === "query") {
operations += `
2021-10-26 19:13:50 +00:00
export const ${name} = writable<${op}>()
2021-10-26 19:13:50 +00:00
export const fetch${pascalName} = ({ variables, fetch}: FetchWrapperArgs<${opv}>):
2021-10-06 21:41:44 +00:00
Promise<GFetchReturnWithErrors<${op}>> =>
g.fetch<${op}>({
queries: [{ query: ${pascalName}Doc, variables }],
2021-10-06 21:41:44 +00:00
fetch
})
2021-10-13 17:06:31 +00:00
2021-10-26 19:13:50 +00:00
// Cached
export async function get${pascalName}({ fetch, variables }: GGetParameters<${opv}>, options?: CacheFunctionOptions) {
const data = await g.fetch<${op}>({
queries: [{ query: ${pascalName}Doc, variables }],
fetch
})
2021-10-26 19:13:50 +00:00
await ${name}.set({ ...data, error: data?.error, gQueryStatus: 'LOADED' })
return data
2021-10-13 17:06:31 +00:00
}
2021-10-06 21:41:44 +00:00
`;
2021-10-13 17:06:31 +00:00
}
else if (o.operation === "mutation") {
operations += `
2021-10-06 21:41:44 +00:00
export const ${name} = ({ variables }: SubscribeWrapperArgs<${opv}>):
Promise<GFetchReturnWithErrors<${op}>> =>
g.fetch<${op}>({
queries: [{ query: ${pascalName}Doc, variables }],
2021-10-06 21:41:44 +00:00
fetch,
})
`;
}
2021-10-13 17:06:31 +00:00
return operations;
}
})
.join("\n");
const imports = [
`import { writable } from "svelte/store"`,
2021-10-26 19:13:50 +00:00
`import type { Writable } from "svelte/store"`,
2021-10-13 17:06:31 +00:00
`import { g } from '${config.gPath}'`,
`import type { GFetchReturnWithErrors, GGetParameters } from '@leveluptuts/g-query'`,
2021-10-13 17:06:31 +00:00
];
return {
2021-10-26 19:13:50 +00:00
prepend: [...imports, ...visitor.getImports()],
2021-10-13 17:06:31 +00:00
content: [
defaultTypes,
visitor.fragments,
...visitorResult.definitions.filter((t) => typeof t == "string"),
ops,
].join("\n"),
};
2021-10-06 21:41:44 +00:00
};
2021-10-26 19:13:50 +00:00
// TODO
// - add option to force update of cache. ie getUserTutorials({update: true})
// if update.true is not set, then it will only update if the cache is empty
2021-10-06 21:41:44 +00:00
//# sourceMappingURL=codegen.js.map