Hi, I am trying to search for tags in graphql and ...
# orm-help
l
Hi, I am trying to search for tags in graphql and need some help with the Prisma
AND
operation. I have a schema graphql schema like this
Copy code
type SavedPost{
  id: ID
  createdAt: String
  post: Post
  user: User
  note: String
}

type Post{
  id: ID!
  tags: Tag!
}

type Tag{
  id: ID!
  name: String!
}
For searching tags in
Post
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)
Copy code
// 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 = []
    }
and is for if someone issues a query like
feed(filter: "test", tags: ["ed", "tech"])
I want to be able to find tags in the
savedPots
with a query looking something like this
savedPostsFeed(filter: "test", tags: ["ed", "tech"]
How would the where condition be like then? I am very confused
d
Hey @Levi Lawliet I believe something like this is what you are looking for, if not, help me understand better what you are looking for.
Copy code
const resp = ctx.prisma.post.findMany({
      where: {
        AND: args.tags && args.tags.length ? args.tags.map(tag =>
			({tags: { some: { name: { contains: tag } } } })
			) : undefined
      },
    });
l
Hi sorry for not being clear, I was trying to search for tags in saved posts, like
when doing filtering,
Copy code
const AND =
{
   post: {
       description: {
             constains: args.filter,
             mode: "insensitive"
       }
    }
}
How would the same work got tags?
Like would it work something like this @Daniel Olavio Ferreira
Copy code
({
      link: {
              tags: { some: { name: tag.toLowerCase().trim() } }
             }
     })
This though, doesn't work and throws an error
Unknown arg '0' in where.AND.0.0 for type SavedLinkWhereInput. Available args:
d
@Levi Lawliet could you share how your models are defined, I believe this will help me help you. How does the Post model relate to the Tags model?
l
Yeah, sorry about the confusion, I was using post to make it little less confusing
I am using this as my schema
Copy code
model 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])
}
I want to be able to search through the tags in
SavedLinks
This is the query I am using to search through
tags
in model
Link
Copy code
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 = []
    }
So, the query to search
tags
in model
SavedLink
should look like this?
Copy code
if (args.tags && args.tags.length) {
        const AND = []
        args.tags.forEach((tag) => {
            AND.push({
                link: {
                    tags: { some: { name: tag.toLowerCase().trim() } }
                }
            })
        })
        where.AND.push(AND)
    }
d
@Levi Lawliet I came up with two possible solutions, could you give them a try?
Copy code
// # 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 } },
					},
				},
			},
		});
Copy code
// # 2

		await ctx.prisma.savedLink.findMany({
			where: {
				link: {
					tags: {
						some: { name: { in: ["a", "b"] } },
					},
				},
			},
		});
🙌 1
Assuming ["a", "b"] are the tags that you are looking for
l
Hi thanks!!
for some reason it works with writing query this way
prisma rainbow 1
and fails with AND
d
I believe you are looking at the AND in a different way than it is implemented, maybe the docs can help you better understand it https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#and
🙌 1
l
Yeah, I had it all wrong!
Thanks again for the help!
d
I'm happy to help