Prisma plug-in exposing all fields #429
Replies: 5 comments 6 replies
-
This is probably not something that would be part of Pothos core. It could be something you could write yourself if you wanted. There will be some utilities coming in the future (see #348) that will make building something like this your self easier. It would be posssible to use data from the prisma client (currently I strongly believe that your data and API should not be tied closely together, and the Pothos APIs try to reflect that by making the types and fields in your API be defined explicitly. A simple test for this is asking if the API would change automatically if your data source chages. In Pothos, the goal is for this to cause a type-error rather than for it to cause a change in the schema. You will notice things like the expose methods requiring the field name as well as the name of the column they are exposing even though they are often the same. Pothos also requires you to explicit define the type of the fields you are exposing. a String type in your DB could be a String in graphql, but it could also be an ID, or a custom scalar like email. When you get into exposing things automatically you take away that control, and usually end up with a lower quality API. I recently posted a bit about plans around making crud APIs easier to build in the Pothos discord channel:
Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
I can probably use the Prisma |
Beta Was this translation helpful? Give feedback.
-
This feature would be extraordinarily helpful in early prototyping/development; perhaps enabled/gated by some sort of |
Beta Was this translation helpful? Give feedback.
-
i totally agree that this is usually not the right thing to do, but I also have now a case where it would be really good to easily be able to expose all fields. @marceloverdijk did you find a solution for this? |
Beta Was this translation helpful? Give feedback.
-
Wildly enough, but this works and exposes EVERYTHING in one call: import { getModel } from "@pothos/plugin-prisma";
import { builder } from "../builder.js";
import { Prisma } from "@prisma/client";
export const exposeEverything = () => {
return Object.fromEntries(
// unfortunatly, the getDmmf function is private in pothos, so we use Prisma directly here
Prisma.dmmf.datamodel.models.map((model) => {
const allFields = getModel(model.name, builder).fields;
const ref = builder.prismaObject(model.name as any, {
fields: (t) => {
return Object.fromEntries(
allFields.map((field) => {
const fieldRef = field.relationName
? t.relation(field.name, {
nullable: field.isRequired ? false : true,
onNull: "error",
})
: t.expose(field.name, {
type: field.type as any,
nullable: field.isRequired ? false : true,
});
return [field.name, fieldRef];
}),
);
},
});
return [model.name, ref];
}),
);
}; it would technically return refs for each of the objectRefs, but you can just reimplement them if you need to add fields. This is definitly "do-not-try-at-home" unless you know what you are doing! But i am surprised it works so easily. If you only want to expose scalars: (was my first draft draft): import type { PrismaObjectRef } from "@pothos/plugin-prisma";
import { getModel } from "@pothos/plugin-prisma";
import { builder } from "../builder.js";
export const exposeAllScalars = <O extends PrismaObjectRef<any, any>>(
objectRef: O,
) => {
const allScalars = getModel(
objectRef.modelName,
builder as any,
).fields.filter((f) => f.kind === "scalar");
builder.objectFields(objectRef, (t) => {
return Object.fromEntries(
allScalars.map((scalar) => {
return [
scalar.name,
t.expose(
scalar.name as any,
{
type: scalar.type,
nullable: scalar.isRequired ? false : true,
} as any,
),
];
}),
);
});
}; |
Beta Was this translation helpful? Give feedback.
-
Would it be possible to expose all Prisma fields on an Object automatically without exposing the one by one explicitly?
Beta Was this translation helpful? Give feedback.
All reactions