我试图进入GraphGL,但在构建测试应用程序时遇到了我无法修复的Mutations问题(查询工作正常)。
POCO数据对象:
public class MockObject
{
[Key]
public int? ObjId { get; set; }
public string ObjName { get; set; }
public double ObjValue { get; set; }
}
模式:
public class TemplateProjectSchema : GraphQL.Types.Schema
{
public TemplateProjectSchema(IServiceProvider provider) : base(provider)
{
Query = provider.GetService(typeof(ProjectTemplateQuery)) as ProjectTemplateQuery
?? throw new ArgumentNullException(nameof(ProjectTemplateQuery));
Mutation = provider.GetService(typeof(ProjectTemplateMutation)) as ProjectTemplateMutation
?? throw new ArgumentNullException(nameof(ProjectTemplateMutation));
}
}
突变:
public class ProjectTemplateMutation : ObjectGraphType
{
public ProjectTemplateMutation(IMockObjectRepository mockObjectRepository)
{
Field<MockObjectType>("addMockObject",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<MockObjectInputType>> {Name = "mockObject"}),
resolve: context =>
{
var mockObj = context.GetArgument<MockObject>("mockObject");
return mockObjectRepository.UpsertMockObject(mockObj);
});
}
}
输入类型:
public class MockObjectInputType : InputObjectGraphType<MockObject>
{
public MockObjectInputType()
{
Name = "MockObjectInputType";
Field<IntGraphType>("objid");
Field<NonNullGraphType<StringGraphType>>("objname");
Field<FloatGraphType>("objvalue");
}
}
我一直在尝试几种方法来使其正常工作,但我想着一件事。 在GraphQL中,您可以查询定义的类型及其字段,如下所示:
__type(name: "MockObjectInputType") {
name
fields {
name
type {
name
kind
}
}
}
}
此查询与我定义的负责graphql查询的类型配合使用很好。如果我在InputType上运行此命令,则结果显示字段为null,这意味着我没有为InputType定义的字段。 输出:
{
"__type": {
"name": "MockObjectInputType",
"fields": null
}
}
我的InputType字段配置错误吗?