【问题标题】:Passing feathers app object to TypeGraphQL using dependency injection使用依赖注入将羽毛应用程序对象传递给 TypeGraphQL
【发布时间】: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


    【解决方案1】:

    我设法让它工作,如果有人遇到同样的问题,这是我的解决方案:

    我创建了一个用typedi 中的@Service 装饰的Graphql 类,它接收一个应用程序对象

    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
    }
    

    这解决了我,希望它对你有任何帮助?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 2019-12-28
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      相关资源
      最近更新 更多