How to register generic class when using AddAutoSchema in GraphQL.net?

16 Views Asked by At

I am adding Graphql schema (using graphql-dotnet library https://graphql-dotnet.github.io/) like this:

services.AddGraphql(options => {
    options.AddAutoSchema<QuerySchema>(q =>
    {
        q.WithMutation<MutationSchema>();
    });
})

I am not registering any Graphql types, just using CLR types that are auto added to the schema. However there is a problem with generic classes like this one:

// Reusable generic class
public class GraphqlError<TErrorCode>
{
    public required TErrorCode ErrorCode { get; init; }
    
    public required string? Message { get; init; }
}

public enum ErrorCodeA { A1, A2 }
public enum ErrorCodeB { B1, B2 }

// Class A referencing GraphqlError class
public class A {
    public required GraphqlError<ErrorCodeA> Error { get; init; } 
}

// Class A referencing GraphqlError class
public class B {
    public required GraphqlError<ErrorCodeB> Error { get; init; } 
}

In such case GraphQL throws following error:

Unable to register GraphType 'AutoRegisteringObjectGraphType<GraphqlError<ErrorCodeB>>' with the name 
'GraphqlError'. The name 'GraphqlError' is already registered to 
'AutoRegisteringObjectGraphType<GraphqlError<ErrorCodeA>>'. 

So what I need is somehow dynamically set name (GQL name) of generic class taking into account name of generic argument, but no idea how to do this...

0

There are 0 best solutions below