带有 Pothos Graphql 和 prisma-plugin 的 TypeError

TypeError with Pothos Graphql and prisma-plugin

提问人:AnthonyDev220 提问时间:11/17/2023 更新时间:11/18/2023 访问量:24

问:

我正在构建一个社交应用程序,其中包含用于前端和 SSR 的 nextjs、graphql-yoga 服务器以及用于类型安全 graphql 的 pothos 和我的数据库的 prisma。我已经遵循了他们所有文档中给出的所有示例进行集成。但是,我遇到了一个令人困惑的错误。

ERR Error: Type Post must define one or more fields.

Type User must define one or more fields.

在我的graphql yoga界面中,我看到这个错误消息

"message": "Type Post must define one or more fields.\n\nType User must define one or more fields.",

在我的类型定义中,我清楚地定义了 和 type 中的所有字段,所以我非常困惑,我不知道这个错误指的是什么。PostUser

这是我的应用程序目录的文件夹结构:

api
 ┣ auth
 ┃ ┗ [...nextauth]
 ┃ ┃ ┣ authorization.ts
 ┃ ┃ ┣ options.ts
 ┃ ┃ ┗ route.ts
 ┗ graphql
 ┃ ┣ types
 ┃ ┃ ┣ Post.ts
 ┃ ┃ ┣ Profile.ts
 ┃ ┃ ┗ User.ts
 ┃ ┣ builder.ts
 ┃ ┣ route.ts
 ┃ ┗ schema.ts

这是我的文件夹:Post.ts

//./api/graphql/types/Post.ts

import prisma from "@/prisma/database";
import { builder } from "../builder";

export const Post = builder.prismaObject("Post", {
    fields: (t) => ({
        id: t.exposeString("id"),
        visibleTo: t.exposeString("visibleTo"),
        published: t.exposeBoolean("published"),
        hasImage: t.exposeBoolean("hasImage"),
        hasVideo: t.exposeBoolean("hasVideo"),
        createdAt: t.expose("createdAt", { type: Date }),
        updatedAt: t.expose("updatedAt", { type: Date }),
        title: t.exposeString("title", { nullable: true }),
        content: t.exposeString("content", { nullable: true }),
        likes: t.relation("likes"),
        comments: t.relation("comments"),
        media: t.relation("media"),
        category: t.relation("category"),
        viewCount: t.exposeInt("viewCount"),
        author: t.relation("author"),
    }),
});

builder.queryField("posts", (t) =>
    t.prismaField({
        type: ["Post"],
        resolve: async (query, root, args, ctx, info) => {
            return prisma.post.findMany({ ...query });
        },
    })
);

builder.queryFields((t) => ({
    post: t.prismaField({
        type: "Post",
        args: {
            id: t.arg.string({ required: true }),
        },
        resolve: async (query, root, args, ctx, info) => {
            return prisma.post.findUniqueOrThrow({
                where: {
                    id: args.id,
                },
            });
        },
    }),
}));

这是我的:route.ts

// pages/api/graphql/route.ts

import { createYoga } from "graphql-yoga";
import type { NextApiRequest, NextApiResponse } from "next";
import { schema } from "./schema";

const { handleRequest } = createYoga<{
    req: NextApiRequest;
    res: NextApiResponse;
}>({
    schema,
    // While using Next.js file convention for routing, we need to configure Yoga to use the correct endpoint
    graphqlEndpoint: "/api/graphql",

    // Yoga needs to know how to create a valid Next response
    fetchAPI: { Response: Response },
});

export {
    handleRequest as GET,
    handleRequest as POST,
    handleRequest as OPTIONS,
};
打字稿 graphql prisma-graphql graphql graphql-yoga

评论


答:

0赞 AnthonyDev220 11/18/2023 #1

通过在我的文件中定义根查询类型和根突变类型来解决问题,如下所示:builder.ts

/* MUST DEFINE A ROOT QUERY TYPE AND root MUTATION TYPE WHEN USING POTHOS GRAPHQL */
builder.queryType({
    description: "The query root type.",
    fields: (t) => ({
        ok: t.boolean({ resolve: () => true }),
    }),
});
builder.mutationType({});

最佳做法是在架构生成器旁边定义它,以便它选取。

//schema builder from @pothos/core
export const builder = new SchemaBuilder<{
    PrismaTypes: PrismaTypes;
    Context: {};
    Scalars: {
        DateTime: { Input: Date; Output: Date };
    };
}>({
    plugins: [PrismaPlugin],
    prisma: {
        client: prisma,
        filterConnectionTotalCount: true,
        onUnusedQuery: process.env.NODE_ENV === "production" ? null : "warn",
    },
});