Filtering on many-many child relationship


#1

We have two types, Content and Tags, with a many-many relationship defined below:

type Content @model {
  id: ID! @isUnique
  tags: [Tag!]! @relation(name: "ContentOnTags")
  title: String
}

type Tag @model {
  contents: [Content!]! @relation(name: "ContentOnTags")
  id: ID! @isUnique
  name: String!
}

Given a search string, I would like to return content that has one or more tags where name_contains the search string. Not sure what to put in the middle here:

query(
   $search: String
) {
   allContents(filter: {
     ????
}) {
   id
   title
}

So far this is one or two steps more advanced than any other examples I have been able to find. Any help would be appreciated.


#2

In your schema.graphql you should must have inputs related with Content and Tag types. You must to use them in order to achieve that. By example

input ContentFilter {
   /* a lot of another fields*/
   tags_some: TagFilter
}

you must to define a filter object, something like that:

const filter  = { tags_some: { name_contains: yourfilter } }

Then your query must be:

query( $filter: ContentFilter!) {
   allContents(filter: $filter
}) {
   id
   title
}

Just in case you must to send filter object in your variables.

Your solution must be pretty similar to this.


#3

Figured out the simple answer:

query(
    $search: String
) {
    allContents(filter: {
        tags_some: {
            name_contains: $search
        }
    }) {
        id
        title
    }
}