Last active
October 13, 2021 21:29
-
-
Save andrewlinfoot/fa7daad5af2dd028ab5b73e7d511e68e to your computer and use it in GitHub Desktop.
generateResolversFiles.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This script generates server/src/graphql/resolvers.ts file so that we don't | |
* have to manually maintain an imports file. | |
*/ | |
import { writeFileSync } from "fs"; | |
import { join } from "path"; | |
import { format } from "prettier"; | |
import getDirectoryFiles from "utils/getDirectoryFiles"; | |
import logger from "utils/logger"; | |
const resolversDirectoryPath = join(__dirname, "../graphql/resolvers"); | |
const resolversQueriesPath = join(resolversDirectoryPath, "queries"); | |
const resolversMutationsPath = join(resolversDirectoryPath, "mutations"); | |
const objects = getDirectoryFiles({ directoryPath: resolversDirectoryPath }); | |
const queries = getDirectoryFiles({ directoryPath: resolversQueriesPath }); | |
const mutations = getDirectoryFiles({ directoryPath: resolversMutationsPath }); | |
const resolversFilePath = join( | |
resolversDirectoryPath, | |
"../__generated__/resolvers.ts" | |
); | |
const generateResolverFileText = ({ | |
objects, | |
queries, | |
mutations, | |
}: { | |
objects: string[]; | |
queries: string[]; | |
mutations: string[]; | |
}): string => { | |
const queryImports = queries.map( | |
(query) => `import ${query} from "../resolvers/queries/${query}";` | |
); | |
const mutationImports = mutations.map( | |
(mutation) => | |
`import ${mutation} from "../resolvers/mutations/${mutation}";` | |
); | |
const objectImports = objects.map( | |
(object) => `import ${object} from "../resolvers/${object}";` | |
); | |
const text = ` | |
/* eslint-disable */ | |
/** | |
* This file is auto generated from 'scripts/generateResolversFile.ts' DO | |
* NOT EDIT. THE FILE WILL BE OVERWRITTEN DURING BUILD. | |
**/ | |
// queries | |
${queryImports.join("\n")} | |
// mutations | |
${mutationImports.join("\n")} | |
// objects | |
${objectImports.join("\n")} | |
const resolvers = { | |
${objects.join(",\n")}, | |
Query:{ | |
${queries | |
// Remove word "Query" used on for the filename | |
.map((query) => `${query.replace("Query", "")}: ${query}`) | |
.join(",\n")} | |
}, | |
Mutation:{ | |
${mutations.join(",\n")} | |
}, | |
}; | |
export default resolvers; | |
`; | |
return format(text, { parser: "typescript" }); | |
}; | |
const resolverFileText = generateResolverFileText({ | |
objects, | |
queries, | |
mutations, | |
}); | |
writeFileSync(resolversFilePath, resolverFileText); | |
logger.info(`Created resolvers file at ${resolversFilePath}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { parse } from "path"; | |
import glob from "glob"; | |
interface GetDirectoryFilesParams { | |
directoryPath: string; | |
} | |
const getDirectoryFiles = (params: GetDirectoryFilesParams): string[] => { | |
const { directoryPath } = params; | |
const filesWithAbsolutePaths = glob.sync(`${directoryPath}/*.{t,j}s`); | |
const fileNames = filesWithAbsolutePaths.map((filePath: string) => { | |
return parse(filePath).name; | |
}); | |
return fileNames; | |
}; | |
export default getDirectoryFiles; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment