From 4f9c6a94e34fb82d7ae0e38132849bd275a24f37 Mon Sep 17 00:00:00 2001 From: Jason Kuhrt Date: Fri, 8 Feb 2019 22:16:04 -0500 Subject: [PATCH 1/2] feat: make apollo iresolvers compatible with gg resolvers closes #15 --- docs/01-configuration.md | 1 + .../graphqlgen-json-schema/src/definition.ts | 1 + .../src/generators/typescript/generator.ts | 27 +++++++++++++++++++ packages/graphqlgen/src/index.ts | 4 +++ packages/graphqlgen/src/types.ts | 1 + 5 files changed, 34 insertions(+) diff --git a/docs/01-configuration.md b/docs/01-configuration.md index d95a1222..a1329f32 100644 --- a/docs/01-configuration.md +++ b/docs/01-configuration.md @@ -15,6 +15,7 @@ The configuration file must be called **`graphqlgen.yml`**. - `output`: Specifies where the scaffolded resolvers should be located. Must point to a **directory**. - `layout`: Specifies the [_layout_](#layouts) for the generated files. Possible values: `file-per-type` (more layouts [coming soon](https://github.com/prisma/graphqlgen/issues/106): `single-file`, `file-per-type-classes`, `single-file-classes`). - `default-resolvers`: A boolean dictating if default resolvers will be generated or not. Defaults to `true`. +- `iresolvers-augmentation`: A boolean dictating if Apollo Server IResolvers type should be augmented so that it is compatible with graphqlgen `Resolvers` type. Defaults to `true`. Whether a property is required or not depends on whether you're doing [Generation](#generation) or [Scaffolding](#scaffolding). diff --git a/packages/graphqlgen-json-schema/src/definition.ts b/packages/graphqlgen-json-schema/src/definition.ts index 6f0def1e..48bc2b5a 100644 --- a/packages/graphqlgen-json-schema/src/definition.ts +++ b/packages/graphqlgen-json-schema/src/definition.ts @@ -6,6 +6,7 @@ export interface GraphQLGenDefinition { output: string ['resolver-scaffolding']?: ResolverScaffolding ['default-resolvers']?: boolean + ['iresolvers-augmentation']?: boolean } export interface Models { diff --git a/packages/graphqlgen/src/generators/typescript/generator.ts b/packages/graphqlgen/src/generators/typescript/generator.ts index c095b58e..a50fe15f 100644 --- a/packages/graphqlgen/src/generators/typescript/generator.ts +++ b/packages/graphqlgen/src/generators/typescript/generator.ts @@ -98,6 +98,33 @@ export function generate(args: GenerateArgs): string { ${renderResolvers(args)} + ${ + args.iResolversAugmentationEnabled + ? renderGraphqlToolsModuleAugmentationIResolvers() + : '' + } + + ` +} + +/** + * This renders a TypeScript module augmentation against graphql-tools + * IResolvers type. Apollo Server uses that type to type its resolvers. + * The problem with that type is that it is very loose compared to + * graphqlgen including being an index type. The index type in particular + * breaks compatibility with the resolvers generated by graphqlgen. We + * fix this by augmenting the IResolvers type. + * + * References: + * + * - https://www.typescriptlang.org/docs/handbook/declaration-merging.html + * - https://github.com/prisma/graphqlgen/issues/15 + */ +const renderGraphqlToolsModuleAugmentationIResolvers = (): string => { + return ` + declare module "graphql-tools" { + interface IResolvers extends Resolvers {} + } ` } diff --git a/packages/graphqlgen/src/index.ts b/packages/graphqlgen/src/index.ts index 4921171f..c721482f 100644 --- a/packages/graphqlgen/src/index.ts +++ b/packages/graphqlgen/src/index.ts @@ -105,6 +105,10 @@ export function generateCode(codeGenArgs: CodeGenArgs): CodeGenResult { typeof codeGenArgs.config['default-resolvers'] === 'boolean' ? codeGenArgs.config['default-resolvers'] : true, + iResolversAugmentationEnabled: + typeof codeGenArgs.config['iresolvers-augmentation'] === 'boolean' + ? codeGenArgs.config['iresolvers-augmentation'] + : true, } const generatedTypes = generateTypes(generateArgs, codeGenArgs) diff --git a/packages/graphqlgen/src/types.ts b/packages/graphqlgen/src/types.ts index 0b3cbbb6..5d7c0e02 100644 --- a/packages/graphqlgen/src/types.ts +++ b/packages/graphqlgen/src/types.ts @@ -15,6 +15,7 @@ export interface GenerateArgs { context?: ContextDefinition modelMap: ModelMap defaultResolversEnabled: boolean + iResolversAugmentationEnabled: boolean } export interface ModelMap { From 10897ea64122324ce237e7c6f91b00d297dbcf0e Mon Sep 17 00:00:00 2001 From: Jason Kuhrt Date: Fri, 8 Feb 2019 22:25:25 -0500 Subject: [PATCH 2/2] fix tests --- .../src/generators/typescript/generator.ts | 5 ++ .../__snapshots__/basic.test.ts.snap | 50 +++++++++++++++++++ .../__snapshots__/large-schema.test.ts.snap | 5 ++ 3 files changed, 60 insertions(+) diff --git a/packages/graphqlgen/src/generators/typescript/generator.ts b/packages/graphqlgen/src/generators/typescript/generator.ts index a50fe15f..8de681cd 100644 --- a/packages/graphqlgen/src/generators/typescript/generator.ts +++ b/packages/graphqlgen/src/generators/typescript/generator.ts @@ -121,7 +121,12 @@ export function generate(args: GenerateArgs): string { * - https://github.com/prisma/graphqlgen/issues/15 */ const renderGraphqlToolsModuleAugmentationIResolvers = (): string => { + // Use ts-ignore otherwise tests will throw an error about no such + // module being found. Further, if a user for some reason is not using + // Apollo Server, then this augmentation doesn't matter anyways, and + // should not throw an exception for them either. return ` + // @ts-ignore declare module "graphql-tools" { interface IResolvers extends Resolvers {} } diff --git a/packages/graphqlgen/tests/typescript/__snapshots__/basic.test.ts.snap b/packages/graphqlgen/tests/typescript/__snapshots__/basic.test.ts.snap index 41a7de43..da4646ab 100644 --- a/packages/graphqlgen/tests/typescript/__snapshots__/basic.test.ts.snap +++ b/packages/graphqlgen/tests/typescript/__snapshots__/basic.test.ts.snap @@ -205,6 +205,11 @@ export interface Resolvers { Query: QueryResolvers.Type; User: UserResolvers.Type; } + +// @ts-ignore +declare module \\"graphql-tools\\" { + interface IResolvers extends Resolvers {} +} " `; @@ -455,6 +460,11 @@ export interface Resolvers { Mutation: MutationResolvers.Type; AddMemberPayload: AddMemberPayloadResolvers.Type; } + +// @ts-ignore +declare module \\"graphql-tools\\" { + interface IResolvers extends Resolvers {} +} " `; @@ -935,6 +945,11 @@ export interface Resolvers { Video: VideoResolvers.Type; Media?: MediaResolvers.Type; } + +// @ts-ignore +declare module \\"graphql-tools\\" { + interface IResolvers extends Resolvers {} +} " `; @@ -1156,6 +1171,11 @@ export interface Resolvers { Mutation: MutationResolvers.Type; AddMemberPayload: AddMemberPayloadResolvers.Type; } + +// @ts-ignore +declare module \\"graphql-tools\\" { + interface IResolvers extends Resolvers {} +} " `; @@ -1775,6 +1795,11 @@ export interface Resolvers { Query: QueryResolvers.Type; Number: NumberResolvers.Type; } + +// @ts-ignore +declare module \\"graphql-tools\\" { + interface IResolvers extends Resolvers {} +} " `; @@ -2098,6 +2123,11 @@ export interface Resolvers { User: UserResolvers.Type; UserType?: UserTypeResolvers.Type; } + +// @ts-ignore +declare module \\"graphql-tools\\" { + interface IResolvers extends Resolvers {} +} " `; @@ -2289,6 +2319,11 @@ export interface Resolvers { Query: QueryResolvers.Type; User: UserResolvers.Type; } + +// @ts-ignore +declare module \\"graphql-tools\\" { + interface IResolvers extends Resolvers {} +} " `; @@ -2919,6 +2954,11 @@ export interface Resolvers { Query: QueryResolvers.Type; Number: NumberResolvers.Type; } + +// @ts-ignore +declare module \\"graphql-tools\\" { + interface IResolvers extends Resolvers {} +} " `; @@ -3574,6 +3614,11 @@ export interface Resolvers { Query: QueryResolvers.Type; Number: NumberResolvers.Type; } + +// @ts-ignore +declare module \\"graphql-tools\\" { + interface IResolvers extends Resolvers {} +} " `; @@ -3753,6 +3798,11 @@ export interface Resolvers { Subscription: SubscriptionResolvers.Type; User: UserResolvers.Type; } + +// @ts-ignore +declare module \\"graphql-tools\\" { + interface IResolvers extends Resolvers {} +} " `; diff --git a/packages/graphqlgen/tests/typescript/__snapshots__/large-schema.test.ts.snap b/packages/graphqlgen/tests/typescript/__snapshots__/large-schema.test.ts.snap index 1c51ad24..ce2a4fba 100644 --- a/packages/graphqlgen/tests/typescript/__snapshots__/large-schema.test.ts.snap +++ b/packages/graphqlgen/tests/typescript/__snapshots__/large-schema.test.ts.snap @@ -8530,6 +8530,11 @@ export interface Resolvers { AuthPayload: AuthPayloadResolvers.Type; MutationResult: MutationResultResolvers.Type; } + +// @ts-ignore +declare module \\"graphql-tools\\" { + interface IResolvers extends Resolvers {} +} " `;