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
Browse files Browse the repository at this point in the history
closes #15
  • Loading branch information
jasonkuhrt committed Feb 9, 2019
1 parent dcedf5f commit 4f9c6a9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/01-configuration.md
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
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
27 changes: 27 additions & 0 deletions packages/graphqlgen/src/generators/typescript/generator.ts
Expand Up @@ -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 {}
}
`
}

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

export interface ModelMap {
Expand Down

0 comments on commit 4f9c6a9

Please sign in to comment.