Search code examples
asp.nethotchocolate

Using hotchocolate's filters with schema first approach


I'm using a schema interceptor to configure my schema. It's a multi-tenant application, so I build the schema according to the tenant's configuration. I'm mapping that configuration to SDL language (schema-first approach) and then I add it to the schema builder (schemaBuilder.AddDocumentFromString(...)).

As said on the documentation (here), "Schema-first does currently not support filtering!". But that is the only approach I can use right now, so I'm trying to find a workaround.

What I've tried:

Manually create the input filter types and add the filtering to the server (something like this):

      ...
      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
        ...

And on the server configuration:

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

However, this is not enough because the filters aren't being applied.

Query:

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

Results:

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

Is there anything I can do to workaround this problem?

Thank you people


Solution

  • Filter support for schema-first is coming with version 12. You then do not even have to specify everything since we will provide schema building directives.

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

    you also will be able to control which filter operations can be provided. We have the first preview coming up this week.