简体   繁体   English

TypeGraphQL:使用从本地 Package 导入的 GraphQL class 时出现@Arg 类型错误

[英]TypeGraphQL : @Arg Type Error when using a GraphQL class imported from a local Package

I have created a local package with several objects.我创建了一个带有多个对象的本地 package。 this package is aimed to be installed via npm in other programs in order to benefit from a common librairy of objects.此 package 旨在通过 npm 在其他程序中安装,以便从通用对象库中受益。 Among them, I have created a simple class to create a AppoloServer, to be used with TypeGraphQL entities, types and resolvers:其中,我创建了一个简单的 class 来创建 AppoloServer,与 TypeGraphQL 实体、类型和解析器一起使用:

export class ClsApolloGraphQlServer {
    private _resolvers: any[];
    private _connection: Connection;

    constructor(
        resolvers: any[],
        connection: Connection,
    ) {
        this._resolvers = resolvers;
        this._connection = connection;
    }

    public async initApolloServer(
        app: express.Application,
        corsOpt: cors.CorsOptions
    ): Promise<ApolloServer> {
            const {
                typeDefs,
                resolvers,
            } = await buildTypeDefsAndResolvers({
                resolvers: this._resolvers,
            });

            const schema = makeExecutableSchema({
                typeDefs,
                resolvers,
            });

            addSchemaLevelResolveFunction(
                schema,
                (_, __, context) =>
                    !!getSession(context, this._SESSION_TYPE)
            );

            const apolloServer: ApolloServer = new ApolloServer({
                schema,
                context: ({
                    req,
                    res,
                }: {
                    req: Request;
                    res: Response;
                }): IGlobalContext => {
                    return {
                        req,
                        res,
                        dbConnection: this._connection,
                    };
                },
                formatError: (err: GraphQLError) =>
                    FormatErrorMessageGraphQlServer(err),
            });

            apolloServer.applyMiddleware({ app, cors: corsOpt });

            return apolloServer;

    }
}

Duplicating this code in one of the final program and importing the class from this final program works fine.在最终程序之一中复制此代码并从该最终程序导入 class 工作正常。

However, importing the class in the final program after installing the common library makes TypeGraphQL failing with the error " Cannot determine GraphQL input type for start ".但是,在安装公共库后在最终程序中导入 class 会导致 TypeGraphQL 失败,并出现错误“ Cannot determine GraphQL input type for start ”。

Below is an example of the resolver that is falling and the type defined for Arg, since 'start' is an argument of resolvers to manage pagination.下面是正在下降的解析器示例和为 Arg 定义的类型,因为“开始”是解析器管理分页的参数。

Don't know what is wrong.不知道有什么问题。 Just mentionning that I'm importing 'reflect-metadata' in the class Definition file in the librairy, and also at the very beginning of the final program.只是提到我在图书馆的 class 定义文件中导入“反射元数据”,也是在最终程序的开头。

Resolver解析器

@Query(() => [objectTypeCls], { name: `getAll${suffix}` })
        async getAll(
            @Ctx() context: IGlobalContext,
            @Args() { start, nbRecords }: PaginationArgs
        ): Promise<TExposeApi[]> {

            if (context.dbConnection && context.dbConnection.isConnected) {
                const resu: TExposeApi[] = await context.dbConnection
                    .getRepository<TExposeApi>(objectTypeCls)
                    .createQueryBuilder(suffix)
                    .skip(start)
                    .take(nbRecords)
                    .getMany();
                return resu;
            } else return [];
        }

ArgType参数类型

@ArgsType()
export class PaginationArgs {
    @Field(() => Int)
    start: number;

    @Field(() => Int)
    nbRecords: number;
}

You have to use package repository for bundled artifacts or a lerna monorepo with dependencies hoisting.对于捆绑的工件,您必须使用 package 存储库或具有依赖项提升的 lerna monorepo。 In your case you just linking the packages but they have separated graphql package in their node_modules, which raises a conflict where Int !== Int between graphql packages instances.在您的情况下,您只是链接包,但它们在其 node_modules 中分离了graphql package,这会引发冲突,其中Int !== Intgraphql实例之间包实例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:在 typegraphql 中将数组作为输入类型传递时,无法确定 GraphQL 输入类型 - Error: Cannot determine GraphQL input type when passing array as input type in typegraphql typegraphql:使用变量时在操场上出现参数错误 - typegraphql : parameter error in playground when using variables 使用 webpack 和 babel-loader 进行转译时,TypeGraphQL @Arg 装饰器因解析器错误而失败 - TypeGraphQL @Arg decorator fails with parser error when transpiling with webpack and babel-loader 如何将 `graphql-scalars` package 与 `typegraphql` 一起使用? - How can I use the `graphql-scalars` package with `typegraphql`? 使用带有 typegraphql 的非标准类型 (BigNumber) - Using a non-standard type (BigNumber) with typegraphql 使用 TypeGraphQL 在浏览器中为 Apollo 本地状态生成 typedef 和解析器 - Using TypeGraphQL to generate typedefs and resolvers for Apollo local state in browser TypeGraphql 和 Prisma 2 类型冲突 - TypeGraphql and Prisma 2 type conflicts 使用 TypeScript 中的导入类型在一行中调用导入的类中的方法 - Calling a method from an imported class using the Import Type in TypeScript in One Line 在 Angular 应用程序中使用 graphql-codegen 时出现错误“对象类型未知” - Error "Object is of type unknown" when using graphql-codegen in the Angular application Javascript - Function 从 package 导入时返回不同的结果 - Javascript - Function returning different result when imported from a package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM