'Graphql how to create a complex filter

I am creating an app where teachers can search for resources. Each resource is tagged with topics that are used to filter results. I have a complex use case and am looking for some advice.

For example:

  • Resource 1 is tagged with the topic "Maths". Topic "Maths" has a topic label "Subject", which is tier 1

  • Resource 2 is tagged with the topic "Algebra". Topic "Algebra" has a topic label "Unit", which is tier 2

  • Resource 2 is tagged with the topic "2019". Topic "2019" has the topic label "Year", which is tier 1

  • Resource 2 is tagged with the topic "Calculator". Topic "Calculator" has a topic label "Question Type", which is tier 1

  • Resource 3 is tagged with the topic "Algebra". Topic "Algebra" has a topic label "Unit", which is tier 2

  • Resource 3 is tagged with the topic "2018". Topic "2018" has a topic label "Year", which is tier 1

I am trying to write a query that allows the user to get all resources that contain the provided topics.

For example:

  • Get me all resources tagged with the topic "2019" and topic "Algebra". This should return only Resource 2 as it has both these tags
  • Get me all resources that are tagged with the topic "Algebra". This should return Resource 2 and Resource 3 as they both are tagged with the topic "Algebra."

My current attempt fails to do this as it does not differntiate between the topics. My query is shown below:

query FilterBlocks($topicIds: [bigint!]) {
  block(
    where: {
      tags: {
        topic_id: { _in: $topicIds, _is_null: false }
      }
    }
  ) {
    id
    tags {
      id
      topic {
        id
        title
      }
    }
    type
    ...
  }
}

Any advice on how to go about this would be much appreciated.



Solution 1:[1]

If I am correct, you are querying for blocks that have a specific tag with a specific topic id.

What you expect: The blocks with just those tags and topics that are actually part of your filter.

What you get: The blocks which fulfil this filter with all their tags and topics.


You need to apply the filter to the subentities as well

query FilterBlocks($topicIds: [bigint!]) {
  block(
    where: {
      tags: {
        topic_id: { _in: $topicIds, _is_null: false }
      }
    }
  ) {
    id
    tags(
      where: {
        topic_id: { _in: $topicIds, _is_null: false }
      }
    ) {
      id
      topic( where: { id: { _in: $topicIds } } ) {
        id
        title
      }
    }
    type
    ...
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Friedrich