【问题标题】:GraphQL query using TypeORM entity使用 TypeORM 实体的 GraphQL 查询
【发布时间】:2018-11-22 04:17:09
【问题描述】:

我正在学习 graphql 并将其与 typeorm 结合使用。我用graphql 编写了一个查询,想知道这是否是将typeorm 实体和graphql 类型组合为同一实体的正确方法。或者有没有办法让我使用typeorm 实体而不是graphql 类型作为返回值?

typeorm 实体和graphql 类型具有完全相同的字段,本质上是完全相同的。只有一个被定义为graphql 类型,另一个使用typeorm 装饰器。

我也不确定这个魔法是如何从Promise<Test> 返回TestType。其中TestTypegraphql 类型,<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


    【解决方案1】:

    如果您正在寻找 Prisma(自动生成的数据库和架构)的魔力,以及托管 API 和访问模型和解析器以便执行自定义操作的灵活性,您应该查看 @987654321 @。它是一个用 TypeScript 编写的 Node.js GraphQL API 框架,您可以在其中设置解析器和数据模型,并且它会自动生成您的整个架构,包括自以为是的分页、过滤等……类似于 Prisma 的约定。它不像 Prisma 那样功能丰富,但它给了你更多的控制权。您可以查看warthog-starter,它会让您快速上手。

    免责声明:我是 Warthog 的作者

    【讨论】:

    • 谢谢,我去看看。
    【解决方案2】:

    我决定放弃使用 typeorm 的尝试,转而使用 Prisma

    【讨论】:

      猜你喜欢
      • 2022-01-06
      • 2021-02-26
      • 2022-07-23
      • 2019-12-30
      • 1970-01-01
      • 2021-06-02
      • 2020-11-01
      • 2019-04-24
      • 2020-12-22
      相关资源
      最近更新 更多