From 260df8036a07b5f5b004c6b8f0b70df423128d92 Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Tue, 9 Apr 2019 13:15:48 +0200 Subject: [PATCH 1/3] Allow to use {T} in mappers --- .../tests/mapping.spec.ts | 57 ++++++++++++++----- .../src/base-resolvers-visitor.ts | 29 ++++++++-- 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/packages/plugins/typescript-resolvers/tests/mapping.spec.ts b/packages/plugins/typescript-resolvers/tests/mapping.spec.ts index 75c866f1de5..b4322609349 100644 --- a/packages/plugins/typescript-resolvers/tests/mapping.spec.ts +++ b/packages/plugins/typescript-resolvers/tests/mapping.spec.ts @@ -132,16 +132,16 @@ describe('ResolversTypes', () => { export type ResolversTypes = { Query: Partial, MyType: Partial, - String: Scalars['String'], + String: Partial, MyOtherType: Partial, Subscription: Partial, - Boolean: Scalars['Boolean'], + Boolean: Partial, Node: Partial, - ID: Scalars['ID'], + ID: Partial, SomeNode: Partial, MyUnion: Partial, - MyScalar: Scalars['MyScalar'], - Int: Scalars['Int'], + MyScalar: Partial, + Int: Partial, };`); }); @@ -161,16 +161,16 @@ describe('ResolversTypes', () => { export type ResolversTypes = { Query: CustomPartial, MyType: CustomPartial, - String: Scalars['String'], + String: CustomPartial, MyOtherType: CustomPartial, Subscription: CustomPartial, - Boolean: Scalars['Boolean'], + Boolean: CustomPartial, Node: CustomPartial, - ID: Scalars['ID'], + ID: CustomPartial, SomeNode: CustomPartial, MyUnion: CustomPartial, - MyScalar: Scalars['MyScalar'], - Int: Scalars['Int'], + MyScalar: CustomPartial, + Int: CustomPartial, };`); }); @@ -208,10 +208,10 @@ describe('ResolversTypes', () => { export type ResolversTypes = { Query: Partial & { me: Maybe }>, User: number, - ID: Scalars['ID'], - String: Scalars['String'], + ID: Partial, + String: Partial, Chat: Partial & { owner: ResolversTypes['User'], members: Maybe> }>, - Boolean: Scalars['Boolean'], + Boolean: Partial, }; `); @@ -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 & { something: ResolversTypes['MyType'] }, + MyType: Partial, + 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 */ ` diff --git a/packages/plugins/visitor-plugin-common/src/base-resolvers-visitor.ts b/packages/plugins/visitor-plugin-common/src/base-resolvers-visitor.ts index a9ba5067407..5258d044d96 100644 --- a/packages/plugins/visitor-plugin-common/src/base-resolvers-visitor.ts +++ b/packages/plugins/visitor-plugin-common/src/base-resolvers-visitor.ts @@ -180,12 +180,15 @@ export class BaseResolversVisitor { @@ -216,8 +220,13 @@ export class BaseResolversVisitor;` return null; } } + +function replacePlaceholder(pattern: string, typename: string): string { + return pattern.replace('{T}', typename); +} + +function hasPlaceholder(pattern: string): boolean { + return pattern.includes('{T}'); +} From ba6aa483bb959320ed35ab01adc129f693096d85 Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Tue, 9 Apr 2019 13:22:30 +0200 Subject: [PATCH 2/3] hasDefaultMapper --- .../visitor-plugin-common/src/base-resolvers-visitor.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/plugins/visitor-plugin-common/src/base-resolvers-visitor.ts b/packages/plugins/visitor-plugin-common/src/base-resolvers-visitor.ts index 5258d044d96..3e1584a4c5a 100644 --- a/packages/plugins/visitor-plugin-common/src/base-resolvers-visitor.ts +++ b/packages/plugins/visitor-plugin-common/src/base-resolvers-visitor.ts @@ -182,11 +182,12 @@ export class BaseResolversVisitor Date: Tue, 9 Apr 2019 13:34:46 +0200 Subject: [PATCH 3/3] Fix flow tests --- .../flow-resolvers/tests/mapping.spec.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/plugins/flow-resolvers/tests/mapping.spec.ts b/packages/plugins/flow-resolvers/tests/mapping.spec.ts index b4da573f499..79a20de5486 100644 --- a/packages/plugins/flow-resolvers/tests/mapping.spec.ts +++ b/packages/plugins/flow-resolvers/tests/mapping.spec.ts @@ -614,16 +614,16 @@ describe('ResolversTypes', () => { export type ResolversTypes = { Query: $Shape, MyType: $Shape, - String: $ElementType, + String: $Shape<$ElementType>, MyOtherType: $Shape, Subscription: $Shape, - Boolean: $ElementType, + Boolean: $Shape<$ElementType>, Node: $Shape, - ID: $ElementType, + ID: $Shape<$ElementType>, SomeNode: $Shape, MyUnion: $Shape, - MyScalar: $ElementType, - Int: $ElementType, + MyScalar: $Shape<$ElementType>, + Int: $Shape<$ElementType>, };`); }); @@ -642,16 +642,16 @@ describe('ResolversTypes', () => { export type ResolversTypes = { Query: CustomPartial, MyType: CustomPartial, - String: $ElementType, + String: CustomPartial<$ElementType>, MyOtherType: CustomPartial, Subscription: CustomPartial, - Boolean: $ElementType, + Boolean: CustomPartial<$ElementType>, Node: CustomPartial, - ID: $ElementType, + ID: CustomPartial<$ElementType>, SomeNode: CustomPartial, MyUnion: CustomPartial, - MyScalar: $ElementType, - Int: $ElementType, + MyScalar: CustomPartial<$ElementType>, + Int: CustomPartial<$ElementType>, };`); }); });