Spreading schemas across multiple files #421
-
Hello! I need help with spreading my schemas across multiple files. So far this is what i was able to come up with but when i try to run the code I am getting this error // project.ts
import { Project, ProjectStatus } from "@prisma/client";
import { builder } from "../builder";
import { projects } from "../sample-data";
export const ProjectObject = builder.objectRef<Project>("ProjectType");
const ProjectStatusEnum = builder.enumType("ProjectStatusEnum", {
values: [
ProjectStatus.Completed,
ProjectStatus.InProgress,
ProjectStatus.NotStarted,
] as const,
});
ProjectObject.implement({
fields: (t) => ({
id: t.exposeID("id"),
clientId: t.exposeString("clientId"),
name: t.exposeString("name"),
description: t.exposeString("description"),
status: t.field({
type: ProjectStatusEnum,
resolve: ({ status }) => status,
}),
}),
});
builder.queryType({
fields: (t) => ({
projects: t.field({
type: [ProjectObject],
resolve: () => projects,
}),
}),
});
export const projectSchema = builder.toSchema({}); // client.ts
import { Client } from "@prisma/client";
import { builder } from "../builder";
import { clients } from "../sample-data";
export const ClientObject = builder.objectRef<Client>("ClientType");
ClientObject.implement({
fields: (t) => ({
id: t.exposeID("id"),
name: t.exposeString("name"),
email: t.exposeString("email"),
phone: t.exposeString("phone"),
}),
});
builder.queryType({
fields: (t) => ({
clients: t.field({
type: [ClientObject],
resolve: () => clients,
}),
}),
});
export const clientSchema = builder.toSchema({}); // schema.ts
import { mergeSchemas } from "@graphql-tools/schema";
import { clientSchema } from "./schema/client";
import { projectSchema } from "./schema/project";
export const schema = mergeSchemas({
schemas: [clientSchema, projectSchema],
}); // graphql.ts
import type { NextApiRequest, NextApiResponse } from "next";
import { createServer } from "@graphql-yoga/node";
import { schema } from "../../graphql/schema";
const server = createServer<{ req: NextApiRequest; res: NextApiResponse }>({
schema,
});
export default server; |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
builder.queryType defines the query type, which you are doing multiple times. You can use builder.queryFields or builder.queryField to just add fields to the existing query type. I usually define the query and mutation types without any fields in the file that I create my builder in, then add the queries/mutations for various types in the files that define those types (similar to what you are doing now) |
Beta Was this translation helpful? Give feedback.
-
I just added a new (will add a lot more in the future) that shows how to build a schema that's spread across multiple files: https://github.com/hayes/pothos/tree/main/examples/complex-app |
Beta Was this translation helpful? Give feedback.
I just added a new (will add a lot more in the future) that shows how to build a schema that's spread across multiple files: https://github.com/hayes/pothos/tree/main/examples/complex-app