P
Prisma5mo ago
Bell

Method to "compose" queries? (a la GraphQL Fragments)

Hi, Is it possible with Prisma to "compose" queries, like you can in GraphQL queries using fragments, all in a type-safe way? For example, if I want to find a model from my back-end, and then be able to say "I want this model with the data that is required for each of these front-end components to display properly". I know you can just define simple objects that are typed to be Prisma.<Model>Select and such, but if you have underlying relationships that overlap, they won't be deeply merged properly. I know there are functions for this on the runtime side, but when considering types, maybe this is a bit heavy to do properly if you end up with a lot of "fragments" in one query? Has anyone achieved this before, or is there maybe a better / simpler way to go about it? (I asked over here as well, but I'm making sure I'm getting all the answers I can by posting here too)
GitHub
Method to "compose" queries? (a la GraphQL Fragments) · prisma pri...
Question Hi, Is it possible with Prisma to &quot;compose&quot; queries, like you can in GraphQL queries using fragments, all in a type-safe way? For example, if I want to find a model from my back-...
2 Replies
Prisma AI Help
Prisma AI Help5mo ago
Skipping coffee for instant knowledge! Let's brew that AI answer now. Prisma does not natively support query composition or reusable fragments in the same way as GraphQL fragments. You can define reusable select/include objects (e.g., typed as Prisma.<Model>Select), but Prisma does not provide built-in deep merging or fragment-like composition for these objects. This means if you try to combine multiple "fragments" (select objects) with overlapping nested relations, you must manually merge them, which can be error-prone and is not type-safe out of the box. Some users have created helper functions to build select/include objects dynamically, but these helpers do not solve the deep merging and type-safety challenges when composing complex, overlapping fragments. For example, you can use a function like:
function prismaSelectify(keys) {
return keys.reduce((acc, key) => {
const parts = key.split('.');
let current = acc;
parts.forEach((part, index) => {
if (index === parts.length - 1) {
current[part] = true;
} else {
current[part] = current[part] || { select: {} };
current = current[part].select;
}
});
return acc;
}, {});
}
function prismaSelectify(keys) {
return keys.reduce((acc, key) => {
const parts = key.split('.');
let current = acc;
parts.forEach((part, index) => {
if (index === parts.length - 1) {
current[part] = true;
} else {
current[part] = current[part] || { select: {} };
current = current[part].select;
}
});
return acc;
}, {});
}
But this approach lacks type-safety and does not handle all edge cases, especially with nested relations and overlapping fields. The Prisma team and community have discussed this limitation, and the main challenge is maintaining type-safety when composing select/include objects from multiple sources. There is no official solution yet, though some workarounds exist for simple cases. For now, you must manually construct and merge your select/include objects, or use custom helper functions with caution, understanding their limitations regarding type-safety and deep merging of nested relations More elegant select API by listing fields as array, Specifying the "select" param in a query using a variable with a defined type breaks types.
Nurul
Nurul5mo ago
GitHub
Method to "compose" queries? (a la GraphQL Fragments) · prisma pri...
Question Hi, Is it possible with Prisma to &quot;compose&quot; queries, like you can in GraphQL queries using fragments, all in a type-safe way? For example, if I want to find a model from my back-...

Did you find this page helpful?