【发布时间】:2020-02-21 10:55:25
【问题描述】:
我创建了一个包含多个对象的本地包。该软件包旨在通过 npm 安装在其他程序中,以便从公共对象库中受益。其中,我创建了一个简单的类来创建 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;
}
}
在最终程序之一中复制此代码并从该最终程序导入类可以正常工作。
但是,在安装公共库后在最终程序中导入类会导致 TypeGraphQL 失败并出现错误“Cannot determine GraphQL input type for start”。
下面是正在下降的解析器示例和为 Arg 定义的类型,因为“开始”是解析器管理分页的参数。
不知道怎么回事。只是提到我在库的类定义文件中导入了“反射元数据”,也是在最终程序的开头。
解析器
@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;
}
【问题讨论】:
标签: typescript graphql typegraphql