-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
defaultFields, set child defaultFields from parent type #333
Comments
Hi, Is this needed every time you query the parent type or in only some query |
It looks like I can't do it at the query level easily because the "parent" is not always at the root of the query, sometimes there is a parent above it. Why can't I return also a "mergeDeep" object in the defaultFields ? |
Look at this code and see how we can do this we are filtering the input fields to remove the fields not in the prisma model. prisma-tools/packages/plugins/src/select.ts Line 256 in c5c0661
Also, look at the line 284 |
@ResolveField()
exercises(@Args() args: FindManySportExerciseArgs, @Info() info) {
const select = new PrismaSelect(info, {
defaultFields,
});
const originalQuery = select.valueWithFilter('');
let selectValue = select.value;
if (originalQuery?.select?.title) {
selectValue = PrismaSelect.mergeDeep(selectValue, {
select: {
simpleExercise: {
select: {
title: true,
},
},
},
});
}
return this.sportExercisesService.findMany(args, selectValue);
} I have done that, but it doesn't feel very efficient. isn't there a way to do this like defaultFields, so that I don't have to add this piece of code on every CRUD resolvers .. ? |
I am thinking on away to do the merge between default fields and the requested But for now you can for easy way always query simpleExercise.title by adding it to default fields direct without any conditions |
Unfortunately, I am not querying simpleExercise, so even if I add title, it is not queried in my original request. What I am think now, is to create an helper method that generate the PrismaSelect AND apply a given mergeDeep object based on some kind of logic similar to the defaultFields one. However, as of today, I am relying on this to determine if the computed field is requested : const originalQuery = select.valueWithFilter('');
if (originalQuery?.select?.title) {
} which works only if the compute field is at the parent level. What would be great, would be to be able to generate directly from the defaultFields object, not only a parent-level fields flat object but also a mergeDeep compatible one that would be applied automatically or manually. And then we could easily add general logic to query nested fields based on a parent field condition. I am thinking of doing that manually but I don't think there is an easy way to check if a given field is present on a given GraphQL type with prisma-tools, you for sure do that already within the library but no method of this kind is exposed appart from the defaultFields which is not flexible enough. I am also thinking of doing that manually, but then we would be analyzing the graphQL twice (1 within prisma-tools and once more with my code). I really think that there is some kind of easy fix to that, as it seems you already do everything, but I don't know the inside of prisma-tool enough to expose the appropriate method. |
Hi there,
Thanks for your awesome library, it is so great to optimize GraphQL queries!
Problem Description
I am trying to do the following:
I have a type
Parent
which contains an array ofChild
.It is easy to define the
defaultFields
for theParent
, and also for theChild
, but what I am trying to do is to adddefaultFields
to theChild
if theParent
has some field inselect
.Attempted Solution
I have tried this:
But it only works if children is not requested in the GraphQL query. As soon as it is requested, the children.select is overridden.
Unfortunately, in the Child.select function, I don’t have the parent select fields, so I can’t add the fields there.
Desired Outcome
Is there any solution to this problem?
Basically, what I need to do is to add computed fields on the Parent which need info from Child, and I’d like to avoid an additional query as I could easily compute it if children are present.
Thanks for your help.
The text was updated successfully, but these errors were encountered: