您需要将正则表达式字符串作为解析器使用的输入参数,GraphQL 不会为您执行过滤器,您需要根据您的输入在解析器中执行/调用该逻辑。
根据您的示例,您可以在架构和解析器上使用类似的内容:
type Node {
name: String!
}
type NodeQueries {
nodes (filterRegEx :String): [Node]!
}
一旦你在解析器上输入了字符串,过滤机制的实现就取决于你了。
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'}]
}
}
...
}