gpt4 book ai didi

c# - 带有 List> | 的 GraphQL.NET 突变JSON字符串

转载 作者:行者123 更新时间:2023-11-30 18:15:15 25 4
gpt4 key购买 nike

我要注册alarms在我的服务器应用程序上。为了防止传递 10 个以上的参数,我序列化了我的 alarm在客户端并将其作为 List<JSONString> 传递到我的服务器。反序列化它,注册它并给出注册的答案 alarm .

现在我的问题是,我不知道如何传递这些参数:

使用突变 - DictionaryType

Dictionary

"Variable \"$params\" of type \"[String]!\" used in position expecting type \"[DictionaryType]!\"."

使用变异 - StringGraphType

"Cannot convert value to AST: System.Collections.Generic.Dictionary`2[System.String,System.Object]",**

服务器

突变 - 字典类型

public class Mutation : ObjectGraphType
{
public Mutation()
{
Name = "Mutation";

FieldAsync<HtStaticAlarmBaseType>(
"registerStaticAlarms",
"Register a list with static alarms.",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<ListGraphType<DictionaryType>>> {Name = "params"}
),
resolve: async context =>
{
List<object> parameterString = context.GetArgument<List<object>>("params");

//TODO

return null;
}
);
}
}

突变 - 字典类型

public class Mutation : ObjectGraphType
{
public Mutation()
{
Name = "Mutation";

FieldAsync<HtStaticAlarmBaseType>(
"registerStaticAlarms",
"Register a list with static alarms.",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<ListGraphType<StringGraphType>>> {Name = "params"}
),
resolve: async context =>
{
List<object> parameterString = context.GetArgument<List<object>>("params");

//TODO

return null;
}
);
}
}

字典类型

public class DictionaryType : ObjectGraphType<Dictionary<string,string>>
{
public DictionaryType()
{
Name = "DictionaryType";
Description = "Dictionary of type string, string.";
}
}

HtStaticAlarmBaseType

public class HtStaticAlarmBaseType : ObjectGraphType<HtStaticAlarmBase>
{
public HtStaticAlarmBaseType()
{
Name = "HtStaticAlarmBase";
Description = "Base class of a static alarm.";


// ##################################################
// HtAlarmBase
// ##################################################

#region HtAlarmBase

Field<StringGraphType>(
"AlarmClass",
resolve: context => context.Source.AlarmClass.ToString());

Field<StringGraphType>(
"AlarmGroup",
resolve: context => context.Source.AlarmGroup);

Field<IntGraphType>(
"ErrorCode",
resolve: context => (int)context.Source.ErrorCode);

Field<StringGraphType>(
"Id",
resolve: context => context.Source.Id.ToString());

Field<StringGraphType>(
"Message",
resolve: context => context.Source.Message);

Field<StringGraphType>(
"Station",
resolve: context => context.Source.Station);

Field<StringGraphType>(
"TimeStampCome",
resolve: context => context.Source.TimeStampCome?.ToString());

Field<StringGraphType>(
"TimeStampGone",
resolve: context => context.Source.TimeStampGone?.ToString());

Field<StringGraphType>(
"TimeStampAcknowledge",
resolve: context => context.Source.TimeStampAcknowledge?.ToString());

Field<StringGraphType>(
"Origin",
resolve: context => context.Source.Origin);

#endregion

Field<IntGraphType>(
"Number",
resolve: context => context.Source.Number);

Field<BooleanGraphType>(
"Active",
resolve: context => context.Source.Active);
}
}

最佳答案

实际上这是我目前工作的解决方案:

查询客户端

mutation RegisterStaticAlarms($params: [HtStaticAlarmInputType])
{
registerStaticAlarms(params: $params)
{
id,
number,
message,
errorCode
}
}

突变

