我在学习graphql
并将其与 typeorm
结合.我用 graphql
写了一个查询想知道这是否是结合 typeorm
的正确方法实体和 graphql
为同一实体键入。或者有没有办法让我使用 typeorm
实体而不是 graphql
类型作为返回值?
typeorm
实体和 graphql
类型具有完全相同的字段,本质上是完全相同的。只有一个被定义为 graphql
类型,另一个正在使用 typeorm
装饰器。
我也不确定这个魔法是如何返回 TestType
的来自Promise<Test>
.在哪里TestType
是 graphql
类型和 <Test>
是 typeorm
实体。
import {GraphQLFieldConfig, GraphQLNonNull} from "graphql/type/definition";
import {TestType} from "../type/TestType";
import {GraphQLID} from "graphql";
import {IGraphQLContext} from "../IGraphQLContext";
export const Test: GraphQLFieldConfig<any, IGraphQLContext, any> = {
// notice the type to return here is the graphql type
type: new GraphQLNonNull(TestType),
description: "A query for a test",
args: {
id: {
type: new GraphQLNonNull(GraphQLID),
description: "The ID for the desired test"
}
},
async resolve (source, args, context) {
// getTest(...) here returns a promise<Test> where Test is a typeorm entity
return context.db.testDAO.getTest(args.id);
}
};
最佳答案
如果您正在寻找 Prisma(自动生成的数据库和架构)的魔力,以及托管 API 和访问模型和解析器以便执行自定义操作的灵 active ,您应该查看 Warthog。它是一个用 TypeScript 编写的 Node.js GraphQL API 框架,您可以在其中设置解析器和数据模型,它会自动生成带有固定分页、过滤等的整个模式……类似于 Prisma 的约定。它不像 Prisma 那样功能丰富,但它给了你更多的控制权。您可以查看 warthog-starter ,这将使您快速启动并运行。
免责声明:我是 Warthog 的作者
关于javascript - 使用 TypeORM 实体的 GraphQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50822710/