【问题标题】:Using hotchocolate's filters with schema first approach使用带有模式优先方法的 hotchocolate 过滤器
【发布时间】:2021-06-01 16:14:38
【问题描述】:

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

如文档 (here) 中所述,“Schema-first 目前不支持过滤!”。但这是我现在可以使用的唯一方法,所以我正在尝试寻找解决方法。

我尝试过的:

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

      ...
      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"
            }
        ]
    }
}

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

谢谢大家

【问题讨论】:

    标签: asp.net hotchocolate


    【解决方案1】:

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

    type Query {
      persons: [Person] @filtering
    }
    
    type Person {
      name: String
    }
    

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

    【讨论】:

    • 太棒了!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2022-06-24
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多