Levi Lawliet
06/07/2021, 5:02 PMAND operation. I have a schema graphql schema like thisLevi Lawliet
06/07/2021, 5:03 PMtype SavedPost{
id: ID
createdAt: String
post: Post
user: User
note: String
}
type Post{
id: ID!
tags: Tag!
}
type Tag{
id: ID!
name: String!
}Levi Lawliet
06/07/2021, 5:06 PMPost I have been using this query (this is an optional para if a user doesn't supply the para in the query then it would still run)Levi Lawliet
06/07/2021, 5:06 PM// adding tags
if (args.tags && args.tags.length) {
const AND = []
args.tags.forEach((tag) => {
AND.push({
tags: { some: { name: tag.toLowerCase().trim() } },
})
})
where.AND = AND
} else {
where.AND = []
}Levi Lawliet
06/07/2021, 5:07 PMfeed(filter: "test", tags: ["ed", "tech"])Levi Lawliet
06/07/2021, 5:08 PMsavedPots with a query looking something like this savedPostsFeed(filter: "test", tags: ["ed", "tech"]Levi Lawliet
06/07/2021, 5:09 PMDaniel Olavio Ferreira
06/07/2021, 6:12 PMconst resp = ctx.prisma.post.findMany({
where: {
AND: args.tags && args.tags.length ? args.tags.map(tag =>
({tags: { some: { name: { contains: tag } } } })
) : undefined
},
});Levi Lawliet
06/07/2021, 7:30 PMLevi Lawliet
06/07/2021, 7:30 PMLevi Lawliet
06/07/2021, 7:31 PMconst AND =
{
post: {
description: {
constains: args.filter,
mode: "insensitive"
}
}
}Levi Lawliet
06/07/2021, 7:33 PMLevi Lawliet
06/07/2021, 7:33 PMLevi Lawliet
06/07/2021, 7:33 PM({
link: {
tags: { some: { name: tag.toLowerCase().trim() } }
}
})Levi Lawliet
06/07/2021, 7:38 PMUnknown arg '0' in where.AND.0.0 for type SavedLinkWhereInput. Available args:Daniel Olavio Ferreira
06/07/2021, 9:43 PMLevi Lawliet
06/08/2021, 6:41 AMLevi Lawliet
06/08/2021, 6:41 AMLevi Lawliet
06/08/2021, 6:41 AMmodel Link {
id String @id @default(uuid())
createdAt DateTime @default(now())
description String
url String @unique
title String
tags Tag[] @relation(references: [id])
type String
postedBy User? @relation(fields: [postedById], references: [id])
// if someone ever deleted their account but the post should remain there.
postedById String?
severity String
votes Vote[]
downvotes Downvote[]
savedLinks SavedLink[]
}
model Tag {
id String @id @default(uuid())
createdAt DateTime @default(now())
name String @unique
links Link[] @relation(references: [id])
description String
tips String
}
model SavedLink {
id String @id @default(uuid())
createdAt DateTime @default(now())
link Link @relation(fields: [linkId], references: [id])
linkId String
user User @relation(fields: [userId], references: [id])
userId String
note String?
@@unique([linkId, userId])
}Levi Lawliet
06/08/2021, 7:36 AMSavedLinksLevi Lawliet
06/08/2021, 8:18 AMtags in model Link
if (args.tags && args.tags.length) {
const AND = []
args.tags.forEach((tag) => {
AND.push({
tags: { some: { name: tag.toLowerCase().trim() } },
})
})
where.AND = AND
} else {
where.AND = []
}Levi Lawliet
06/08/2021, 8:19 AMtags in model SavedLink should look like this?Levi Lawliet
06/08/2021, 8:19 AMif (args.tags && args.tags.length) {
const AND = []
args.tags.forEach((tag) => {
AND.push({
link: {
tags: { some: { name: tag.toLowerCase().trim() } }
}
})
})
where.AND.push(AND)
}Daniel Olavio Ferreira
06/08/2021, 8:30 AM// # 1
const unwantedTags = await ctx.prisma.tag.findMany({where: {name: {
notIn: ["a", "b"]
}}})
const unwantedTagsNames = unwantedTags.map(tag => tag.name)
const resp = await ctx.prisma.savedLink.findMany({
where: {
link: {
tags: {
every: { name: { notIn: unwantedTagsNames } },
},
},
},
});Daniel Olavio Ferreira
06/08/2021, 8:30 AM// # 2
await ctx.prisma.savedLink.findMany({
where: {
link: {
tags: {
some: { name: { in: ["a", "b"] } },
},
},
},
});Daniel Olavio Ferreira
06/08/2021, 8:31 AMLevi Lawliet
06/08/2021, 8:41 AMLevi Lawliet
06/08/2021, 8:41 AMLevi Lawliet
06/08/2021, 8:41 AMDaniel Olavio Ferreira
06/08/2021, 8:56 AMLevi Lawliet
06/08/2021, 9:42 AMLevi Lawliet
06/08/2021, 9:43 AMDaniel Olavio Ferreira
06/08/2021, 10:03 AM