Use generic class with graphql in .net core

Viewed 926

I'm new to GraphQL, I'm making a small website which is for managing students and there classes.

In my application, I have a generic class:

public class RangeModel<TFrom, TTo>
    {
        #region Propertes

        public TFrom From { get; set; }

        public TTo To { get; set; }

        #endregion
    }

This class is for defining a range with generic data type. Range can be:

RangeModel<double?, double?>

RangeModel<int?, int?>

... and so on.

I have tried making a RangeModelType class which inherits ObjectGraphType :

    public class RangeModelType<TFrom, TTo>: InputObjectGraphType<RangeModel<TFrom, TTo>>
        {
            public RangeModelType()
            {
                Field(x => x.From).Description("Minimum range");
                Field(x => x.To).Description("Maximum range");
            }
        }

And defined my query like below:

var studentsQueryArguments = new QueryArguments();
            studentsQueryArguments.Add(new QueryArgument<ListGraphType<IntGraphType>> { Name = "ids", Description = "Student indexes." });
            studentsQueryArguments.Add(new QueryArgument<ObjectGraphType<RangeModelType<double?, double?>>>{Name = "age", Description = "Age range of student."});
            Field<ListGraphType<StudentType>>(
                "students",
                arguments: studentsQueryArguments,
                resolve: context =>
                {
                    // Resolve data...
                    return results;
                });
        }

When I ran my app and did the query. One exception was thrown back which said:

Unable to cast object of type GraphQL.Types.ObjectGraphType1[GraphQlStudy.Models.GraphQL.Types.RangeModelType2[System.Nullable1[System.Double],System.Nullable1[System.Double]]]' to type 'GraphQL.Types.ScalarGraphType'.

I have been searching for tutorials that use GraphQL and generic class, but nothing mentions about using this kind of Generic class.

Can anyone help me ?

Thanks,

1 Answers

After having spent hours, I have the following solution.

  1. Define RangeModel.cs as:

    public class RangeModel<TFrom, TTo>
    {
        public TFrom From { get; set; }
    
        public TTo To { get; set; }
    }
    
  2. Define RangeModelType.cs:

    public class RangeModelType<TFrom, TTo> : InputObjectGraphType<RangeModel<TFrom, TTo>>
    {
        public RangeModelType()
        {
            var fromType = typeof(TFrom);
            Field(x => x.From, fromType.IsGenericType && fromType.GetGenericTypeDefinition() == typeof(Nullable<>));
    
            var toType = typeof(TTo);
            Field(x => x.To, toType.IsGenericType && toType.GetGenericTypeDefinition() == typeof(Nullable<>));
        }
    }
    
  3. This is my query argument:

    studentsQueryArguments.Add(new QueryArgument<RangeModelType<double?, double?>> {Name = "age", Description = "Age range of student."});
    

I don't know whether this implementation is good or not. For now, it solves my problem.

Hope this help.

Related