如何在GraphQL for .NET中更改自定义对象的列表 [英] How to mutate a list of custom objects in GraphQL for .NET

查看:46
本文介绍了如何在GraphQL for .NET中更改自定义对象的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用用于.NET的GraphQL ,我想将Foo的集合替换为一个新收藏.

Using GraphQL for .NET, I would like to replace the collection of Foo with a new collection.

给出以下服务器端代码:

Given this server-side code:

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的最新版本在撰写本文时.

I'm using the latest version of GraphQL for .NET at the time of writing.

我想念什么?

工作示例: 如何在GraphQL中更改自定义对象列表.NET

推荐答案

我从吉特(Gitter)的答案.

My answer from Gitter.

为结果创建一个 ObjectGraphType .请注意,从resolve返回的对象的形状"与图形类型的形状"匹配.

Make an ObjectGraphType for the result. Notice that the "shape" of the object that is returned from resolve matches the "shape" of the graph type.

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
        }
    }
}

这篇关于如何在GraphQL for .NET中更改自定义对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