UNPKG

@graphql-eslint/eslint-plugin

Version:
50 lines (49 loc) 1.88 kB
import { dirname } from '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; export function loadOnDiskGraphQLConfig(filePath) { return loadConfigSync({ // load config relative to the file being linted rootDir: dirname(filePath), throwOnEmpty: false, throwOnMissing: false, extensions: [codeFileLoaderExtension], }); } export function loadGraphQLConfig(options) { // We don't want cache config on test environment // Otherwise schema and documents will be same for all tests if (process.env.NODE_ENV !== 'test' && graphQLConfig) { return graphQLConfig; } const onDiskConfig = !options.skipGraphQLConfig && loadOnDiskGraphQLConfig(options.filePath); debug('options.skipGraphQLConfig: %o', options.skipGraphQLConfig); if (onDiskConfig) { debug('Graphql-config path %o', onDiskConfig.filepath); } const configOptions = options.projects ? { projects: options.projects } : { schema: (options.schema || ''), documents: options.documents || options.operations, extensions: options.extensions, include: options.include, exclude: options.exclude, }; graphQLConfig = onDiskConfig || new GraphQLConfig({ config: configOptions, filepath: 'virtual-config', }, [codeFileLoaderExtension]); return graphQLConfig; } const codeFileLoaderExtension = api => { const { schema, documents } = api.loaders; schema.register(new CodeFileLoader()); documents.register(new CodeFileLoader()); return { name: 'graphql-eslint-loaders' }; };