【发布时间】:2018-11-22 04:17:09
【问题描述】:
我正在学习 graphql 并将其与 typeorm 结合使用。我用graphql 编写了一个查询,想知道这是否是将typeorm 实体和graphql 类型组合为同一实体的正确方法。或者有没有办法让我使用typeorm 实体而不是graphql 类型作为返回值?
typeorm 实体和graphql 类型具有完全相同的字段,本质上是完全相同的。只有一个被定义为graphql 类型,另一个使用typeorm 装饰器。
我也不确定这个魔法是如何从Promise<Test> 返回TestType。其中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);
}
};
【问题讨论】:
标签: javascript graphql typeorm