gQuery/codegen/codegen.ts

129 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

2021-10-06 21:41:44 +00:00
import {
2021-10-13 17:06:31 +00:00
GraphQLSchema,
2021-10-06 21:41:44 +00:00
concatAST,
Kind,
2021-10-13 17:06:31 +00:00
FragmentDefinitionNode,
2021-10-06 21:41:44 +00:00
OperationDefinitionNode,
} from "graphql";
2021-12-24 00:56:25 +00:00
import {
Types,
PluginFunction,
oldVisit,
} from "@graphql-codegen/plugin-helpers";
2021-10-26 19:13:50 +00:00
import {
ClientSideBaseVisitor,
LoadedFragment,
} from "@graphql-codegen/visitor-plugin-common";
import pascalCase from "just-pascal-case";
2021-10-13 17:06:31 +00:00
export const plugin: PluginFunction<any> = (
schema: GraphQLSchema,
documents: Types.DocumentFile[],
config
) => {
const allAst = concatAST(documents.map((d) => d.document));
2021-10-26 19:13:50 +00:00
const allFragments: LoadedFragment[] = [
2021-10-13 17:06:31 +00:00
...(
allAst.definitions.filter(
(d) => d.kind === Kind.FRAGMENT_DEFINITION
) as FragmentDefinitionNode[]
).map((fragmentDef) => ({
node: fragmentDef,
name: fragmentDef.name.value,
onType: fragmentDef.typeCondition.name.value,
isExternal: false,
})),
...(config.externalFragments || []),
];
const visitor = new ClientSideBaseVisitor(
schema,
allFragments,
2021-10-26 19:13:50 +00:00
config,
2021-10-13 17:06:31 +00:00
{ documentVariableSuffix: "Doc" },
documents
);
2021-12-24 00:56:25 +00:00
const visitorResult = oldVisit(allAst, { leave: visitor });
2021-10-13 17:06:31 +00:00
const operations = allAst.definitions.filter(
(d) => d.kind === Kind.OPERATION_DEFINITION
) as OperationDefinitionNode[];
2021-10-13 17:06:31 +00:00
const defaultTypes = `
2021-09-30 16:19:53 +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-09-30 16:19:53 +00:00
2021-10-13 17:06:31 +00:00
const ops = operations
.map((o) => {
if (o) {
const name = o?.name?.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-12-14 16:20:26 +00:00
export const ${name} = writable<GFetchReturnWithErrors<${op}>>()
2021-10-13 17:06:31 +00:00
// Cached
2021-10-26 19:13:50 +00:00
export async function get${pascalName}({ fetch, variables }: GGetParameters<${opv}>, options?: CacheFunctionOptions) {
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}>> =>
2021-09-30 16:19:53 +00:00
g.fetch<${op}>({
queries: [{ query: ${pascalName}Doc, variables }],
2021-09-30 16:19:53 +00:00
fetch,
})
2021-10-06 21:41:44 +00:00
`;
2021-09-30 16:19:53 +00:00
}
2021-10-13 17:06:31 +00:00
return operations;
}
})
.join("\n");
const imports = [
`import { 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-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