2

here I have a mutation, as you can see the parameter name is Product with capital P and the field name is CreateProduct with capital C when I execute this mutation from graphical I have to write the name of the field on camel casing and also the name of the parameter, is there a way to configure graphql-dotnet to respect the names as they are written in the code?

 const string STR_Product = "Product";
    public Mutations(IProductService ProductService)
    {
        Name = "Mutation";
        Field<ProductType>(
            "CreateProduct",
            arguments: new QueryArguments(
                new QueryArgument<NonNullGraphType<ProductInputType>> { Name = STR_Product }),
            resolve: context =>
            {
                var ProductInput = context.GetArgument<ProductInput>(STR_Product);
                return ProductService.CreateAsync(ProductInput.Code, ProductInput.Name, ProductInput.Description);
                //return new ProductInputType();
            }
        );


    }
}
  • [you can see answer here best Option in Schema](https://stackoverflow.com/a/67096446/3400445) – JAvAd Apr 14 '21 at 17:40

2 Answers2

1

You can pass a IFieldNameConverter to the ExecutionOptions. It defaults to using CamelCaseFieldNameConverter.

Some special considerations are needed for the Introspection types, as those are required to be camel case according to the GraphQL Specification.

GraphQL.NET provides CamelCaseFieldNameConverter, PascalCaseFieldNameConverter, and DefaultFieldNameConverter. You could also write your own. Source found here.

using System;
using GraphQL;
using GraphQL.Types;

public class Program
{
  public static void Main(string[] args)
  {
    var schema = Schema.For(@"
      type Query {
        Hello: String
      }
    ");

    var json = schema.Execute(_ =>
    {
      _.Query = "{ Hello }";
      _.Root = new { Hello = "Hello World!" };
      _.FieldNameConverter = new DefaultFieldNameConverter();
    });

    Console.WriteLine(json);
  }
}
Joe McBride
  • 3,789
  • 2
  • 34
  • 38
  • This is for the server. The server defines what fields are available. See the intro docs for this syntax. https://graphql-dotnet.github.io/docs/getting-started/introduction – Joe McBride Apr 26 '19 at 00:11
  • schema.Execute is just an extension method that wraps `DocumentExecuter` and `DocumentWriter`. https://github.com/graphql-dotnet/graphql-dotnet/blob/144d81214c4ae3aa5a506afa77a8f22e288460a5/src/GraphQL/SchemaExtensions.cs – Joe McBride Apr 26 '19 at 00:15
1

See the above answer by Joe McBride, but instead of "FieldNameConverter" should be using "NameConverter". Example:

var json = schema.Execute(_ =>
    {
      _.NameConverter = new DefaultNameConverter(); //or PascalNameConverter, etc.
    });

(posted as an answer because I am unable to comment)