2021-10-06 21:41:44 +00:00
|
|
|
import {
|
2021-10-13 17:06:31 +00:00
|
|
|
visit,
|
|
|
|
|
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-10-20 03:58:13 +00:00
|
|
|
import { Types, PluginFunction } from "@graphql-codegen/plugin-helpers";
|
2021-10-22 15:24:32 +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: PluginFunction<any> = (
|
|
|
|
|
schema: GraphQLSchema,
|
|
|
|
|
documents: Types.DocumentFile[],
|
|
|
|
|
config
|
|
|
|
|
) => {
|
|
|
|
|
const allAst = concatAST(documents.map((d) => d.document));
|
|
|
|
|
|
2021-10-22 15:24:32 +00:00
|
|
|
const allFragments = [
|
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,
|
|
|
|
|
{},
|
|
|
|
|
{ documentVariableSuffix: "Doc" },
|
|
|
|
|
documents
|
|
|
|
|
);
|
|
|
|
|
const visitorResult = visit(allAst, { leave: visitor });
|
|
|
|
|
|
|
|
|
|
const operations = allAst.definitions.filter(
|
|
|
|
|
(d) => d.kind === Kind.OPERATION_DEFINITION
|
|
|
|
|
) as OperationDefinitionNode[];
|
2021-10-22 15:24:32 +00:00
|
|
|
|
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 dsl = `export const ${pascalCase(op.name.value)}Doc = gql\`
|
|
|
|
|
// ${documents.find((d) => d.rawSDL.includes(`${op.operation} ${op.name.value}`)).rawSDL}\``
|
|
|
|
|
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-10-22 15:24:32 +00:00
|
|
|
export const ${name}Store = writable()
|
|
|
|
|
|
2021-10-06 21:41:44 +00:00
|
|
|
export const ${name} = ({ variables, fetch}: FetchWrapperArgs<${opv}>):
|
|
|
|
|
Promise<GFetchReturnWithErrors<${op}>> =>
|
2021-09-30 16:19:53 +00:00
|
|
|
g.fetch<${op}>({
|
2021-10-22 15:24:32 +00:00
|
|
|
queries: [{ query: ${pascalName}Doc, variables }],
|
2021-09-30 16:19:53 +00:00
|
|
|
fetch
|
|
|
|
|
})
|
2021-10-13 17:06:31 +00:00
|
|
|
|
|
|
|
|
|
2021-10-22 15:24:32 +00:00
|
|
|
// Cached
|
|
|
|
|
export async function get${pascalCase(
|
|
|
|
|
name
|
|
|
|
|
)}({ fetch, variables }: GGetParameters<${opv}>, options?: CacheFunctionOptions) {
|
|
|
|
|
const data = await g.fetch<${op}>({
|
|
|
|
|
queries: [{ query: ${pascalName}Doc, variables }],
|
|
|
|
|
fetch
|
|
|
|
|
})
|
|
|
|
|
await ${name}Store.set({ ...data})
|
|
|
|
|
await gQuery(${name}, { query: ${name}, variables }, options)
|
2021-10-13 17:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
2021-10-06 21:41:44 +00:00
|
|
|
`;
|
2021-10-13 17:06:31 +00:00
|
|
|
// If config is set to have subscription query, also write the
|
|
|
|
|
if (config.subscriptionQuery) {
|
|
|
|
|
operations += `
|
2021-10-06 21:41:44 +00:00
|
|
|
export const ${name}Subscribe = ({ variables }: SubscribeWrapperArgs<${opv}>):
|
|
|
|
|
Readable<GFetchReturnWithErrors<${op}>> =>
|
2021-09-30 16:19:53 +00:00
|
|
|
g.oFetch<${op}>({
|
2021-10-22 15:24:32 +00:00
|
|
|
queries: [{ query: ${pascalName}Doc, variables }]
|
2021-09-30 16:19:53 +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}>({
|
2021-10-22 15:24:32 +00:00
|
|
|
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 type { Readable } from "svelte/store"`,
|
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
|
|
|
`import { gQuery } from '@leveluptuts/g-query'`,
|
|
|
|
|
`import gql from "graphql-tag"`,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
prepend: imports,
|
|
|
|
|
content: [
|
|
|
|
|
defaultTypes,
|
|
|
|
|
visitor.fragments,
|
|
|
|
|
...visitorResult.definitions.filter((t) => typeof t == "string"),
|
|
|
|
|
ops,
|
|
|
|
|
].join("\n"),
|
|
|
|
|
};
|
|
|
|
|
};
|