Defining FieldNameConverter inside GraphQL.Types.Schema is not working in asp.net core

649 Views Asked by At

I am using graphql-dotnet and graphql-dotnet server with asp.net core. I have configured schema in following way.

public class PdsGraphQlSchema: Schema
{
    public PdsGraphQlSchema()
    {
        FieldNameConverter = new PascalCaseFieldNameConverter();
        Query = CommonServiceLocator.ServiceLocator.Current.GetInstance<GraphQlQueries>();
        Mutation = CommonServiceLocator.ServiceLocator.Current.GetInstance<GraphQlMutations>();
    }
}

Here I have added FieldNameConverter = new PascalCaseFieldNameConverter(); but i am not getting the changes at output. Output always camelCased. How i can ignore camel casing or use Pascale casing.

For ConfigureServices I have used following

services.AddGraphQL(_ =>
            {
                _.EnableMetrics = true;
                _.ExposeExceptions = true;
            });

services.AddSingleton();

and inside Configure I have used following

app.UseGraphQL<PdsGraphQlSchema>();
app.UseGraphQLPlayground(new GraphQLPlaygroundOptions
{   Path = "/ui/playground" });

Expecting your help.

2

There are 2 best solutions below

0
On

you must use from NameConverter field in your schema

public class AppSchema : Schema
{
    public AppSchema(IServiceProvider provider)
        : base(provider)
    {
        NameConverter = new GraphQL.Conversion.DefaultNameConverter();
        Query = new AppQuery();
        Mutation = new AppMutation();
    }
}

you have 3 options

1- DefaultFieldNameConverter() without Change,You see what you wrote in the output

2- PascalCaseFieldNameConverter() in the all First Charecter to Lowercase

3- CamelCaseFieldNameConverter() in the all First Charecter to Lowercase

0
On

For this issue, it is caused by DefaultGraphQLExecuter did not set FieldNameConverter in GetOptions.

Try solutions below:

  • Custom DefaultGraphQLExecuter.

    public class MyDefaultGraphQLExecuter<TSchema> : DefaultGraphQLExecuter<TSchema>
         where TSchema : ISchema
    {
    
        public MyDefaultGraphQLExecuter(TSchema schema, IDocumentExecuter documentExecuter, IOptions<GraphQLOptions> options, IEnumerable<IDocumentExecutionListener> listeners, IEnumerable<IValidationRule> validationRules) 
            : base(schema, documentExecuter, options, listeners, validationRules)
        {
        }
    
        protected override ExecutionOptions GetOptions(string operationName, string query, Inputs variables, object context, CancellationToken cancellationToken)
        {
            var options = base.GetOptions(operationName, query, variables, context, cancellationToken);
            options.FieldNameConverter = Schema.FieldNameConverter;
            return options;
        } 
    }
    
  • Replace built-in DefaultGraphQLExecuter

                services.AddGraphQL(options =>
            {
                options.EnableMetrics = true;
                options.ExposeExceptions = Environment.IsDevelopment();                
                //options.
            })
            .AddWebSockets()
            .AddDataLoader();
    
            services.AddMvc();
    
            services.AddTransient(typeof(IGraphQLExecuter<>), typeof(MyDefaultGraphQLExecuter<>));