Bohdan Dorokhin
Bohdan Dorokhin

Reputation: 21

Can input type object contains a property of interface type?

I've only started learning GraphQL on .net platform. If input type object can contains property of interface type then how to configure it in HotChocolate library?

An example of model:

public interface ILocationFilter {
    double Lat { get; set;}
    double Lng { get; set;}
}

public class LocationFilter : ILocationFilter {
// ...
}

public class FilterModel {
    public string Search { get; set; }
    public ILocationFilter Location { get; set; } = new LocationFilter();
// ...
}

GraphQL Query example:

public class Query {
    public IEnumerable<SomeModel> GetByFilter(FilterModel filter) {
// ...
    }
}

Startup.cs example:

// ...
services.AddGraphQL(SchemaBuilder.New()
                        .AddQueryType<Query>()
                        .Create(),
                    new QueryExecutionOptions { IncludeExceptionDetails = true });
// ...
    app.UseGrapQL();
// ...

Right now I get an exception "Unable to infer or resolve a schema type from the type reference Input: ILocationFilter".

BTW: If remove interface everything will be work.

What should I configure to correct working with properties of interface type?

Upvotes: 1

Views: 1465

Answers (1)

Bohdan Dorokhin
Bohdan Dorokhin

Reputation: 21

public class FilterModelType : InputObjectType<FilterModel> {
    protected override void Configure(IInputObjectTypeDescriptor<FilterModel> descriptor)
    {
        descriptor.Field(x => x.Location).Type(typeof(LocationFilter));
    }
}

I've added a new class which describe a FilterModel. After that I also registered this type in Startup.cs

 SchemaBuilder.New()
    .AddType<FilterModelType>()
    .AddQueryType<Query>()
    .Create() 

It works for me.

Upvotes: 1

Related Questions