简体   繁体   English

如何在GraphQL中检查具有正则表达式的数组

[英]How to check an array with regular expression in GraphQL

I need to check the existence of some elements in an array as such 我需要检查数组中是否存在某些元素

I have an array as such 我有一个阵列

ar = ['one','two','three'] ar = ['one','two','three']

I want to know how I can individually check the elements in the regular expression code below instead of "/something/" that would map through my array and check if they exist in graphQL one by one. 我想知道如何单独检查下面的正则表达式代码中的元素而不是“/ something /”,它将映射到我的数组并检查它们是否一个接一个地存在于graphQL中。

similar : allCockpitHello (filter: {Association : {value : {regex: "\/something/" }}} limit:2){
      nodes{
        Name{
          value
        }
}

GraphQL is not a magic bullet - it's only a query language, it 'transports' your needs to the engine (local client, remote server ...) where all the necessary processing takes place. GraphQL不是一个神奇的子弹 - 它只是一种查询语言,它将您的需求“传输”到引擎(本地客户端,远程服务器......),在那里进行所有必要的处理。

In this case you probably need to pass your array and expression as variables to the server (resolver). 在这种情况下,您可能需要将数组和表达式作为变量传递给服务器(解析程序)。 If processing is expensive results (similar relation) should be already defined, cached, preprocessed, etc. 如果处理成本昂贵,那么结果(类似关系)应该已经定义,缓存,预处理等。

If dataset is small you can do this entirely client-side - iterate over an array (fetched using graphql). 如果数据集很小,您可以完全在客户端执行此操作 - 遍历数组(使用graphql获取)。

You need to have the regex string as an input parameter to be used by the resolver, GraphQL is not going to do the filter for you, you need to do/call that logic in the resolver based on your inputs. 您需要将正则表达式字符串作为解析器使用的输入参数,GraphQL不会为您执行过滤,您需要根据您的输入在解析程序中调用/调用该逻辑。

Based on your example, you could have something like this on the schema and resolver: 根据您的示例,您可以在架构和解析器上使用类似的内容:

type Node {
   name: String!
}

type NodeQueries {
   nodes (filterRegEx :String): [Node]!
}

Once you have the input string on the resolver, the implementation of the filter mechanism is up to you. 在解析器上有输入字符串后,过滤器机制的实现由您决定。

const resolvers = {
...
NodeQueries: {
    nodes: (parent, params) => {
      const {filterRegEx} = params; // regex input string

      const ar = ['one','two','three'];

      // Create a RegExp based on the input, 
      // Compare the with the elements in ar and store the result...
      // You might end up with ... res = ['one', 'three'];
      // Now map the result to match your schema:

      return _.map(res, name => ({name}) ); // to end up with [{name: 'one'}, {name: 'three'}]
    }
}
...

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM