Skip to content

Commit

Permalink
Custom mappers w. placeholder will apply omit (#9110)
Browse files Browse the repository at this point in the history
* Custom mappers w. placeholder will apply omit

* enhancements

* test fix

* changeset

* test fix
  • Loading branch information
gilgardosh authored Mar 6, 2023
1 parent af7db11 commit ba0610b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
6 changes: 6 additions & 0 deletions .changeset/beige-guests-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/visitor-plugin-common': patch
'@graphql-codegen/typescript-resolvers': patch
---

Custom mappers with placeholder will apply omit
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,6 @@ export class BaseResolversVisitor<
return prev;
}

let shouldApplyOmit = false;
const isRootType = this._rootTypeNames.has(typeName);
const isMapped = this.config.mappers[typeName];
const isScalar = this.config.scalars[typeName];
Expand All @@ -727,7 +726,7 @@ export class BaseResolversVisitor<

return prev;
}
if (isMapped && this.config.mappers[typeName].type) {
if (isMapped && this.config.mappers[typeName].type && !hasPlaceholder(this.config.mappers[typeName].type)) {
this.markMapperAsUsed(typeName);
prev[typeName] = applyWrapper(this.config.mappers[typeName].type);
} else if (isInterfaceType(schemaType)) {
Expand Down Expand Up @@ -763,28 +762,31 @@ export class BaseResolversVisitor<
} else if (isEnumType(schemaType)) {
prev[typeName] = this.convertName(typeName, { useTypesPrefix: this.config.enumPrefix }, true);
} else {
shouldApplyOmit = true;
prev[typeName] = this.convertName(typeName, {}, true);
}

if (shouldApplyOmit && prev[typeName] !== 'any' && isObjectType(schemaType)) {
const relevantFields = this.getRelevantFieldsToOmit({
schemaType,
getTypeToUse,
shouldInclude,
});
if (prev[typeName] !== 'any' && isObjectType(schemaType)) {
const relevantFields = this.getRelevantFieldsToOmit({
schemaType,
getTypeToUse,
shouldInclude,
});

if (relevantFields.length > 0) {
// Puts ResolverTypeWrapper on top of an entire type
prev[typeName] = applyWrapper(this.replaceFieldsInType(prev[typeName], relevantFields));
} else {
// We still want to use ResolverTypeWrapper, even if we don't touch any fields
prev[typeName] = applyWrapper(prev[typeName]);
}
}
// If relevantFields, puts ResolverTypeWrapper on top of an entire type
let internalType =
relevantFields.length > 0 ? this.replaceFieldsInType(prev[typeName], relevantFields) : prev[typeName];

if (isMapped && hasPlaceholder(prev[typeName])) {
prev[typeName] = replacePlaceholder(prev[typeName], typeName);
if (isMapped) {
// replace the placeholder with the actual type
if (hasPlaceholder(internalType)) {
internalType = replacePlaceholder(internalType, typeName);
}
if (this.config.mappers[typeName].type && hasPlaceholder(this.config.mappers[typeName].type)) {
internalType = replacePlaceholder(this.config.mappers[typeName].type, internalType);
}
}

prev[typeName] = applyWrapper(internalType);
}
}

if (!isMapped && hasDefaultMapper && hasPlaceholder(this.config.defaultMapper.type)) {
Expand Down
10 changes: 5 additions & 5 deletions packages/plugins/typescript/resolvers/tests/mapping.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ describe('ResolversTypes', () => {
`);
expect(result.content).toBeSimilarStringTo(`
export type ResolversTypes = {
MyType: ResolverTypeWrapper<CustomPartial<MyType>>;
MyType: ResolverTypeWrapper<CustomPartial<Omit<MyType, 'unionChild'> & { unionChild?: Maybe<ResolversTypes['ChildUnion']> }>>;
String: ResolverTypeWrapper<Scalars['String']>;
Child: ResolverTypeWrapper<Omit<Child, 'parent'> & { parent?: Maybe<ResolversTypes['MyType']> }>;
MyOtherType: ResolverTypeWrapper<MyOtherType>;
Expand All @@ -490,7 +490,7 @@ describe('ResolversTypes', () => {
};`);
expect(result.content).toBeSimilarStringTo(`
export type ResolversParentTypes = {
MyType: CustomPartial<MyType>;
MyType: CustomPartial<Omit<MyType, 'unionChild'> & { unionChild?: Maybe<ResolversParentTypes['ChildUnion']> }>;
String: Scalars['String'];
Child: Omit<Child, 'parent'> & { parent?: Maybe<ResolversParentTypes['MyType']> };
MyOtherType: MyOtherType;
Expand Down Expand Up @@ -887,7 +887,7 @@ describe('ResolversTypes', () => {
`);
expect(result.content).toBeSimilarStringTo(`
export type ResolversTypes = {
MyType: ResolverTypeWrapper<Partial<MyType>>;
MyType: ResolverTypeWrapper<Partial<Omit<MyType, 'unionChild'> & { unionChild?: Maybe<ResolversTypes['ChildUnion']> }>>;
String: ResolverTypeWrapper<Scalars['String']>;
Child: ResolverTypeWrapper<Omit<Child, 'parent'> & { parent?: Maybe<ResolversTypes['MyType']> }>;
MyOtherType: ResolverTypeWrapper<MyOtherType>;
Expand Down Expand Up @@ -1690,7 +1690,7 @@ describe('ResolversTypes', () => {
`);
expect(result.content).toBeSimilarStringTo(`
export type ResolversTypes = {
MyType: ResolverTypeWrapper<MyNamespace.MyType<MyType>>;
MyType: ResolverTypeWrapper<MyNamespace.MyType<Omit<MyType, 'unionChild'> & { unionChild?: Maybe<ResolversTypes['ChildUnion']> }>>;
String: ResolverTypeWrapper<MyNamespace.MyDefaultMapper<Scalars['String']>>;
Child: ResolverTypeWrapper<MyNamespace.MyDefaultMapper<Omit<Child, 'parent'> & { parent?: Maybe<ResolversTypes['MyType']> }>>;
MyOtherType: ResolverTypeWrapper<MyNamespace.MyDefaultMapper<MyOtherType>>;
Expand All @@ -1709,7 +1709,7 @@ describe('ResolversTypes', () => {

expect(result.content).toBeSimilarStringTo(`
export type ResolversParentTypes = {
MyType: MyNamespace.MyType<MyType>;
MyType: MyNamespace.MyType<Omit<MyType, 'unionChild'> & { unionChild?: Maybe<ResolversParentTypes['ChildUnion']> }>;
String: MyNamespace.MyDefaultMapper<Scalars['String']>;
Child: MyNamespace.MyDefaultMapper<Omit<Child, 'parent'> & { parent?: Maybe<ResolversParentTypes['MyType']> }>;
MyOtherType: MyNamespace.MyDefaultMapper<MyOtherType>;
Expand Down

0 comments on commit ba0610b

Please sign in to comment.