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";
|
2021-10-22 15:24:32 +00:00
|
|
|
import pascalCase from "just-pascal-case";
|
2021-10-13 17:06:31 +00:00
|
|
|
export const plugin = (schema, documents, config) => {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
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)}`;
|
2021-10-22 15:24:32 +00:00
|
|
|
const pascalName = pascalCase(name);
|
2021-10-13 17:06:31 +00:00
|
|
|
const opv = `${op}Variables`;
|
|
|
|
|
let operations = "";
|
|
|
|
|
if (o.operation === "query") {
|
|
|
|
|
operations += `
|
2021-12-14 16:20:26 +00:00
|
|
|
export const ${name} = writable<GFetchReturnWithErrors<${op}>>()
|
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) {
|
2021-10-22 15:24:32 +00:00
|
|
|
const data = await g.fetch<${op}>({
|
|
|
|
|
queries: [{ query: ${pascalName}Doc, variables }],
|
|
|
|
|
fetch
|
|
|
|
|
})
|
2021-12-14 16:20:26 +00:00
|
|
|
await ${name}.set({ ...data, errors: data?.errors, gQueryStatus: 'LOADED' })
|
2021-10-26 19:13:50 +00:00
|
|
|
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}>({
|
2021-10-22 15:24:32 +00:00
|
|
|
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 = [
|
2021-10-22 15:24:32 +00:00
|
|
|
`import { writable } from "svelte/store"`,
|
2021-10-13 17:06:31 +00:00
|
|
|
`import { g } from '${config.gPath}'`,
|
2021-10-22 15:24:32 +00:00
|
|
|
`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
|