gQuery/codegen/codegen.ts

170 lines
4.6 KiB
TypeScript
Raw Normal View History

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-13 17:06:31 +00:00
import {
LoadedFragment,
ClientSideBaseVisitor,
} from "@graphql-codegen/visitor-plugin-common";
2021-10-06 21:41:44 +00:00
import { pascalCase } from "pascal-case";
2021-09-30 16:19:53 +00:00
2021-10-13 17:06:31 +00:00
console.log("codegen.ts");
export const plugin: PluginFunction<any> = (
schema: GraphQLSchema,
documents: Types.DocumentFile[],
config
) => {
console.log("config", config);
const allAst = concatAST(documents.map((d) => d.document));
const allFragments: LoadedFragment[] = [
...(
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[];
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)}`;
const opv = `${op}Variables`;
let operations = "";
if (o.operation === "query") {
operations += `
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}>({
queries: [{ query: ${pascalCase(name)}Doc, variables }],
fetch
})
2021-10-13 17:06:31 +00:00
// Cached
export async function get${pascalCase(
name
)}(variables, options?: CacheFunctionOptions) {
await gQuery('user', { query: ${name}, variables }, options)
}
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}>({
queries: [{ query: ${pascalCase(name)}Doc, variables }]
})
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: ${pascalCase(name)}Doc, variables }],
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"`,
`import { g } from '${config.gPath}'`,
`import type { GFetchReturnWithErrors } from '@leveluptuts/g-query'`,
`import { gQuery } from '@leveluptuts/g-query'`,
`import gql from "graphql-tag"`,
];
// let schemaInputs = getCachedDocumentNodeFromSchema(schema).definitions.filter((d) => {
// return d.kind === 'InputObjectTypeDefinition'
// })
// let inputs = schemaInputs
// .map((d) => {
// console.log('/* START */')
// // @ts-ignore
// console.log('NAME: ', d.fields[0].name.value)
// // @ts-ignore
// let isReq = d.fields[0]?.type?.kind === 'NonNullType'
// console.log('REQUIRED: ', isReq ? '✅' : '❌')
// // @ts-ignore
// console.log('TYPE: ', isReq ? d.fields[0]?.type?.type?.name?.value : d.fields[0]?.type?.name?.value)
// // @ts-ignore
// // @ts-ignore
// console.log('d.fields[0]', d.fields[0]?.type)
// console.log('/* END */')
// console.log('')
// return `
// const inputName = {
// ${d.fields[0].name.value}: ${isReq ? d.fields[0]?.type?.type?.name?.value : d.fields[0]?.type?.name?.value}
// }
// `
// })
// .join('\n')
// console.log('inputs', inputs)
return {
prepend: imports,
content: [
defaultTypes,
visitor.fragments,
...visitorResult.definitions.filter((t) => typeof t == "string"),
ops,
].join("\n"),
};
};