dependency-injection - 使用依赖注入将羽毛应用程序对象传递给 TypeGraphQL
问题描述
我不知道如何将我的传递app object
给我的 TypeGrapQL 解析器。
我创建了我的类型和解析器,并使用express-graphql
. 我能够运行该图,但在传递应用程序对象以使用注册服务方面没有运气。
我的graphql.service.ts
样子是这样的:
import { ServiceAddons } from '@feathersjs/feathers'
import { graphqlHTTP } from 'express-graphql'
import 'reflect-metadata'
import { buildSchemaSync } from 'type-graphql'
import { Container } from 'typedi'
import { Application } from '../../declarations'
import { Graphql } from './graphql.class'
import { ArticleResolver } from './resolvers/article.resolver'
// Add this service to the service type index
declare module '../../declarations' {
interface ServiceTypes {
graphql: Graphql & ServiceAddons<any>
}
}
export default async function (app: Application): Promise<void> {
const schema = buildSchemaSync({
resolvers: [__dirname + '/resolvers/*.resolver.ts'],
container: Container,
})
app.use(
'/graphql',
graphqlHTTP({
schema: schema,
graphiql: true,
})
)
}
这是我的解析器类之一article.resolver.ts
import { Arg, Query, Resolver } from 'type-graphql'
import { Service } from 'typedi'
import { Application } from '../../../declarations'
import { Category } from '../types/category.type'
@Service()
@Resolver(Category)
export class CategoryResolver {
constructor(private readonly app: Application) {}
@Query((returns) => [Category])
async categories() {
try {
const result = await this.app.service('category').find()
return (result as any).data // TODO: Refactor to return result with pagination details
} catch (err) {
console.log('Categories resolver error', err)
return []
}
}
}
我不能做this.app.service()
未定义this.app
的事情
我对依赖注入在 TypeGrapQL 中的工作方式有点困惑,感谢任何帮助。
谢谢
解决方案
我设法使它工作,如果有人有同样的问题,这是我的解决方案:
@Service
我创建了一个用from装饰的 Graphql 类,typedi
它接收一个应用程序对象
import { Service } from 'typedi'
import { Application } from '../../declarations'
@Service()
export class Graphql {
app: Application
//eslint-disable-next-line @typescript-eslint/no-unused-vars
constructor(app: Application) {
this.app = app
}
}
在我graphql.service.ts
的课程中,我启动了课程并将实例传递给typedi
容器
import { buildSchemaSync } from 'type-graphql'
import { Container } from 'typedi'
import { Application } from '../../declarations'
import { Graphql } from './graphql.class'
export default async function (app: Application): Promise<void> {
const graphql = new Graphql(app)
Container.set('graphql', graphql)
const schema = buildSchemaSync({
resolvers: [__dirname + '/resolvers/category.resolver.ts'],
container: Container, // Pass the container to the resolvers
})
// Initialize our express graphql server
}
最后在我的解析器中,我装饰解析器@Service
并将graphql实例注入构造函数:
import { Application } from '../../../declarations'
import { Graphql } from '../graphql.class'
import { Inject, Service } from 'typedi'
@Service()
@Resolver(Category)
export class CategoryResolver {
app: Application
constructor(@Inject('graphql') private readonly graphql: Graphql) {
this.app = this.graphql.app
}
// Queries and Mutations
}
这解决了我,希望它对你有帮助
推荐阅读
- sql - 需要以下格式的正确输出
- java - 使用计时器依次显示一组 JButtons [JAVA SWING]
- sql - 使用 CASE TERADATA 插入空表
- git-bash - Windows 上的 git-bash 中的 push、pull 等命令会无限期冻结
- json - 针对 XML 或 XSD 验证转换后的 JSON
- dart - 如果我在 if 语句中使用 'is' 关键字,Dart 如何自动向上转换
- python - 如何通过 Tuning 改进深度学习
- javascript - 如何识别和响应 BTTV/FFZ/Twitch 表情?
- python - 通过 for 循环将 str 和 int 连接到调用列表
- binding - Jaxb xjc 绑定,按规则重命名包