簡體   English   中英

使用typeorm實體的graphql查詢

[英]graphql query using typeorm entity

我正在學習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);
    }
};

我決定放棄嘗試使用typeorm和Prisma

如果您正在尋找Prisma(自動生成的數據庫和架構)的神奇之處,而且還可以靈活地托管您的API並訪問模型和解析器以便您可以執行自定義操作,那么您應該查看Warthog 它是一個用TypeScript編寫的Node.js GraphQL API框架,您可以在其中設置解析器和數據模型,並使用固定的分頁,過濾等自動生成整個模式......類似於Prisma的約定。 它不像Prisma那樣功能豐富,但它可以為您提供更多控制。 您可以查看warthog-starter ,它將幫助您快速啟動並運行。

免責聲明:我是Warthog的作者

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM