Hello there!
I'm trying to use the bult-in filtering mechanism to filter a query, but my field is a list.
Initiative.cs
public class Initiative
{
public long Id { get; set; }
public IEnumerable<Tag> Tags { get; set; } // I want to filter Initiatives by Tag.Name
}
Tag.cs
public class Tag
{
public long Id { get; set; }
public string Name { get; set; }
}
What I want to achieve is something like this query:
initiatives(where: { tags_contains: { name: "test" } })
or
initiatives(where: { tags_name_contains: "test" })
So that I could filter out initiatives that have tags with Name that contains "test".
I followed the docs for Custom Filters but without success. That's what I came up with (it won't build)
public class InitiativeTagFilterType : FilterInputType<Initiative>
{
protected override void Configure( IFilterInputTypeDescriptor<Initiative> descriptor)
{
descriptor
.BindFieldsExplicitly()
.Filter(d => d.Tags) // What should I specify here?
.AllowEquals().Name("equals").And()
.AllowContains().Name("contains").And()
.AllowIn().Name("contem");
}
}
How am I supposed to achieve this kind of filter?
Thank you!
Hi @felrugai
To use the filters you have to specify the attribute on the field.
There are two basic principals.
Explicit Binding
You can create your own Filters by extending FIlterInputType<T>. This way you can e.g. only add the Field you like.
Implicit Binding
You can let the filters infer your type. This way all fields are filterable.
Registering Filters
Code First
public class InitiativeType: ObjectType<Initiative>
{
protected override void Configure(IObjectTypeDescriptor<Initiative> descriptor)
{
// Implicit
descriptor.Field(t => t.Tags).UseFiltering();
// In case you want to use your own filter type
descriptor.Field(t => t.Tags).UseFiltering<CustomFilterType>();
}
}
Or Pure Code First
public class Initiative
{
public long Id { get; set; }
[UseFiltering]
public IEnumerable<Tag> Tags { get; set; }
}
Hi @PascalSenn, thanks for the reply!
By using the Pure Code First attribute aproach, I can only filter the tags inside of an Initiative. My goal is to filter all Initiatives by their tags. For example, I have 10 Initiatives, and 5 of them holds a list of Tags which have an Name of "GitHub". I would like to query only those 5, like
initiatives ( where: { tag_name_contains: "GitHub" } ) {}
I think this image can show in details what I mean.
That said, how could I implement an CustomFilter which I believe that would use LINQ to get only the Initiatives that have at least one tag of name "GitHub"?
@felrugai on which version are you? 10.3.x?
@felrugai on which version are you? 10.3.x?
Yes! All my packages are at v10.3.2
OK, that is why. In V11 previews the filters are much more powerful and let you do all those things.
Nice! Im just updating my packages to the prerelease versions.
With v.11.0.0-preview.74 I could accomplish what I wanted with:
initiatives(where: { tags_some: { name_contains: "GitHub" } })
Thanks!