Hello, everybody, I’m trying to figure out the mos...
# orm-help
j
Hello, everybody, I’m trying to figure out the most performant way to write type resolvers, we can use either Prisma Fluent API or separate query: Here is the link to the issue with 2+ queries when fluent api is used. https://github.com/prisma/prisma/issues/1984
Copy code
// Option 1 - Separate Query
export const Story = {
  tags: (_args, parent) =>
    db.tag.findMany({ where: { story: { id: parent.id } } })
};

// Option 2 - Fluent API
export const Story = {
  tags: (_args, parent) =>
    db.story.findUnique({ where: { id: parent.id } }).tags()
};
But also docs states that
findUnique
queries are batched and it improves performance, so my thought was to use Fluent api if the separate query uses
findMany
like in the example above, but if we were to query single
tag
instead of
tags
(
findUnique
in both separate and fluent api queries) it’s better to use separate query. But regarding this issues people say that 1 query always outperform multiple queries, if that’s the case I don’t see any use cases where Fluent API is preferred. When would Fluent API be a better choice ? Thank you.
d
Using the fluent api is a must for when querying objects. So when you define story, for the tags resolver, you should use the fluent api, otherwise, if you query an array of 20 stories, it would need 21 requests, one for the list of stories and an additional request per story that is a tag query. Using the fluent api, the tags query will be handled by the data loader and the tags for each story will be included in the initial request
✅ 1
j
So basically if I were to use
findMany
in a standalone query I’d better use
findUnique
with Fluent API ? It means in the original question Option 2 is the way to go ? Cause it seems that it’s slower, no ?
d
so if its a standalone query like
getTagsByStory
, you will pass the story id in the args and then in the resolver you can do either I guess. Im not sure what the difference in speed is. The only place that you must use the data loader is when resolving tags on the story object