-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for IoC in resolvers defined as classes (#52)
Closes: #50
- Loading branch information
Showing
7 changed files
with
197 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import assert from 'node:assert'; | ||
|
||
import { loadFilesSync } from '@graphql-tools/load-files'; | ||
import { mergeResolvers } from '@graphql-tools/merge'; | ||
|
||
import { ContainerBindings, IocContract } from '@ioc:Adonis/Core/Application'; | ||
|
||
type UnknownConstructor = new (...args: unknown[]) => unknown; | ||
|
||
const antiClashSuffix = 'Resolvers'; | ||
|
||
export function loadResolvers( | ||
resolversPaths: string[], | ||
container: IocContract<ContainerBindings>, | ||
) { | ||
const resolverModules = resolversPaths.flatMap((resolversPath) => | ||
loadFilesSync(resolversPath, { recursive: false }), | ||
); | ||
const resolversPartials = resolverModules.map((resolverModule) => | ||
makeResolversPartial(resolverModule, container), | ||
); | ||
|
||
return mergeResolvers(resolversPartials); | ||
} | ||
|
||
function makeResolversPartial( | ||
resolverModule: Record<string, unknown>, | ||
container: IocContract<ContainerBindings>, | ||
) { | ||
return Object.fromEntries( | ||
Object.entries(resolverModule) | ||
.filter( | ||
([, value]) => | ||
(typeof value === 'object' && value !== null) || | ||
typeof value === 'function', | ||
) | ||
.map(([key, value]) => { | ||
if (key.endsWith(antiClashSuffix) && key !== antiClashSuffix) { | ||
key = key.slice(0, -antiClashSuffix.length); | ||
} | ||
if (typeof value === 'object' && value !== null) { | ||
return [key, value]; | ||
} else { | ||
assert(typeof value === 'function'); | ||
return [ | ||
key, | ||
mapResolverClass(value as UnknownConstructor, container), | ||
]; | ||
} | ||
}), | ||
); | ||
} | ||
|
||
function mapResolverClass( | ||
value: UnknownConstructor, | ||
container: IocContract<ContainerBindings>, | ||
) { | ||
const instance = container.make(value); | ||
const prototype = Object.getPrototypeOf(instance); | ||
return Object.fromEntries( | ||
Object.entries(Object.getOwnPropertyDescriptors(prototype)) | ||
.filter( | ||
([name, desc]) => | ||
name !== 'constructor' && typeof desc.value === 'function', | ||
) | ||
.map(([name, desc]) => [name, desc.value.bind(instance)]), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
exports.Query = class Query { | ||
queryD() { | ||
return 'test'; | ||
} | ||
}; | ||
|
||
exports.DResolvers = class D { | ||
value() { | ||
return 'test'; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type Query { | ||
queryD: String | ||
} | ||
|
||
type D { | ||
value: String! | ||
} |