Entity framework context.GetArgument()返回带有ByteGraphType的null
我正在学习如何在graphql dotnet中使用CustomScalar 我的表中有一个tinyint列,从我读到的内容来看,我应该在C#中使用该列上的byte。经过研究,我发现我需要创建一个ByteGraphType,但我在这方面遇到了困难 我从这个链接得到了ByteGraphType示例,所以我认为它会起作用 使用这段代码,我可以查询表,但是,我的变异不起作用。我没有找到一个例子来说明在字节列中的变异是什么样子的。我尝试了代码示例中所述的方法,但在这一行(var avaliacao=context.GetArgument(“avaliacao”);中,我的参数avaliacao.Nota返回null,我不确定如何继续 有人能帮我吗 多谢各位 这是我的密码 //模型Entity framework context.GetArgument()返回带有ByteGraphType的null,entity-framework,byte,tinyint,graphql-dotnet,Entity Framework,Byte,Tinyint,Graphql Dotnet,我正在学习如何在graphql dotnet中使用CustomScalar 我的表中有一个tinyint列,从我读到的内容来看,我应该在C#中使用该列上的byte。经过研究,我发现我需要创建一个ByteGraphType,但我在这方面遇到了困难 我从这个链接得到了ByteGraphType示例,所以我认为它会起作用 使用这段代码,我可以查询表,但是,我的变异不起作用。我没有找到一个例子来说明在字节列中的变异是什么样子的。我尝试了代码示例中所述的方法,但在这一行(var avaliacao=con
[Column("nota")]
public byte Nota { get; set; }
//类型
Field<ByteGraphType>("Nota", resolve: context => context.Source.Nota);
字段(“Nota”,解析:context=>context.Source.Nota);
//输入类型
Field<ByteGraphType>("nota");
字段(“nota”);
//质疑
Field<ListGraphType<AvaliacaoType>>(
"avaliacoes",
resolve: context => contextServiceLocator.AvaliacaoRepository.All());
字段(
“阿瓦利亚科斯”,
解析:context=>contextServiceLocator.avalicaoRepository.All();
//突变
Field<AvaliacaoType>(
"createAvaliacao",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<AvaliacaoInputType>> { Name = "avaliacao" }
),
resolve: context =>
{
var schema = new Schema();
schema.RegisterValueConverter(new ByteValueConverter());
var avaliacao = context.GetArgument<Avaliacao>("avaliacao");
avaliacao.Nota.AstFromValue(schema, new ByteGraphType());
return contextServiceLocator.AvaliacaoRepository.Add(avaliacao);
});
字段(
“createAvaliacao”,
参数:新的QueryArguments(
新查询语言{Name=“avaliacao”}
),
解析:上下文=>
{
var schema=newschema();
schema.RegisterValueConverter(新的ByteValueConverter());
var avaliacao=context.GetArgument(“avaliacao”);
AstFromValue(schema,newbytegraphtype());
返回contextServiceLocator.AvaliacaoRepository.Add(avaliacao);
});
//按位图类型
using GraphQL.Language.AST;
using GraphQL.Types;
using System;
namespace Api.Helpers
{
public class ByteGraphType : ScalarGraphType
{
public ByteGraphType()
{
Name = "Byte";
}
public override object ParseLiteral(IValue value)
{
var byteVal = value as ByteValue;
return byteVal?.Value;
}
public override object ParseValue(object value)
{
if (value == null)
return null;
try
{
var result = Convert.ToByte(value);
return result;
}
catch (FormatException)
{
return null;
}
}
public override object Serialize(object value)
{
return ParseValue(value).ToString();
}
public class ByteValueConverter : IAstFromValueConverter
{
public bool Matches(object value, IGraphType type)
{
return value is byte;
}
public IValue Convert(object value, IGraphType type)
{
return new ByteValue((byte)value);
}
}
public class ByteValue : ValueNode<byte>
{
public ByteValue(byte value)
{
Value = value;
}
protected override bool Equals(ValueNode<byte> node)
{
return Value == node.Value;
}
}
}
}
使用GraphQL.Language.AST;
使用GraphQL.Types;
使用制度;
名称空间Api.Helpers
{
公共类ByteGraphType:ScalarGraphType
{
公共ByteGraphType()
{
Name=“Byte”;
}
公共重写对象ParseLiteral(IValue值)
{
var byteVal=作为字节值的值;
返回字节值?.Value;
}
公共重写对象值(对象值)
{
如果(值==null)
返回null;
尝试
{
var结果=换算成字节(值);
返回结果;
}
捕获(格式化异常)
{
返回null;
}
}
公共重写对象序列化(对象值)
{
返回ParseValue(value.ToString();
}
公共类ByteValueConverter:IAstFromValueConverter
{
公共布尔匹配(对象值,IGraphType类型)
{
返回值为字节;
}
公共IValue转换(对象值,IGraphType)
{
返回新的字节值((字节)值);
}
}
公共类字节值:ValueNode
{
公共字节值(字节值)
{
价值=价值;
}
受保护的覆盖布尔等于(ValueNode节点)
{
返回值==节点值;
}
}
}
}
我需要的是能够保存具有tinyint列的表的记录。如果我将代码中的类型更改为int,我可以变异,但不能查询。我更改了CustomScalar,它成功了:
using GraphQL.Language.AST;
using GraphQL.Types;
using System;
namespace Api.Helpers
{
public class ByteGraphType : ScalarGraphType
{
public ByteGraphType()
{
Name = "Byte";
Description = "ByteGraphType";
}
/// <inheritdoc />
public override object Serialize(object value)
{
return ParseValue(value).ToString();
}
/// <inheritdoc />
public override object ParseValue(object value)
{
byte result;
if (byte.TryParse(value?.ToString() ?? string.Empty, out result))
{
return result;
}
return null;
}
/// <inheritdoc />
public override object ParseLiteral(IValue value)
{
if (value is StringValue)
{
return ParseValue(((StringValue)value).Value);
}
return null;
}
}
}
使用GraphQL.Language.AST;
使用GraphQL.Types;
使用制度;
名称空间Api.Helpers
{
公共类ByteGraphType:ScalarGraphType
{
公共ByteGraphType()
{
Name=“Byte”;
Description=“ByteGraphType”;
}
///
公共重写对象序列化(对象值)
{
返回ParseValue(value.ToString();
}
///
公共重写对象值(对象值)
{
字节结果;
if(byte.TryParse(值?.ToString()?.string.Empty,输出结果))
{
返回结果;
}
返回null;
}
///
公共重写对象ParseLiteral(IValue值)
{
if(值为StringValue)
{
返回ParseValue(((StringValue)value).value);
}
返回null;
}
}
}