'HotChocolate (GraphQL) dynamic schemas and filtering
I'm using HotChocolate for our GraphQL server in .NET and CosmosDb as DB, which is a document database. Sorry for a long post...
In our Db we will have one part of the document that is different depending on product type. Lets say that one product type is "Shoes", and another is "Jackets".
// A product with type "Shoes"
{
"id": "123",
"productType": "Shoes",
"specific": {
"shoeSize": 4,
"isSandal": true,
}
}
// A product with type "Jackets"
{
"id": "888",
"productType": "Jackets",
"specific": {
"fluffyness": 12,
"backPrint" : "Gretzky 99"
}
}
It should be possible for a Content Editor in our system to add new product types and add new fields to it, on a running system (cms-style). This means that the schema should be created dynamically.
This article and the provided github-project describes creating new object types "on the fly", but I don't understand how to create inputfilters, so its also possible to query on the new fields. (It doesn't work, just trying to create a FilterInputTypeDefinition)
I have done a small experiment (not used the article way yet), adding the property "shoeSize". I extend the product type with shoeSize-property, that is stored a dictionary on the .NET-basetype.
public class ProductType : ObjectType<Product>
{
protected override void Configure(IObjectTypeDescriptor<Product> descriptor)
{
descriptor.Field("lengthInMeter").Resolve(context =>
{
Product parent = context.Parent<Product>();
return parent.Specific.ContainsKey("shoeSize") ? parent.Specific["shoeSize"] : null;
}).Type<IntType>();
}
}
This works as expected and I can specify the lenghtInMeter as a field in my gql-query. What I cannot do is a query like this
getProducts(where : { shoeSize: {lg : 4}}) //using property shoeSize is not possible
, since the input type, doesnt have lengthInMeter, just the ProductType. How to I do that
in a hardcoded sense, ie inherit from FilterInputType more importantly following the example in the link and also creating a filterinputtype with new fields.
Will this create a correct IQueryable
that can be translated to a SQL for my database? If not: where can I intercept / take over that that IQueryable
creation?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|