Prisma.$exists returns true when really false


#1

I followed @pantharshit00 recommendation on best resolver pattern for upvotes/downvotes from another post, Best Practices for Upvote/Downvote, with no luck.

After a while, I replaced the exists function with a query and everything functioned as expected. I am successfully using prisma-client queries and mutations elsewhere in the app.

It seems as though $exists is returning true no matter what.


#2

It really helps if you post the actual code.


#3

since it was related to the post I referenced I was trying not to be redundant but here it is:

async upvoteBill(parent, args, ctx, info) {
	const { userId } = ctx.request;
	if (!userId) {
		throw new Error('You must be logged in');
	}
	const upvoted = await ctx.prisma.$exists.bill({
		id: args.id,
		upvotes: {
			id: userId
		}
	});
	const downvoted = await ctx.prisma.$exists.bill({
		id: args.id,
		downvotes: {
			id: userId
		}
	});
	if (upvoted && downvoted) {
		console.log('found upvote and downvote. disconnecting one.');
		upvote = await ctx.db.mutation.updateBill({
			data: {
				downvotes: {
					disconnect: {
						id: userId
					}
				}
			},
			where: {
				id: args.id
			}
		});
	} else if (upvoted) {
		console.log('undoing previous upvote');
		upvote = await ctx.db.mutation.updateBill({
			upvotes: {
				disconnect: {
					id: userId
				}
			},
			where: {
				id: args.id
			}
		});
	} else if (downvoted) {
		console.log('previously downvoted. changing your mind...');
		upvote = await ctx.db.mutation.updateBill({
			data: {
				downvotes: {
					disconnect: {
						id: userId
					}
				},
				upvotes: {
					connect: {
						id: userId
					}
				}
			},
			where: { id: args.id }
		});
	} else {
		console.log('default');
		upvote = await ctx.db.mutation.updateBill({
			data: {
				upvotes: {
					connect: { id: userId }
				}
			},
			where: { id: args.id }
		});
	}
	return upvote;
},

async downvoteBill(parent, args, ctx, info) {
	const { userId } = ctx.request;
	if (!userId) {
		throw new Error('You must be logged in');
	}

	const upvoted = await ctx.prisma.$exists.bill({
		id: args.id,
		upvotes: {
			id: userId
		}
	});
	
	const downvoted = await ctx.prisma.$exists.bill({
		id: args.id,
		downvotes: {
			id: userId
		}
	});	

	let downvote;
	if (upvoted && downvoted) {
		downvote = await ctx.db.mutation.updateBill({
			data: {
				upvotes: {
					disconnect: {
						id: userId
					}
				}
			},
			where: {
				id: args.id
			}
		});
	} else if (downvoted) {
		downvote = await ctx.db.mutation.updateBill({
			data: {
				downvotes: {
					disconnect: {
						id: userId
					}
				}
			},
			where: {
				id: args.id
			}
		});
	}
	if (upvoted) {
		downvote = await ctx.db.mutation.updateBill({
			data: {
				upvotes: {
					disconnect: {
						id: userId
					}
				},
				downvotes: {
					connect: {
						id: userId
					}
				}
			},
			where: {
				id: args.id
			}
		});
	} else {
		downvote = await ctx.db.mutation.updateBill({
			data: {
				downvotes: {
					connect: {
						id: userId
					}
				}
			},
			where: { id: args.id }
		});
	}
	return downvote;
},

#4

you are mixing syntax of prisma binding and prisma client. Can you please share your package.json


#5

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.