Skip to content

Commit 2fc3869

Browse files
Partially defined enumValue support (#10419)
* Add failing test for #10418 * Fix enum resolver for partially mapped enumValues * Add changeset * Missing queries in test * Prefer valueName over name node --------- Co-authored-by: Eddy Nguyen <[email protected]>
1 parent d54ad7f commit 2fc3869

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

.changeset/mighty-hornets-laugh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/typescript-resolvers': patch
3+
---
4+
5+
Fix enum resolver for partially mapped enumValues

packages/plugins/typescript/resolvers/src/visitor.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,12 @@ export class TypeScriptResolversVisitor extends BaseResolversVisitor<
134134
return `{ ${(node.values || [])
135135
.map(v => {
136136
const valueName = v.name as any as string;
137-
const mappedValue = valuesMapping[valueName];
137+
const mappedValue = valuesMapping[valueName] ?? valueName;
138+
const hasMapping = !!valuesMapping[valueName];
138139
139-
return `${valueName}: ${typeof mappedValue === 'number' ? mappedValue : `'${mappedValue}'`}`;
140+
return `${valueName}${hasMapping || this.config.avoidOptionals.resolvers ? '' : '?'}: ${
141+
typeof mappedValue === 'number' ? mappedValue : `'${mappedValue}'`
142+
}`;
140143
})
141144
.join(', ')} }`;
142145
}

packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,54 @@ __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
336336
expect(mergedOutput).toContain(`export type MyEnumResolvers = { A: 'val_1', B: 'val_2', C: 'val_3' };`);
337337
});
338338

339+
it('#10418 - Should generate enum internal values resolvers when enum has enumValues set as object with partially explicit values', async () => {
340+
const testSchema = buildSchema(/* GraphQL */ `
341+
type Query {
342+
v: MyEnum
343+
w: MyEnum
344+
x: MyEnum
345+
}
346+
347+
enum MyEnum {
348+
A
349+
B
350+
C
351+
}
352+
`);
353+
const config = {
354+
noSchemaStitching: true,
355+
enumValues: {
356+
MyEnum: {
357+
A: 'val_1',
358+
},
359+
},
360+
};
361+
const result = await plugin(testSchema, [], config, { outputFile: '' });
362+
363+
const mergedOutput = await resolversTestingValidate(
364+
result,
365+
config,
366+
testSchema,
367+
`
368+
export const resolvers: Resolvers = {
369+
MyEnum: {
370+
A: 'val_1',
371+
B: 'B'
372+
},
373+
Query: {
374+
v: () => 'val_1',
375+
w: () => 'B',
376+
z: () => 'C',
377+
}
378+
};
379+
`
380+
);
381+
382+
expect(mergedOutput).not.toContain(ENUM_RESOLVERS_SIGNATURE);
383+
expect(mergedOutput).not.toContain('EnumResolverSignature');
384+
expect(mergedOutput).toContain(`export type MyEnumResolvers = { A: 'val_1', B?: 'B', C?: 'C' };`);
385+
});
386+
339387
it('Should generate enum internal values resolvers when enum has enumValues set as external enum', async () => {
340388
const testSchema = buildSchema(/* GraphQL */ `
341389
type Query {

0 commit comments

Comments
 (0)