From 9ebb249df092ab28b949e2acd6dca42c56491315 Mon Sep 17 00:00:00 2001 From: Shah Shalin Date: Thu, 6 Feb 2025 17:08:49 +0530 Subject: [PATCH 1/3] Refactor: Move the schema file logic from individual codegen configurations to the base configuration. Add the `withCodegenConfig` function to extend the base configuration with the provided configurations. --- examples/nextjs/starter/codegen.ts | 21 ++-------- packages/codegen-config/src/index.ts | 57 ++++++++++++++++++++++++---- packages/query/codegen.ts | 23 ++--------- 3 files changed, 56 insertions(+), 45 deletions(-) diff --git a/examples/nextjs/starter/codegen.ts b/examples/nextjs/starter/codegen.ts index b583c1a2..5053b4f8 100644 --- a/examples/nextjs/starter/codegen.ts +++ b/examples/nextjs/starter/codegen.ts @@ -1,28 +1,13 @@ import type { CodegenConfig } from '@graphql-codegen/cli'; import { sync as globSync } from 'glob'; -import baseConfig from '@snapwp/codegen-config'; -import { generateGraphqlUrl } from '@snapwp/core'; +import { withCodegenConfig } from '@snapwp/codegen-config'; import 'dotenv/config'; const GRAPHQL_GLOB = './src/**/*.graphql'; const graphqlFiles = globSync( GRAPHQL_GLOB ); -const config: CodegenConfig = { +const config: Partial< CodegenConfig > = { ...( graphqlFiles.length > 0 && { documents: GRAPHQL_GLOB } ), - ...baseConfig, - // Use the schema file if it's set by CI. - schema: process.env.GRAPHQL_SCHEMA_FILE ?? [ - { - [ generateGraphqlUrl( - process.env.NEXT_PUBLIC_WORDPRESS_URL, - process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT - ) ]: { - headers: { - Authorization: `${ process.env.INTROSPECTION_TOKEN }`, - }, - }, - }, - ], }; -export default config; +export default withCodegenConfig( config ); diff --git a/packages/codegen-config/src/index.ts b/packages/codegen-config/src/index.ts index 8926fe08..abb5bac2 100644 --- a/packages/codegen-config/src/index.ts +++ b/packages/codegen-config/src/index.ts @@ -1,13 +1,54 @@ import type { CodegenConfig } from '@graphql-codegen/cli'; +import { generateGraphqlUrl } from '@snapwp/core'; -const baseConfig: CodegenConfig = { - overwrite: true, - generates: { - 'src/__generated/': { - preset: 'client', - plugins: [], +/** + * Base configuration for GraphQL Codegen. + * + * @return The base configuration. + */ +const baseConfig = (): CodegenConfig => { + return { + overwrite: true, + generates: { + 'src/__generated/': { + preset: 'client', + plugins: [], + }, }, - }, + // Use the schema file if it's set by CI. + // eslint-disable-next-line n/no-process-env + schema: process.env.GRAPHQL_SCHEMA_FILE ?? [ + { + [ generateGraphqlUrl( + // eslint-disable-next-line n/no-process-env + process.env.NEXT_PUBLIC_WORDPRESS_URL, + // eslint-disable-next-line n/no-process-env + process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT + ) ]: { + headers: { + // eslint-disable-next-line n/no-process-env + Authorization: `${ process.env.INTROSPECTION_TOKEN }`, + }, + }, + }, + ], + }; }; -export default baseConfig; +/** + * Extend the base configuration with the provided configuration. + * + * @param config The configuration to extend the base configuration with. + * + * @return The extended configuration. + */ +const withCodegenConfig = ( + config: Partial< CodegenConfig > +): CodegenConfig => { + return { + ...baseConfig(), + ...config, + }; +}; + +export { withCodegenConfig }; diff --git a/packages/query/codegen.ts b/packages/query/codegen.ts index b92e74dc..26602698 100644 --- a/packages/query/codegen.ts +++ b/packages/query/codegen.ts @@ -1,26 +1,11 @@ -import type { CodegenConfig } from '@graphql-codegen/cli'; import * as dotenv from 'dotenv'; -import baseConfigs from '@snapwp/codegen-config'; -import { generateGraphqlUrl } from '@snapwp/core'; +import type { CodegenConfig } from '@graphql-codegen/cli'; +import { withCodegenConfig } from '@snapwp/codegen-config'; dotenv.config( { path: '../../.env' } ); -const config: CodegenConfig = { - ...baseConfigs, +const config: Partial< CodegenConfig > = { documents: './src/**/*.graphql', - // Use the schema file if it's set by CI. - schema: process.env.GRAPHQL_SCHEMA_FILE ?? [ - { - [ generateGraphqlUrl( - process.env.NEXT_PUBLIC_WORDPRESS_URL, - process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT - ) ]: { - headers: { - Authorization: `${ process.env.INTROSPECTION_TOKEN }`, - }, - }, - }, - ], }; -export default config; +export default withCodegenConfig( config ); From eb3ea90f03593cc4b5e8bb5bfa3f579514f17cd6 Mon Sep 17 00:00:00 2001 From: Shah Shalin Date: Tue, 11 Feb 2025 13:08:52 +0530 Subject: [PATCH 2/3] refactor: remove withCodegenConfig HOC and moved back to baseConfig() function --- examples/nextjs/starter/codegen.ts | 13 +++++++------ packages/codegen-config/src/index.ts | 18 +----------------- packages/query/codegen.ts | 10 ++++------ 3 files changed, 12 insertions(+), 29 deletions(-) diff --git a/examples/nextjs/starter/codegen.ts b/examples/nextjs/starter/codegen.ts index 5053b4f8..7c768d87 100644 --- a/examples/nextjs/starter/codegen.ts +++ b/examples/nextjs/starter/codegen.ts @@ -1,13 +1,14 @@ -import type { CodegenConfig } from '@graphql-codegen/cli'; import { sync as globSync } from 'glob'; -import { withCodegenConfig } from '@snapwp/codegen-config'; +import { baseConfig } from '@snapwp/codegen-config'; import 'dotenv/config'; const GRAPHQL_GLOB = './src/**/*.graphql'; const graphqlFiles = globSync( GRAPHQL_GLOB ); -const config: Partial< CodegenConfig > = { - ...( graphqlFiles.length > 0 && { documents: GRAPHQL_GLOB } ), -}; +const config = baseConfig(); -export default withCodegenConfig( config ); +if ( graphqlFiles.length > 0 ) { + config.documents = GRAPHQL_GLOB; +} + +export default config; diff --git a/packages/codegen-config/src/index.ts b/packages/codegen-config/src/index.ts index abb5bac2..ee2034e1 100644 --- a/packages/codegen-config/src/index.ts +++ b/packages/codegen-config/src/index.ts @@ -35,20 +35,4 @@ const baseConfig = (): CodegenConfig => { }; }; -/** - * Extend the base configuration with the provided configuration. - * - * @param config The configuration to extend the base configuration with. - * - * @return The extended configuration. - */ -const withCodegenConfig = ( - config: Partial< CodegenConfig > -): CodegenConfig => { - return { - ...baseConfig(), - ...config, - }; -}; - -export { withCodegenConfig }; +export { baseConfig }; diff --git a/packages/query/codegen.ts b/packages/query/codegen.ts index 26602698..81dda8fd 100644 --- a/packages/query/codegen.ts +++ b/packages/query/codegen.ts @@ -1,11 +1,9 @@ import * as dotenv from 'dotenv'; -import type { CodegenConfig } from '@graphql-codegen/cli'; -import { withCodegenConfig } from '@snapwp/codegen-config'; +import { baseConfig } from '@snapwp/codegen-config'; dotenv.config( { path: '../../.env' } ); -const config: Partial< CodegenConfig > = { - documents: './src/**/*.graphql', -}; +const config = baseConfig(); +config.documents = './src/**/*.graphql'; -export default withCodegenConfig( config ); +export default config; From 0a4b87d2325d13f43f1adb35ec4b9ae641292ebd Mon Sep 17 00:00:00 2001 From: Shah Shalin Date: Wed, 12 Feb 2025 12:13:42 +0530 Subject: [PATCH 3/3] refactor: renamed baseConfig() function to getBaseConfig() --- examples/nextjs/starter/codegen.ts | 4 ++-- packages/codegen-config/src/index.ts | 4 ++-- packages/query/codegen.ts | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/nextjs/starter/codegen.ts b/examples/nextjs/starter/codegen.ts index 7c768d87..f8043595 100644 --- a/examples/nextjs/starter/codegen.ts +++ b/examples/nextjs/starter/codegen.ts @@ -1,11 +1,11 @@ import { sync as globSync } from 'glob'; -import { baseConfig } from '@snapwp/codegen-config'; +import { getBaseConfig } from '@snapwp/codegen-config'; import 'dotenv/config'; const GRAPHQL_GLOB = './src/**/*.graphql'; const graphqlFiles = globSync( GRAPHQL_GLOB ); -const config = baseConfig(); +const config = getBaseConfig(); if ( graphqlFiles.length > 0 ) { config.documents = GRAPHQL_GLOB; diff --git a/packages/codegen-config/src/index.ts b/packages/codegen-config/src/index.ts index bee8adfe..627dc015 100644 --- a/packages/codegen-config/src/index.ts +++ b/packages/codegen-config/src/index.ts @@ -6,7 +6,7 @@ import { generateGraphqlUrl } from '@snapwp/core'; * * @return The base configuration. */ -const baseConfig = (): CodegenConfig => { +const getBaseConfig = (): CodegenConfig => { return { overwrite: true, generates: { @@ -40,4 +40,4 @@ const baseConfig = (): CodegenConfig => { }; }; -export { baseConfig }; +export { getBaseConfig }; diff --git a/packages/query/codegen.ts b/packages/query/codegen.ts index 81dda8fd..923e6ccb 100644 --- a/packages/query/codegen.ts +++ b/packages/query/codegen.ts @@ -1,9 +1,9 @@ import * as dotenv from 'dotenv'; -import { baseConfig } from '@snapwp/codegen-config'; +import { getBaseConfig } from '@snapwp/codegen-config'; dotenv.config( { path: '../../.env' } ); -const config = baseConfig(); +const config = getBaseConfig(); config.documents = './src/**/*.graphql'; export default config;