Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Commit

Permalink
feat: make apollo iresolvers compatible with gg resolvers (#444)
Browse files Browse the repository at this point in the history
closes #15
  • Loading branch information
Jason Kuhrt authored Feb 9, 2019
1 parent dcedf5f commit 6b2fdd7
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/01-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
1 change: 1 addition & 0 deletions packages/graphqlgen-json-schema/src/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface GraphQLGenDefinition {
output: string
['resolver-scaffolding']?: ResolverScaffolding
['default-resolvers']?: boolean
['iresolvers-augmentation']?: boolean
}

export interface Models {
Expand Down
32 changes: 32 additions & 0 deletions packages/graphqlgen/src/generators/typescript/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,38 @@ 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 => {
// 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 {}
}
`
}

Expand Down
4 changes: 4 additions & 0 deletions packages/graphqlgen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions packages/graphqlgen/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface GenerateArgs {
context?: ContextDefinition
modelMap: ModelMap
defaultResolversEnabled: boolean
iResolversAugmentationEnabled: boolean
}

export interface ModelMap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ export interface Resolvers {
Query: QueryResolvers.Type;
User: UserResolvers.Type;
}

// @ts-ignore
declare module \\"graphql-tools\\" {
interface IResolvers extends Resolvers {}
}
"
`;

Expand Down Expand Up @@ -455,6 +460,11 @@ export interface Resolvers {
Mutation: MutationResolvers.Type;
AddMemberPayload: AddMemberPayloadResolvers.Type;
}

// @ts-ignore
declare module \\"graphql-tools\\" {
interface IResolvers extends Resolvers {}
}
"
`;

Expand Down Expand Up @@ -935,6 +945,11 @@ export interface Resolvers {
Video: VideoResolvers.Type;
Media?: MediaResolvers.Type;
}

// @ts-ignore
declare module \\"graphql-tools\\" {
interface IResolvers extends Resolvers {}
}
"
`;

Expand Down Expand Up @@ -1156,6 +1171,11 @@ export interface Resolvers {
Mutation: MutationResolvers.Type;
AddMemberPayload: AddMemberPayloadResolvers.Type;
}

// @ts-ignore
declare module \\"graphql-tools\\" {
interface IResolvers extends Resolvers {}
}
"
`;

Expand Down Expand Up @@ -1775,6 +1795,11 @@ export interface Resolvers {
Query: QueryResolvers.Type;
Number: NumberResolvers.Type;
}

// @ts-ignore
declare module \\"graphql-tools\\" {
interface IResolvers extends Resolvers {}
}
"
`;

Expand Down Expand Up @@ -2098,6 +2123,11 @@ export interface Resolvers {
User: UserResolvers.Type;
UserType?: UserTypeResolvers.Type;
}

// @ts-ignore
declare module \\"graphql-tools\\" {
interface IResolvers extends Resolvers {}
}
"
`;

Expand Down Expand Up @@ -2289,6 +2319,11 @@ export interface Resolvers {
Query: QueryResolvers.Type;
User: UserResolvers.Type;
}

// @ts-ignore
declare module \\"graphql-tools\\" {
interface IResolvers extends Resolvers {}
}
"
`;

Expand Down Expand Up @@ -2919,6 +2954,11 @@ export interface Resolvers {
Query: QueryResolvers.Type;
Number: NumberResolvers.Type;
}

// @ts-ignore
declare module \\"graphql-tools\\" {
interface IResolvers extends Resolvers {}
}
"
`;

Expand Down Expand Up @@ -3574,6 +3614,11 @@ export interface Resolvers {
Query: QueryResolvers.Type;
Number: NumberResolvers.Type;
}

// @ts-ignore
declare module \\"graphql-tools\\" {
interface IResolvers extends Resolvers {}
}
"
`;

Expand Down Expand Up @@ -3753,6 +3798,11 @@ export interface Resolvers {
Subscription: SubscriptionResolvers.Type;
User: UserResolvers.Type;
}

// @ts-ignore
declare module \\"graphql-tools\\" {
interface IResolvers extends Resolvers {}
}
"
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8530,6 +8530,11 @@ export interface Resolvers {
AuthPayload: AuthPayloadResolvers.Type;
MutationResult: MutationResultResolvers.Type;
}

// @ts-ignore
declare module \\"graphql-tools\\" {
interface IResolvers extends Resolvers {}
}
"
`;

Expand Down

0 comments on commit 6b2fdd7

Please sign in to comment.