prolink007
prolink007

Reputation: 34594

GraphQL query using TypeORM entity

I am learning graphql and combining it with typeorm. I wrote a query with graphql and was wondering if this is the correct way to combine the typeorm entity and the graphql type for the same entity. Or is there a way for me to use the typeorm entity instead of the graphql type as the return value?

The typeorm entity and the graphql type have the exact same fields, essentially the are exactly the same. Just one is defined as a graphql type and the other is using the typeorm decorators.

I am also not sure how this magic is returning a TestType from a Promise<Test>. Where TestType is a graphql type and <Test> is a typeorm entity.

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);
    }
};

Upvotes: 4

Views: 2035

Answers (2)

Dan Caddigan
Dan Caddigan

Reputation: 1598

If you’re looking for the magic of Prisma (autogenerated DB and schema), but also the flexibility of hosting your APIs and having access to the models and resolvers so that you can perform custom actions, you should check out Warthog. It’s a Node.js GraphQL API framework written in TypeScript where you set up resolvers and data models, and it auto-generates your entire schema with opinionated pagination, filtering, etc... similar to Prisma’s conventions. It’s not as feature-rich as Prisma, but it gives you a lot more control. You can check out warthog-starter, which will get you up and running quickly.

Disclaimer: I’m the author of Warthog

Upvotes: 2

prolink007
prolink007

Reputation: 34594

I decided to ditch the attempt to use typeorm and went with Prisma.

Upvotes: 1

Related Questions