将 hotchocolate 的过滤器与模式优先方法结合使用

Using hotchocolate's filters with schema first approach

我正在使用模式拦截器来配置我的模式。这是一个多租户应用程序,因此我根据租户的配置构建架构。我将该配置映射到 SDL 语言(模式优先方法),然后将其添加到模式构建器 (schemaBuilder.AddDocumentFromString(...))。

如文档 (here) 所述,“架构优先目前不支持过滤!”。但这是我现在唯一可以使用的方法,所以我正在努力寻找解决方法。

我尝试过的:

手动创建输入过滤器类型并将过滤添加到服务器(像这样):

      ...
      schemaBuilder.AddDocumentFromString(@"
            type Query {
                persons(where: PersonFilterInput): [Person]
            }

            input PersonFilterInput {
                and: [PersonFilterInput!]
                or: [PersonFilterInput!]
                name: StringOperationFilterInput
            }

            input StringOperationFilterInput {
                and: [StringOperationFilterInput!]
                or: [StringOperationFilterInput!]
                eq: String
                neq: String
                contains: String
                ncontains: String
                in: [String]
                nin: [String]
                startsWith: String
                nstartsWith: String
                endsWith: String
                nendsWith: String
            }
            }

            type Person {
                name: String
            }");

        ...
        //add resolvers
        ...

并且在服务器配置上:

      services
            .AddGraphQLServer()
            .TryAddSchemaInterceptor<TenantSchemaInterceptor>()
            .AddFiltering();

但是,这还不够,因为没有应用过滤器。

查询:

{
   persons (where: { name: {eq: "Joao" }}){
     name
   }
}

结果:

{
    "data": {
        "persons": [
            {
                "name": "Joao"
            },
            {
                "name": "Liliana"
            }
        ]
    }
}

有什么办法可以解决这个问题吗?

谢谢大家

版本 12 将提供对架构优先的过滤器支持。您甚至不必指定所有内容,因为我们将提供架构构建指令。

type Query {
  persons: [Person] @filtering
}

type Person {
  name: String
}

您还可以控制可以提供哪些过滤操作。本周我们将推出第一个预览版。