Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use {T} in mappers #1677

Merged
merged 3 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/plugins/flow-resolvers/tests/mapping.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,16 +614,16 @@ describe('ResolversTypes', () => {
export type ResolversTypes = {
Query: $Shape<Query>,
MyType: $Shape<MyType>,
String: $ElementType<Scalars, 'String'>,
String: $Shape<$ElementType<Scalars, 'String'>>,
MyOtherType: $Shape<MyOtherType>,
Subscription: $Shape<Subscription>,
Boolean: $ElementType<Scalars, 'Boolean'>,
Boolean: $Shape<$ElementType<Scalars, 'Boolean'>>,
Node: $Shape<Node>,
ID: $ElementType<Scalars, 'ID'>,
ID: $Shape<$ElementType<Scalars, 'ID'>>,
SomeNode: $Shape<SomeNode>,
MyUnion: $Shape<MyUnion>,
MyScalar: $ElementType<Scalars, 'MyScalar'>,
Int: $ElementType<Scalars, 'Int'>,
MyScalar: $Shape<$ElementType<Scalars, 'MyScalar'>>,
Int: $Shape<$ElementType<Scalars, 'Int'>>,
};`);
});

Expand All @@ -642,16 +642,16 @@ describe('ResolversTypes', () => {
export type ResolversTypes = {
Query: CustomPartial<Query>,
MyType: CustomPartial<MyType>,
String: $ElementType<Scalars, 'String'>,
String: CustomPartial<$ElementType<Scalars, 'String'>>,
MyOtherType: CustomPartial<MyOtherType>,
Subscription: CustomPartial<Subscription>,
Boolean: $ElementType<Scalars, 'Boolean'>,
Boolean: CustomPartial<$ElementType<Scalars, 'Boolean'>>,
Node: CustomPartial<Node>,
ID: $ElementType<Scalars, 'ID'>,
ID: CustomPartial<$ElementType<Scalars, 'ID'>>,
SomeNode: CustomPartial<SomeNode>,
MyUnion: CustomPartial<MyUnion>,
MyScalar: $ElementType<Scalars, 'MyScalar'>,
Int: $ElementType<Scalars, 'Int'>,
MyScalar: CustomPartial<$ElementType<Scalars, 'MyScalar'>>,
Int: CustomPartial<$ElementType<Scalars, 'Int'>>,
};`);
});
});
57 changes: 44 additions & 13 deletions packages/plugins/typescript-resolvers/tests/mapping.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,16 @@ describe('ResolversTypes', () => {
export type ResolversTypes = {
Query: Partial<Query>,
MyType: Partial<MyType>,
String: Scalars['String'],
String: Partial<Scalars['String']>,
dotansimha marked this conversation as resolved.
Show resolved Hide resolved
MyOtherType: Partial<MyOtherType>,
Subscription: Partial<Subscription>,
Boolean: Scalars['Boolean'],
Boolean: Partial<Scalars['Boolean']>,
Node: Partial<Node>,
ID: Scalars['ID'],
ID: Partial<Scalars['ID']>,
SomeNode: Partial<SomeNode>,
MyUnion: Partial<MyUnion>,
MyScalar: Scalars['MyScalar'],
Int: Scalars['Int'],
MyScalar: Partial<Scalars['MyScalar']>,
Int: Partial<Scalars['Int']>,
};`);
});

Expand All @@ -161,16 +161,16 @@ describe('ResolversTypes', () => {
export type ResolversTypes = {
Query: CustomPartial<Query>,
MyType: CustomPartial<MyType>,
String: Scalars['String'],
String: CustomPartial<Scalars['String']>,
MyOtherType: CustomPartial<MyOtherType>,
Subscription: CustomPartial<Subscription>,
Boolean: Scalars['Boolean'],
Boolean: CustomPartial<Scalars['Boolean']>,
Node: CustomPartial<Node>,
ID: Scalars['ID'],
ID: CustomPartial<Scalars['ID']>,
SomeNode: CustomPartial<SomeNode>,
MyUnion: CustomPartial<MyUnion>,
MyScalar: Scalars['MyScalar'],
Int: Scalars['Int'],
MyScalar: CustomPartial<Scalars['MyScalar']>,
Int: CustomPartial<Scalars['Int']>,
};`);
});

