【发布时间】:2021-05-05 16:43:20
【问题描述】:
我不知道如何将我的 app object 传递给我的 TypeGrapQL 解析器。
我创建了我的类型和解析器,并使用 express-graphql 设置了一个 graphql 服务器。我能够运行图表,但在传递 app 对象以使用已注册服务方面没有运气。
我的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 中的工作方式有点困惑,感谢任何帮助。
谢谢
【问题讨论】:
标签: dependency-injection graphql feathersjs typegraphql