From b7055d6f7f728b1e705027942f1afba96bcd3528 Mon Sep 17 00:00:00 2001 From: Alexander Steppke Date: Fri, 19 Nov 2021 08:51:03 +0100 Subject: [PATCH] add configFile option to schemaLoad --- src/schemaLoader.ts | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/schemaLoader.ts b/src/schemaLoader.ts index 0fe81a3f..650b2c2f 100644 --- a/src/schemaLoader.ts +++ b/src/schemaLoader.ts @@ -1,4 +1,7 @@ +import { getOptions } from 'loader-utils'; import logUpdate from 'log-update'; +import { validate } from 'schema-utils'; +import type { Schema as JsonSchema } from 'schema-utils/declarations/validate'; import { loader } from 'webpack'; import { processCodegenForContext } from './lib/codegen'; import loadConfig from './lib/config'; @@ -8,9 +11,32 @@ import { readFile } from './lib/file'; import memoize from './lib/memoize'; import { PRINT_PREFIX } from './lib/print'; +const optionsSchema: JsonSchema = { + type: 'object', + properties: { + configFile: { + type: 'string', + }, + }, + required: [], +}; +export interface GraphQLLetSchemaLoaderOptions { + configFile?: string; +} + +function parseOptions( + ctx: loader.LoaderContext, +): GraphQLLetSchemaLoaderOptions { + const options = getOptions(ctx); + + validate(optionsSchema, options); + + return (options as unknown) as GraphQLLetSchemaLoaderOptions; +} + const processGraphQLCodegenSchemaLoader = memoize( - async (cwd: string) => { - const [config, configHash] = await loadConfig(cwd); + async (cwd: string, options: GraphQLLetSchemaLoaderOptions) => { + const [config, configHash] = await loadConfig(cwd, options.configFile); const { execContext, codegenContext } = await createExecContext( cwd, config, @@ -35,8 +61,9 @@ const processGraphQLCodegenSchemaLoader = memoize( const graphlqCodegenSchemaLoader: loader.Loader = function (gqlContent) { const callback = this.async()!; const { rootContext: cwd } = this; + const options = parseOptions(this); - processGraphQLCodegenSchemaLoader(cwd) + processGraphQLCodegenSchemaLoader(cwd, options) .then(() => { callback(undefined, gqlContent); })