Can we access to related fields in @Root ? #224
-
Hello, I'm trying to filter some nested field but can't figure it out how to do it. I saw this post: #126 and that's exactly what I'm looking for. This is the query I sent from my frontend stores(where:{productId:{some:{id:{equals:8}}}}){
id
name
products{
storeId
productId
name
} I want to get all stores filtered by productId but also I want products to only match the given product. So I tried to filter it with prisma in my backend @Query((returns) => UserSearchResponse, { nullable: true })
async search2(
@Ctx() { prisma }: typeof context,
@Args() { userId, productId, categoryId, query, location }: SearchArgs,
): Promise<UserSearchResponse | null> {
...
const stores = await prisma.store.findMany({
where: {
product: {
some: {
status: true,
productId,
},
},
},
include: {
product: {where: {id: productId}}
}
})
...
} but this isn't working. Then I tried to create a custom field resolver @Resolver(of => Store)
export class CustomStoreResolver {
// queries and mutations
@FieldResolver(type => Product, { nullable: true })
async productFiltered(
@Ctx() { prisma }: typeof context,
@Arg("productId", type => Int) productId: number,
@Root() store: Store
) {
const [filteredProduct] = await prisma.store
.findUnique({ where: { id: store.id } })
.product({ where: { productId } });
//return store.product.filter(item => item.productId === productId);
return filteredProduct;
}
} I would have expected to get access to related fields on the Root object but I only have access to scalars field. So how can I get access to product so I avoid having to make an extra prisma call ? This is the updated query sent from the frontend stores(where:{productId:{some:{id:{equals:8}}}}){
id
name
productFiltered(productId: 8){
storeId
productId
name
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
@MichalLytek based on this comment I'm assuming creating a custom field resolver is the way to go #112 (comment) since it looks I can't filter on relational fields, right? Is there a way to get access to a relational field from the Root object? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Well.. don't know what to say. Now I can have access to relational fields in the @root object. Not sure why I counld't before. The only change I suspect is related is this I use to use this import {
crudResolvers, relationResolvers, ResolversEnhanceMap,
applyResolversEnhanceMap
} from '../generated/type-graphql';
const schema = await tq.buildSchema({
resolvers: [
...crudResolvers,
...relationResolvers,
other resolvers,
],
authChecker: customAuthChecker,
authMode: "error",
validate: true,
globalMiddlewares: [ErrorInterceptor],
}); and now I have this import {
resolvers, ResolversEnhanceMap,
applyResolversEnhanceMap
} from '../generated/type-graphql';
const schema = await tq.buildSchema({
resolvers: [
...resolvers,
other resovers
],
authChecker: customAuthChecker,
authMode: "error",
validate: true,
globalMiddlewares: [ErrorInterceptor],
}); I'd like to avoid having to create a custom field resolver but for now I only was cared about not having to make that extra prisma query. I'd love to hear if it's possible to use filters on fields when you use |
Beta Was this translation helpful? Give feedback.
Well.. don't know what to say. Now I can have access to relational fields in the @root object. Not sure why I counld't before. The only change I suspect is related is this
I use to use this
and now I have this