Skip to content

Commit be69e9d

Browse files
authored
[graphql-modules-preset] fix: skip sources without a location or document under strict mode (#4683) (#10993)
1 parent 024fa34 commit be69e9d

6 files changed

Lines changed: 21 additions & 5 deletions

File tree

‎.changeset/brave-modules-skip.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/graphql-modules-preset': patch
3+
---
4+
5+
Skip sources without a `location` when grouping sources by module, instead of throwing from `path.normalize`, and skip sources without a `document` AST when building each module's document. Generated output from the CLI is unchanged.

‎.changeset/wide-config-values.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': patch
3+
---
4+
5+
Type `getConfigValue`'s `value` parameter as `T | null | undefined`, so the result is non-nullable when a non-nullable default is given.

‎packages/plugins/other/visitor-plugin-common/src/utils.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import type {
3737
ScalarsMap,
3838
} from './types.js';
3939

40-
export const getConfigValue = <T = any>(value: T, defaultValue: T): T => {
40+
export const getConfigValue = <T = any>(value: T | null | undefined, defaultValue: T): T => {
4141
if (value === null || value === undefined) {
4242
return defaultValue;
4343
}

‎packages/presets/graphql-modules/src/builder.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ export function buildModule(
439439
defined.objects.push(name);
440440
collectFields(node, picks.objects);
441441

442-
if (node.interfaces?.length > 0) {
442+
if ((node.interfaces?.length ?? 0) > 0) {
443443
withIsTypeOf.objects.push(name);
444444
}
445445

@@ -495,7 +495,7 @@ export function buildModule(
495495

496496
pushUnique(extended.objects, name);
497497

498-
if (node.interfaces?.length > 0) {
498+
if ((node.interfaces?.length ?? 0) > 0) {
499499
pushUnique(withIsTypeOf.objects, name);
500500
}
501501

‎packages/presets/graphql-modules/src/index.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ export const preset: Types.OutputPreset<ModulesConfig> = {
9494
normalize(join(relativePath, baseTypesFilename));
9595
const sources = sourcesByModuleMap[moduleName];
9696

97-
const moduleDocument = concatAST(sources.map(source => source.document));
97+
const moduleDocument = concatAST(
98+
sources.flatMap(source => (source.document ? [source.document] : [])),
99+
);
98100

99101
const shouldDeclare = filename.endsWith('.d.ts');
100102

@@ -125,7 +127,7 @@ export const preset: Types.OutputPreset<ModulesConfig> = {
125127
schema.getQueryType()?.name,
126128
schema.getMutationType()?.name,
127129
schema.getSubscriptionType()?.name,
128-
].filter(Boolean),
130+
].filter((name): name is string => typeof name === 'string'),
129131
useTypeImports,
130132
}),
131133
},

‎packages/presets/graphql-modules/src/utils.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ export function groupSourcesByModule(
182182
const grouped: Record<string, Source[]> = {};
183183

184184
for (const source of sources) {
185+
if (!source.location) {
186+
continue;
187+
}
188+
185189
const relativePath = getRelativePath(source.location, basePath);
186190

187191
if (relativePath) {

0 commit comments

Comments
 (0)