【问题标题】:DotNet Core's GraphQL casting parameter value to integerDotNet Core 的 GraphQL 将参数值转换为整数
【发布时间】:2020-05-02 19:09:49
【问题描述】:

我正在使用 graphql-dotnet (dotnet GraphQl) 在 DotNet Core 2.1 中实现 GraphQL。我有以下课程:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class CustomerInputType : InputObjectGraphType
{
    public CustomerInputType()
    {
        Name = "CustomerInput";
        Field<NonNullGraphType<IntGraphType>>(nameof(Customer.Id));
        Field<NonNullGraphType<StringGraphType>>(nameof(Customer.Name));
    }
}
public class CustomerOutputType : ObjectGraphType<Customer>
{
    public CustomerOutputType()
    {
        Name = "Customer";
        Field<NonNullGraphType<IntGraphType>>(nameof(Customer.Id));
        Field<NonNullGraphType<StringGraphType>>(nameof(Customer.Name));
    }
}
public class CustomerMutation : ICustomerMutation
{
    public void Resolve(GraphQLMutation graphQLMutation)
    {
        graphQLMutation.FieldAsync<CustomerOutputType, Customer>
        (
            "createCustomer",
            arguments: new QueryArguments
            (
                new QueryArgument<NonNullGraphType<CustomerInputType>> { Name = "customer"}
            ),
            resolve: context =>
            {
                var customer = context.GetArgument<Customer>("customer");
                return Task.FromResult(customer);
            }
        );
    }
}

这是我通过 GraphIQL 发送给这个突变的输入:

mutation createCustomer
{ 
    createCustomer(customer:{id: 19, name: "Me"})
    {id name} 
}

这是 C# 代码中的输入:

手表显示 id 的值是 0x00000013 而不是 19

这是 GraphIQL 中的输出:

我需要使用19 的值在注入此突变的存储库中进行进一步处理。但是,当我将 Customer 对象传递到存储库时,Customer 对象中 id 的值是 0x00000013,这会导致进一步的下游处理失败,因为它期待的是 19 而不是 0x00000013

为什么 C# 代码中对象 0x00000013 的值却被转换为 GraphIQL 中的实际值?如何在突变返回结果之前将 Cusutomer.id 的值转换为正确的整数值以进行进一步处理?

【问题讨论】:

    标签: c# graphql graphiql graphql-dotnet


    【解决方案1】:

    没错,数值只是显示为十六进制。是你的检查员让你感到困惑。 13 hex 作为二进制是

    10011
    

    那是十进制

    19
    

    https://www.rapidtables.com/convert/number/hex-to-decimal.html

    【讨论】:

    • 是的,它显示为十六进制。为什么在 Visual Studio 中这样做
    • 调试的时候试试右键面板看看能不能改设置stackoverflow.com/questions/3354453/…
    • 是的,我是个白痴。无论出于何种原因,打开了十六进制显示。谢谢你。看代码太长了……
    • 这发生在我们所有人身上! =D
    猜你喜欢
    • 1970-01-01
    • 2019-09-24
    • 2020-03-12
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    相关资源
    最近更新 更多