Skip to content

Commit

Permalink
(plugin/GraphQL Codegen): Support full name for plugin and preset (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
taro-28 authored Jul 13, 2024
1 parent c35bad7 commit 1562b3d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/knip/fixtures/plugins/graphql-codegen/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ module.exports = {
overwrite: true,
generates: {
'./graphql.schema.json': {
plugins: ['introspection'],
plugins: ['introspection', 'graphql-codegen-typescript-validation-schema'],
},
'./graphql.schema.graphql': {
'schema-ast': {},
},
'./src/generated/graphql.ts': {
documents: ['./src/**/*.tsx'],
preset: '@graphql-codegen/graphql-modules-preset',
plugins: ['typescript', 'typescript-operations', 'typescript-urql'],
},
'./lib/': {
Expand Down
14 changes: 11 additions & 3 deletions packages/knip/src/plugins/graphql-codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,20 @@ const resolveConfig: ResolveConfig<GraphqlCodegenTypes | GraphqlConfigTypes | Gr
const presets = configurationOutput
.map(configOutput => (configOutput.preset ? configOutput.preset : undefined))
.filter((preset): preset is PresetNames => typeof preset === 'string')
.map(presetName => `@graphql-codegen/${presetName}${presetName.endsWith('-preset') ? '' : '-preset'}`);
.map(presetName =>
// https://github.com/dotansimha/graphql-code-generator/blob/master/packages/graphql-codegen-cli/src/presets.ts#L8-L11
presetName.startsWith('@graphql-codegen/')
? presetName
: `@graphql-codegen/${presetName}${presetName.endsWith('-preset') ? '' : '-preset'}`
);

const flatPlugins = generateSet
.filter((config): config is ConfiguredPlugin => !isConfigurationOutput(config))
.flatMap(item => Object.keys(item))
.map(plugin => `@graphql-codegen/${plugin}`);
.map(plugin =>
// https://github.com/dotansimha/graphql-code-generator/blob/master/packages/graphql-codegen-cli/src/plugins.ts#L8-L18
plugin.includes('codegen-') ? plugin : `@graphql-codegen/${plugin}`
);

const nestedPlugins = configurationOutput
.flatMap(configOutput => (configOutput.plugins ? configOutput.plugins : []))
Expand All @@ -70,7 +78,7 @@ const resolveConfig: ResolveConfig<GraphqlCodegenTypes | GraphqlConfigTypes | Gr
.flatMap(plugin => {
if (typeof plugin !== 'string') return [];
if (isInternal(plugin)) return [toEntryPattern(plugin)];
return [`@graphql-codegen/${plugin}`];
return [plugin.includes('codegen-') ? plugin : `@graphql-codegen/${plugin}`];
});

return [...presets, ...flatPlugins, ...nestedPlugins];
Expand Down
4 changes: 3 additions & 1 deletion packages/knip/test/plugins/graphql-codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ test('Find dependencies with the graphql-codegen plugin (codegen.ts function)',
assert(issues.unlisted['codegen.ts']['@graphql-codegen/typescript-operations']);
assert(issues.unlisted['codegen.ts']['@graphql-codegen/typescript-urql']);
assert(issues.unlisted['codegen.ts']['@graphql-codegen/typescript-msw']);
assert(issues.unlisted['codegen.ts']['@graphql-codegen/graphql-modules-preset']);
assert(issues.unlisted['codegen.ts']['graphql-codegen-typescript-validation-schema']);

assert(issues.unlisted['codegen.yaml']['@graphql-codegen/add']);
assert(issues.unlisted['codegen.yaml']['@graphql-codegen/typescript']);
Expand All @@ -31,7 +33,7 @@ test('Find dependencies with the graphql-codegen plugin (codegen.ts function)',

assert.deepEqual(counters, {
...baseCounters,
unlisted: 13,
unlisted: 15,
processed: 1,
total: 1,
});
Expand Down

0 comments on commit 1562b3d

Please sign in to comment.