简体   繁体   中英

GraphQl .NET DateGraphType CLR Type Mapping Issue

After upgrading my GraphQL.NET version from v4-v7 I'm running into an issue I can't seem to solve. The solution is also running on .NET 7 which I also recently upgraded.

I've tried to search for a resolution to these errors in the migration guides but I must be missing something. Currently any code I have referencing a DateGraphType that is also nullable is throwing the below error:

The GraphQL type for argument 'DateMutations.updateDate.value' could not be derived implicitly. Could not find type mapping from CLR type 'GraphQL.Types.DateGraphType' to GraphType. Did you forget to register the type mapping with the 'ISchema.RegisterTypeMapping'?

Here is what that code looks like from the field:

            Field<BooleanGraphType>("updateDate")
                .Argument<DateGraphType>("newDate", true)
                .Argument<int>("id")
                .Resolve()
                .WithScope()
                .WithService<IMediator>()
                .ResolveAsync(UpdateDateResolver);

If I remove the code snippet that is implying this is a nullable argument, like so:

            Field<BooleanGraphType>("updateDate")
                .Argument<DateGraphType>("newDate")
                .Argument<int>("id")
                .Resolve()
                .WithScope()
                .WithService<IMediator>()
                .ResolveAsync(UpdateDateResolver);

My schema continues "generating" and just finds the next argument somewhere else in the schema that has the type DateGraphType with the nullable option passed in as true. The rest of the arguments in the solution work if the nullable parameter isn't passed.

To reiterate, all of this code worked correctly before I upgraded from v4 so I think it's got to be something I've missed in the migration guides.

I figured it out. After the transition to GraphQL v7 they added in CLR types to argument generics and that through me for a loop a little bit.

When using the syntax

.Argument<TGraphType>("name")

It is defaulted in as nullable, like it always was. To get a non-nullable type you have to do the following.

.Argument<NonNullGraphType<TGraphType>("name")

However with the addition of CLR types they have a slightly different syntax similar to the Field syntax of .Field(x => x, true) . When using a CLR type you must pass the nullable attribute in separately. However, CLR types default as non-nullable.

.Argument<TClrType>("name") // defaults as nullable: false

If you want a CLR type to be nullable you must pass in the extra paramter.

.Argument<TClrType>("name", true) // now it's nullable

I ended up asking this question in the GitHub repo as an issue, here's a link where the issue was explained to me in case you need more clarity.

https://github.com/graphql-do.net/graphql-do.net/issues/3507

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM