首页 > 解决方案 > 使用依赖注入将羽毛应用程序对象传递给 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 中的工作方式有点困惑,感谢任何帮助。

谢谢

标签: dependency-injectiongraphqlfeathersjstypegraphql

解决方案


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

@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
}

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


推荐阅读