这里我有一个突变,你可以看到参数名称是大写 P 的 Product,字段名称是大写 C 的 CreateProduct 当我从图形执行此突变时,我必须在驼峰外壳上写下字段的名称,并且还有参数的名称,有没有办法配置 graphql-dotnet 以尊重代码中编写的名称?
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();
}
);
}
}
最佳答案
您可以将 IFieldNameConverter
传递给 ExecutionOptions
。默认使用CamelCaseFieldNameConverter
。
对于内省(introspection)类型需要一些特殊的考虑,因为根据 GraphQL 规范,这些类型需要采用驼峰式大小写。
GraphQL.NET 提供了 CamelCaseFieldNameConverter
、PascalCaseFieldNameConverter
和 DefaultFieldNameConverter
。您也可以自己编写。 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);
}
}
关于graphql - 如何在 graphql dotnet 中使用 pascal 大小写代替 Camel 大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55853230/