GraphQL:过滤数组中的数据 [英] GraphQL: Filter data in an array

查看:68
本文介绍了GraphQL:过滤数组中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定这很简单,但是在GraphQL的文档或Graphcool的文档中我都找不到.

I'm sure it's a simple thing to do, but I couldn't find anything in either GraphQL's doc or Graphcool's.

说我有一个具有该架构的实体(新的GraphQL用户,如果在架构表示中出错,抱歉):

Say I have an entity with this schema (new GraphQL user, sorry if I make mistake in the schema representation):

Book {
  name: String!
  author: String!
  categories: [String!]
}

我如何查询属于"mystery" 类别的所有书籍?我知道我可以使用 allBooks(filter:{})进行过滤,但是 categories_in:["mystery"] categories_contains:"mystery" 没有没办法.

How would I do a query for all books that are part of the "mystery" category? I know I can filter with allBooks(filter: {}), but categories_in: ["mystery"] and categories_contains: "mystery" didn't do the trick.

推荐答案

类别模型

再仔细考虑一下这种情况,创建 Category 模型绝对是可行的方法.

Category model

Thinking a bit more about this situation, creating a Category model is definitely the way to go.

例如,假设您想让读者以后订阅他们喜欢的类别.或者,如果您要列出所有现有类别的清单,该怎么办?使用字符串列表,您将需要查询所有书籍,并以某种方式对所有获得的类别进行后处理.在模型级别而不是使用字符串列表来处理它会更自然.

For example, imagine you want to allow readers to subscribe to their favorite categories later. Or, what if you want a list of all existing categories? Using string lists, you would need to query all books and somehow postprocess all obtained categories. Handling this on a model level rather than using string lists feels much more natural.

相反,您可以创建一个新的 Category 模型,并在 Category Book 之间添加多对多关系.在这种情况下,我想添加一个唯一的枚举字段 tag 和一个字符串字段 text .(单独的唯一字符串字段 tag 也可能是合适的,可能只是出于口味问题.)

Instead, you can create a new Category model and add a many-to-many relation between Category and Book. In situations like this, I like to add a unique enum field tag and a string field text. (A unique string field tag alone would also be suitable, probably a matter of taste.)

通过此设置,您可以轻松满足

With this setup, you can easily fulfill data requirements like

哪些图书被分配到给定类别?

query {
  # query books by unique category tag
  Category(tag: MYSTERY) {
    books {
      id
    }
  }
  # query books by specific category text
  Category(filter: {
    text: "mystery"
  }) {
    books {
      id
    }
  }
}

哪些图书被分配到给定列表的至少一个类别?

query {
  allCategories(filter: {
    OR: [{
      tag: MYSTERY
    }, {
      tag: MAGIC
    }]
  }) {
    books {
      id
    }
  }
}

哪些图书分配给了给定列表的所有类别?

query {
  allCategories(filter: {
    AND: [{
      tag: MYSTERY
    }, {
      tag: MAGIC
    }]
  }) {
    books {
      id
    }
  }
}

相关过滤器

即使以上查询满足指定的数据要求,响应中的图书仍按 Category 进行分组,这意味着我们必须将客户端上的组拼合在一起.

Related filters

Even though the above queries fulfill the specified data requirements, books are grouped by Category in the response, meaning that we would have to flatten the groups on the client.

使用所谓的相关过滤器,我们可以将其转为仅根据定义了其相关类别的条件获取图书.

With so called related filters, we can turn that around to only obtain books based on conditions defined its related categories.

例如,要查询分配给给定列表的至少一个类别的图书:

query {
  allBooks(filter: {
    OR: [{
      categories_some: {
        tag: MYSTERY
      },
      categories_some: {
        tag: MAGIC
      }
    }]
  }) {
    id
  }
}

这篇关于GraphQL:过滤数组中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