Skip to content

Commit ee83151

Browse files
committedNov 16, 2023
Fix up plugin resolving in graphql-codegen plugin a bit (resolves #349)
1 parent 72b3add commit ee83151

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
generates:
2+
'../../packages/graphql-types/src/types.ts':
3+
config:
4+
documents:
5+
'src/documents/**/*.graphql':
6+
skipGraphQLImport: false
7+
plugins:
8+
- typescript
9+
- typescript-operations
10+
- typed-document-node
11+
- ./codegen/UserErrorsPlugin.cjs
12+
- add:
13+
content: "/* cSpell:disable */\nimport { type SerializedMoney } from '@contra/money';"
14+
'src/contra-api/__generated__/types.ts':
15+
config:
16+
contextType: '../types.js#ResolverContext'
17+
mappers:
18+
ActivePaidProjectAnalytics: '../types.js#ActivePaidProjectAnalytics'
19+
Workspace: '@contra/temporary-common/domain/workspace/WorkspaceService.js#Workspace'
20+
plugins:
21+
- typescript
22+
- typescript-resolvers
23+
- ./codegen/UserErrorsPlugin.cjs
24+
- ./codegen/GraphQLResolverBuilderPlugin.cjs
25+
- add:
26+
content:
27+
"/* cSpell:disable */\nimport type { DeepPartial } from 'utility-types';\nimport { type Money as
28+
ContraMoney, type USD } from '@contra/money';"
29+
schema:
30+
- node_modules/@contra/graphql-schema/index.js

‎src/plugins/graphql-codegen/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { isInternal } from '../../util/path.js';
12
import { timerify } from '../../util/Performance.js';
23
import { hasDependency, load } from '../../util/plugin.js';
4+
import { toEntryPattern } from '../../util/protocols.js';
35
import { isConfigurationOutput } from './types.js';
46
import type { ConfiguredPlugin, GraphqlCodegenTypes, PresetNames } from './types.js';
57
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';
@@ -50,7 +52,11 @@ const findPluginDependencies: GenericPluginCallback = async (configFilePath, opt
5052

5153
const nestedPlugins = configurationOutput
5254
.flatMap(configOutput => (configOutput.plugins ? configOutput.plugins : []))
53-
.map(plugin => `@graphql-codegen/${plugin}`);
55+
.flatMap(plugin => {
56+
if (typeof plugin !== 'string') return [];
57+
if (isInternal(plugin)) return [toEntryPattern(plugin)];
58+
return [`@graphql-codegen/${plugin}`];
59+
});
5460

5561
return [...presets, ...flatPlugins, ...nestedPlugins];
5662
};

‎test/plugins/graphql-codegen.test.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ test('Find dependencies in graphql-codegen configuration (codegen.ts)', async ()
3131
]);
3232
});
3333

34+
test('Find dependencies in graphql-codegen configuration (codegen.yaml)', async () => {
35+
const configFilePath = join(cwd, 'codegen.yaml');
36+
const dependencies = await graphqlCodegen.findDependencies(configFilePath, { manifest, config });
37+
assert.deepEqual(dependencies, [
38+
'@graphql-codegen/typescript',
39+
'@graphql-codegen/typescript-operations',
40+
'@graphql-codegen/typed-document-node',
41+
'entry:./codegen/UserErrorsPlugin.cjs',
42+
'@graphql-codegen/typescript',
43+
'@graphql-codegen/typescript-resolvers',
44+
'entry:./codegen/UserErrorsPlugin.cjs',
45+
'entry:./codegen/GraphQLResolverBuilderPlugin.cjs',
46+
]);
47+
});
48+
3449
test('Find dependencies in graphql-codegen configuration (codegen.ts function)', async () => {
3550
const { issues, counters } = await main({
3651
...baseArguments,
@@ -44,11 +59,17 @@ test('Find dependencies in graphql-codegen configuration (codegen.ts function)',
4459
assert(issues.unlisted['codegen.ts']['@graphql-codegen/typescript-operations']);
4560
assert(issues.unlisted['codegen.ts']['@graphql-codegen/typescript-urql']);
4661
assert(issues.unlisted['codegen.ts']['@graphql-codegen/typescript-msw']);
62+
63+
// TODO split in separate fixtures/dirs
64+
assert(issues.unlisted['codegen.yaml']['@graphql-codegen/typescript']);
65+
assert(issues.unlisted['codegen.yaml']['@graphql-codegen/typescript-operations']);
66+
assert(issues.unlisted['codegen.yaml']['@graphql-codegen/typed-document-node']);
67+
assert(issues.unlisted['codegen.yaml']['@graphql-codegen/typescript-resolvers']);
4768
assert(issues.unlisted['package.json']['@graphql-codegen/client-preset']);
4869

4970
assert.deepEqual(counters, {
5071
...baseCounters,
51-
unlisted: 8,
72+
unlisted: 12,
5273
devDependencies: 1,
5374
processed: 1,
5475
total: 1,

3 commit comments

Comments
 (3)

CensoredUser commented on Jun 21, 2024

@CensoredUser
Contributor

This test added a case that knip is currently not handling: @graphql-codegen/add should be recognized as used/unlisted in codegen.yaml.
It currently doesn't, because the add plugin defines plugin specific configuration, and is thus not a string, but an object, in the plugins array.

webpro commented on Jun 21, 2024

@webpro
MemberAuthor

You up for a PR? Otherwise I'll pick it up after the weekend.

CensoredUser commented on Jun 21, 2024

@CensoredUser
Contributor

@webpro I just created #692 to track this issue - I'll work on a PR :)

Please sign in to comment.