UNPKG

@graphql-eslint/eslint-plugin

Version:
53 lines (52 loc) 1.74 kB
import fs from "node:fs"; import path from "node:path"; import debugFactory from "debug"; import { GraphQLConfig, loadConfigSync } from "graphql-config"; import { CodeFileLoader } from "@graphql-tools/code-file-loader"; const debug = debugFactory("graphql-eslint:graphql-config"); let graphQLConfig; function getFirstExistingPath(filePath) { while (!fs.existsSync(filePath)) { filePath = path.dirname(filePath); } return filePath; } function loadOnDiskGraphQLConfig(filePath) { return loadConfigSync({ // load config relative to the file being linted rootDir: getFirstExistingPath(path.dirname(filePath)), throwOnMissing: false, extensions: [codeFileLoaderExtension] }); } function loadGraphQLConfig({ graphQLConfig: config, filePath }) { if (process.env.NODE_ENV !== "test" && graphQLConfig) { return graphQLConfig; } debug("parserOptions.graphQLConfig: %o", config); const onDiskConfig = !config && loadOnDiskGraphQLConfig(filePath); if (onDiskConfig) { debug("GraphQL-Config path %o", onDiskConfig.filepath); } const configOptions = config && ("projects" in config || "schemaPath" in config) ? config : { // if `schema` is `undefined` will throw error `Project 'default' not found` schema: config?.schema ?? "", ...config }; graphQLConfig = onDiskConfig || new GraphQLConfig({ config: configOptions, filepath: "" }, [codeFileLoaderExtension]); return graphQLConfig; } const codeFileLoaderExtension = (api) => { const { schema, documents } = api.loaders; schema.register(new CodeFileLoader()); documents.register(new CodeFileLoader()); return { name: "code-file-loaders" }; }; export { getFirstExistingPath, loadGraphQLConfig, loadOnDiskGraphQLConfig };