草庐IT

go - 作为对象的变异参数

coder 2024-07-07 原文

我正在使用 Go implemenatation of GraphQL .

您将如何配置一个变异,以便它可以接收超过 1 级的参数? 例如,这是我想传递给突变 CreateUser 的参数列表:

mutation createUser($user: CreateUser!) {
  createUser(input: $user)
}

{
  "user": {
    "name": {
      "first": "John",
      "last": "Doe"
    },
    "email": "john@doe.com"
  }
}

(请注意,我不想使用 firstnamelastname 而是使用 name 对象)

这是我迄今为止(未成功)的尝试:

var CreateUserInput = graphql.FieldConfigArgument{
    "input": &graphql.ArgumentConfig{
        Description: "Input for creating a new user",
        Type: graphql.NewNonNull(graphql.NewInputObject(graphql.InputObjectConfig{
            Name: "CreateUser",
            Fields: graphql.InputObjectConfigFieldMap{
                "name": &graphql.InputObjectFieldConfig{
                    Type: graphql.NewNonNull(graphql.NewInputObject(graphql.InputObjectConfig{
                        Fields: graphql.InputObjectConfigFieldMap{
                            "first": &graphql.InputObjectFieldConfig{
                                Type: graphql.NewNonNull(graphql.String),
                            },
                            "last": &graphql.InputObjectFieldConfig{
                                Type: graphql.NewNonNull(graphql.String),
                            },
                        },
                    })),
                },
                "email": &graphql.InputObjectFieldConfig{
                    Type: graphql.NewNonNull(graphql.String),
                },
            },
        })),
    },
}

显然,子字段 firstlast 未被识别,因为这是我运行此突变时得到的结果:

{
  "data": null,
  "errors": [
    {
      "message": "Variable \"$user\" got invalid value {\"email\":\"john@doe.com\",\"name\":{\"first\":\"john\",\"last\":\"doe\"}}.\nIn field \"name\": In field \"first\": Unknown field.\nIn field \"name\": In field \"last\": Unknown field.",
      "locations": [
        {
          "line": 1,
          "column": 21
        }
      ]
    }
  ]
}

这可能吗?

编辑:请参阅解决方案已接受答案中的评论。

最佳答案

这是我的第一行 Go,但我会尝试传达我认为的问题所在。

首先让我们谈谈您想要的结构。我将在这里使用 SDL:

type Mutation {
  createUser(user: CreateUser!): Boolean! # Maybe return user type here?
}

input CreateUser {
  name: CreateUserName!
  email: String!
}

input CreateUserName {
  first: String!
  last: String!
}

现在我们知道我们需要两种输入类型,让我们开始吧!

var CreateUserName = graphql.NewInputObject(graphql.InputObjectConfig{
    Name: "CreateUserName",
    Fields: graphql.InputObjectConfigFieldMap{
        "first": &graphql.InputObjectFieldConfig{
            Type: graphql.NewNonNull(graphql.String),
        },
        "last": &graphql.InputObjectFieldConfig{
            Type: graphql.NewNonNull(graphql.String),
        },
    },
})

var CreateUser = graphql.NewInputObject(graphql.InputObjectConfig{
    Name: "CreateUser",
    Fields: graphql.InputObjectConfigFieldMap{
        "name": &graphql.InputObjectFieldConfig{
            Type: graphql.NewNonNull(CreateUserName),
        },
        "email": &graphql.InputObjectFieldConfig{
            Type: graphql.NewNonNull(graphql.String),
        },
    },
})

现在剩下的就是将 mutation 字段添加到您的 mutation 对象类型中。

关于go - 作为对象的变异参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52261045/

有关go - 作为对象的变异参数的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  3. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  4. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  5. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  6. Ruby 写入和读取对象到文件 - 2

    好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信

  7. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  8. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

  9. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  10. ruby-on-rails - 未在 Ruby 中初始化的对象 - 2

    我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调

随机推荐