Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: codegen config schema property moved to the codegen config package. #45

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions examples/nextjs/starter/codegen.ts
Original file line number Diff line number Diff line change
@@ -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 }`,
},
},
},
],
};
Comment on lines -14 to -25
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that we've removed the entire schema.

There are many reasons that a project will need to change this, e.g. to change/add headers. (it's also one of the reasons I think the generateGraphQLUrl function should be removed).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still an option to override this by passing it as the config in withCodegenConfig function

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but a user would need to override the entire property...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give me an example of what you mean by an entire property?

If you say they cannot override the schema property completely, they can. However, they won't be able to keep the schema property as our default and only change the header.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#45 (comment)

If I needed to set the origin header in my local project, I would need to:

  • navigate the monorepo to find the defaults.
  • copy the defaults into my local codegen config
  • add my origin header.
  • actively monitor or changes to the default codegen config.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still don't like this. I feel like there is a difference between defaults and abstractions, and we really don't want to be creating an abstraction.

Will defer to @ayushnirwal

const config = getBaseConfig();

if ( graphqlFiles.length > 0 ) {
config.documents = GRAPHQL_GLOB;
}

export default config;
49 changes: 37 additions & 12 deletions packages/codegen-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 };
23 changes: 3 additions & 20 deletions packages/query/codegen.ts
Original file line number Diff line number Diff line change
@@ -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;
Loading