diff --git a/examples/nextjs/starter/codegen.ts b/examples/nextjs/starter/codegen.ts index b583c1a2..f8043595 100644 --- a/examples/nextjs/starter/codegen.ts +++ b/examples/nextjs/starter/codegen.ts @@ -1,28 +1,14 @@ -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 { getBaseConfig } from '@snapwp/codegen-config'; import 'dotenv/config'; const GRAPHQL_GLOB = './src/**/*.graphql'; const graphqlFiles = globSync( GRAPHQL_GLOB ); -const config: 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 }`, - }, - }, - }, - ], -}; +const config = getBaseConfig(); + +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 f48d5c5b..627dc015 100644 --- a/packages/codegen-config/src/index.ts +++ b/packages/codegen-config/src/index.ts @@ -1,18 +1,43 @@ import type { CodegenConfig } from '@graphql-codegen/cli'; +import { generateGraphqlUrl } from '@snapwp/core'; -const baseConfig: CodegenConfig = { - overwrite: true, - generates: { - 'src/__generated/': { - preset: 'client', - plugins: [], - config: { - enumsAsTypes: true, - skipTypename: true, - useTypeImports: true, +/** + * Base configuration for GraphQL Codegen. + * + * @return The base configuration. + */ +const getBaseConfig = (): CodegenConfig => { + return { + overwrite: true, + generates: { + 'src/__generated/': { + preset: 'client', + plugins: [], + config: { + enumsAsTypes: true, + skipTypename: true, + useTypeImports: true, + }, }, }, - }, + // 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; +export { getBaseConfig }; diff --git a/packages/query/codegen.ts b/packages/query/codegen.ts index b92e74dc..923e6ccb 100644 --- a/packages/query/codegen.ts +++ b/packages/query/codegen.ts @@ -1,26 +1,9 @@ -import type { CodegenConfig } from '@graphql-codegen/cli'; import * as dotenv from 'dotenv'; -import baseConfigs from '@snapwp/codegen-config'; -import { generateGraphqlUrl } from '@snapwp/core'; +import { getBaseConfig } from '@snapwp/codegen-config'; dotenv.config( { path: '../../.env' } ); -const config: CodegenConfig = { - ...baseConfigs, - 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 }`, - }, - }, - }, - ], -}; +const config = getBaseConfig(); +config.documents = './src/**/*.graphql'; export default config;