Expand Down Expand Up @@ -208,10 +208,10 @@ describe('ResolversTypes', () => {
export type ResolversTypes = {
Query: Partial<Omit<Query, 'me'> & { me: Maybe<ResolversTypes['User']> }>,
User: number,
ID: Scalars['ID'],
String: Scalars['String'],
ID: Partial<Scalars['ID']>,
String: Partial<Scalars['String']>,
Chat: Partial<Omit<Chat, 'owner' | 'members'> & { owner: ResolversTypes['User'], members: Maybe<Array<ResolversTypes['User']>> }>,
Boolean: Scalars['Boolean'],
Boolean: Partial<Scalars['Boolean']>,
};
`);

Expand Down Expand Up @@ -311,6 +311,37 @@ describe('ResolversTypes', () => {
};`);
});

it('Should handle {T} in a mapper', async () => {
const result = await plugin(
schema,
[],
{
noSchemaStitching: true,
mappers: {
MyType: 'Partial<{T}>',
},
},
{ outputFile: '' }
);

expect(result).toBeSimilarStringTo(`
export type ResolversTypes = {
Query: Omit<Query, 'something'> & { something: ResolversTypes['MyType'] },
MyType: Partial<MyType>,
String: Scalars['String'],
MyOtherType: MyOtherType,
Subscription: Subscription,
Boolean: Scalars['Boolean'],
Node: Node,
ID: Scalars['ID'],
SomeNode: SomeNode,
MyUnion: MyUnion,
MyScalar: Scalars['MyScalar'],
Int: Scalars['Int'],
};
`);
});

it('should warn about unused mappers by default', async () => {
const spy = jest.spyOn(console, 'warn').mockImplementation();
const testSchema = buildSchema(/* GraphQL */ `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,25 @@ export class BaseResolversVisitor<TRawConfig extends RawResolversConfig = RawRes

let shouldApplyOmit = false;

if (this.config.mappers[typeName] && this.config.mappers[typeName].type) {
const isMapped = this.config.mappers[typeName];
const isScalar = this.config.scalars[typeName];
const hasDefaultMapper = !!(this.config.defaultMapper && this.config.defaultMapper.type);

if (isMapped && this.config.mappers[typeName].type) {
this.markMapperAsUsed(typeName);
prev[typeName] = this.config.mappers[typeName].type;
} else if (this.config.defaultMapper && this.config.defaultMapper.type && !this.config.defaultMapper.type.includes('{T}')) {
} else if (hasDefaultMapper && !hasPlaceholder(this.config.defaultMapper.type)) {
prev[typeName] = this.config.defaultMapper.type;
} else if (this.config.scalars[typeName]) {
} else if (isScalar) {
prev[typeName] = this._getScalar(typeName);
} else {
shouldApplyOmit = true;
prev[typeName] = this.convertName(typeName);
}

const schemaType = allSchemaTypes[typeName];
if ((shouldApplyOmit && prev[typeName] !== 'any' && isObjectType(schemaType)) || (isInterfaceType(schemaType) && !this.config.mappers[typeName])) {

if ((shouldApplyOmit && prev[typeName] !== 'any' && isObjectType(schemaType)) || (isInterfaceType(schemaType) && !isMapped)) {
const fields = schemaType.getFields();
const relevantFields = Object.keys(fields)
.map(fieldName => {
Expand All @@ -216,8 +221,13 @@ export class BaseResolversVisitor<TRawConfig extends RawResolversConfig = RawRes
}
}

if (!this.config.scalars[typeName] && !this.config.mappers[typeName] && this.config.defaultMapper && this.config.defaultMapper.type.includes('{T}')) {
prev[typeName] = this.config.defaultMapper.type.replace('{T}', prev[typeName]);
if (isMapped && hasPlaceholder(prev[typeName])) {
prev[typeName] = replacePlaceholder(prev[typeName], typeName);
}

if (!isMapped && hasDefaultMapper && hasPlaceholder(this.config.defaultMapper.type)) {
const name = isScalar ? this._getScalar(typeName) : prev[typeName];
prev[typeName] = replacePlaceholder(this.config.defaultMapper.type, name);
}

return prev;
Expand Down Expand Up @@ -589,3 +599,11 @@ export type IDirectiveResolvers${contextType} = ${name}<Context>;`
return null;
}
}

function replacePlaceholder(pattern: string, typename: string): string {
return pattern.replace('{T}', typename);
}

function hasPlaceholder(pattern: string): boolean {
return pattern.includes('{T}');
}