public class Mutation : ObjectGraphType
{
public Mutation()
{
Name = "Mutation";

Field<ListGraphType<HtStaticAlarmType>>(
"registerStaticAlarms",
arguments: new QueryArguments(
new QueryArgument<ListGraphType<HtStaticAlarmInputType>>
{
Name = "params"
}
),
resolve: context =>
{
List<HtStaticAlarmInputTypeParams> paramses = context.GetArgument<List<HtStaticAlarmInputTypeParams>>("params");

List<HtStaticAlarmBase> list = new List<HtStaticAlarmBase>();
foreach (HtStaticAlarmInputTypeParams p in paramses)
{
list.Add(HtAlarmManager.Create(p.Origin, (EHtAlarmClassType)Enum.Parse(typeof(EHtAlarmClassType), p.AlarmClass.ToString()), p.AlarmGroup, p.Station, (HtErrorCode)Enum.Parse(typeof(HtErrorCode), p.ErrorCode.ToString()), p.Message, p.Number));
}

return list;
}
);
}
}

类型和型号

/// <summary>
/// GraphQl type of the <see cref="HtStaticAlarmBase"/>
/// </summary>
internal class HtStaticAlarmType : ObjectGraphType<HtStaticAlarmBase>
{
public HtStaticAlarmType()
{
Name = "HtStaticAlarmType";
Description = "Base class of a static alarm.";


// ##################################################
// HtAlarmBase
// ##################################################

#region HtAlarmBase

Field<StringGraphType>(
"AlarmClass",
resolve: context => context.Source.AlarmClass.ToString());

Field<StringGraphType>(
"AlarmGroup",
resolve: context => context.Source.AlarmGroup);

Field<IntGraphType>(
"ErrorCode",
resolve: context => (int)context.Source.ErrorCode);

Field<StringGraphType>(
"Id",
resolve: context => context.Source.Id.ToString());

Field<StringGraphType>(
"Message",
resolve: context => context.Source.Message);

Field<StringGraphType>(
"Station",
resolve: context => context.Source.Station);

Field<StringGraphType>(
"TimeStampCome",
resolve: context => context.Source.TimeStampCome?.ToString());

Field<StringGraphType>(
"TimeStampGone",
resolve: context => context.Source.TimeStampGone?.ToString());

Field<StringGraphType>(
"TimeStampAcknowledge",
resolve: context => context.Source.TimeStampAcknowledge?.ToString());

Field<StringGraphType>(
"Origin",
resolve: context => context.Source.Origin);

#endregion

Field<IntGraphType>(
"Number",
resolve: context => context.Source.Number);

Field<BooleanGraphType>(
"Active",
resolve: context => context.Source.Active);
}
}

/// <summary>
/// GraphQL input type of the <see cref="HtStaticAlarmBase"/>
/// </summary>
internal class HtStaticAlarmInputType : InputObjectGraphType
{
public HtStaticAlarmInputType()
{
Name = "HtStaticAlarmInputType";
Description = "Base class of a static alarm.";

// ##################################################
// HtAlarmBase
// ##################################################

#region HtAlarmBase

Field<IntGraphType>("AlarmClass");
Field<StringGraphType>("AlarmGroup");
Field<IntGraphType>("ErrorCode");
Field<StringGraphType>("Id");
Field<StringGraphType>("Message");
Field<StringGraphType>("Station");
Field<DateGraphType>("TimeStampCome");
Field<DateGraphType>("TimeStampGone");
Field<DateGraphType>("TimeStampAcknowledge");
Field<StringGraphType>("Origin");
Field<StringGraphType>("IsSynced");
Field<StringGraphType>("Pending");

#endregion

Field<IntGraphType>("Number");
Field<BooleanGraphType>("Active");
}
}

/// <summary>
/// A lightweight class to deserialize the incoming <see cref="HtStaticAlarmInputType"/>
/// </summary>
internal class HtStaticAlarmInputTypeParams
{
public int AlarmClass { get; set; }
public string AlarmGroup { get; set; }
public int ErrorCode { get; set; }
public string Message { get; set; }
public string Station { get; set; }
public DateTime TimeStampCome { get; set; }
public DateTime TimeStampGone { get; set; }
public DateTime TimeStampAcknowledge { get; set; }
public string Origin { get; set; }

public int Number { get; set; }
public bool Active { get; set; }
}

预览

Preview

关于c# - 带有 List<Dictionary<string, string>> | 的 GraphQL.NET 突变JSON字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48743214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com