UNPKG

@graphql-eslint/eslint-plugin

Version:
46 lines (45 loc) 1.71 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) { for (; !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: !1, 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); 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 }; return graphQLConfig = onDiskConfig || new GraphQLConfig({ config: configOptions, filepath: "" }, [codeFileLoaderExtension]), graphQLConfig; } const codeFileLoaderExtension = (api) => { const { schema, documents } = api.loaders; return schema.register(new CodeFileLoader()), documents.register(new CodeFileLoader()), { name: "code-file-loaders" }; }; export { getFirstExistingPath, loadGraphQLConfig, loadOnDiskGraphQLConfig };