如何更改 GraphQL for .NET 中的自定义对象列表

2024-04-01

Using .NET 的 GraphQL https://github.com/graphql-dotnet/graphql-dotnet,我想用新的集合替换 Foo 的集合。

给出这个服务器端代码:

public class Foo
{
    public Foo(string name)
    {
        Name = name;
    }

    public string Name { get; set; }
}

public class Root
{
    public Foo[] Foos { get; private set; }

    public Foo[] UpdateFoos(Foo[] foos)
    {
        Foos = foos;
        return Foos;
    }
}

public class MutationSchema : Schema
{
    public MutationSchema()
    {
        Query = new MutationQuery();
        Mutation = new MutationChange();
    }
}

public class FooType : ObjectGraphType
{
    public FooType()
    {
        Name = "IndividualFoo";
        Field<StringGraphType>("name");
    }
}

public class FoosType : ObjectGraphType<ListGraphType<FooType>>
{
    public FoosType()
    {
        Name = "ListOfFoo";
        Field<ListGraphType<FooType>>("foos");
    }
}

public class FoosInput : InputObjectGraphType
{
    public FoosInput()
    {
        Name = "InputForManyFoo";
        Field<ListGraphType<FooInput>>("foos");
        Field<ListGraphType<FooType>>("foosResult");
    }
}

public class FooInput : InputObjectGraphType
{
    public FooInput()
    {
        Name = "InputForSingleFoo";
        Field<StringGraphType>("name");
    }
}

public class MutationQuery : ObjectGraphType
{
    public MutationQuery()
    {
        Name = "Query";
        Field<FoosType>("queryAllFoos");
    }
}

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

        Field<FoosInput>(
            "updateAllFoos",
            arguments: new QueryArguments(
                new QueryArgument<FoosInput>
                {
                    Name = "updateFoosQueryArgument"
                }
            ),
            resolve: context =>
            {
                var root = context.Source as Root;
                var change = context.GetArgument<Foo[]>("updateFoosQueryArgument");
                // TODO: update collection e.g. return root.UpdateFoos(change);
                return change;
            }
        );
    }
}

当我运行突变查询时:

mutation M {
    fooCollection: updateAllFoos(updateFoosQueryArgument: {
        foos: [
            {name: "First Foo"},
            {name: "Second Foo"}
        ]}) {
        foosResult
    }
}

然后我收到以下错误:

{GraphQL.Validation.ValidationError: Cannot query field "foosResult" on type "InputForManyFoo". Did you mean "foosResult"?}

我正在使用适用于 .NET 的 GraphQL 最新版本 https://github.com/graphql-dotnet/graphql-dotnet/commit/b77e508fb92158e0432570cab3b576d30551e855在撰写本文时。

我缺少什么?


工作示例: 如何更改 GraphQL for .NET 中的自定义对象列表 https://gist.github.com/DanielRobinsonSoftware/9ef4aa2834f2c912b5a3d4ef63ba6188


我的回答来自 Gitter。

Make an ObjectGraphType为了结果。请注意,从解析返回的对象的“形状”与图形类型的“形状”相匹配。

public class FoosResultType : ObjectGraphType
{
    public FoosResultType()
    {
        Field<ListGraphType<FooType>>("foosResult");
    }
}

public class FoosResult
{
    public IEnumerable<Foo> FoosResult { get;set; }
}

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

        Field<FoosResultType>(
            "updateAllFoos",
            arguments: new QueryArguments(
                new QueryArgument<ListGraphType<FooInput>>
                {
                    Name = "updateFoosQueryArgument"
                }
            ),
            resolve: context =>
            {
                var root = context.Source as Root;
                var change = context.GetArgument<List<Foo>>("updateFoosQueryArgument");
                // TODO: update collection e.g. return root.UpdateFoos(change);
                return new FoosResult { FoosResult = change };
            }
        );
    }
}

并更新了突变:

mutation M {
    fooCollection: updateAllFoos(updateFoosQueryArgument: [
          {name: "First Foo"},
          {name: "Second Foo"}
        ]) {
        foosResult {
          name
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何更改 GraphQL for .NET 中的自定义对象列表 的相关文章

随机推荐