[英]How do I create a Ruby GraphQL InputObject instance from a Hash?
I have a GraphQL::Schema::InputObject
subclass that I pass around inside my Rails server's GraphQL mutation handler. 我有一个
GraphQL::Schema::InputObject
子类,该子类在Rails服务器的GraphQL变异处理程序中传递。 I've separated out some of the parts of the code as separate units to test and would like to create an instance of this object in test. 我已经将代码的某些部分分离为要测试的单元,并希望在测试中创建此对象的实例。 How can I do this?
我怎样才能做到这一点?
My subclass looks like this: 我的子类如下所示:
class Foo < GraphQL::Schema::InputObject
argument :bar, Int, required: true
end
I peeked inside the graphql-ruby tests and saw there's a constructor that take a Hash , so I tried: 我偷看了graphql-ruby测试, 发现有一个采用Hash的构造函数 ,所以我尝试了:
Foo.new({bar: 42}, context: OpenStruct.new(), defaults_used: Set.new())
but I get back: 但我回来了:
NoMethodError (undefined method `new' for nil:NilClass)
I'm using graphql-ruby 1.9.9. 我正在使用graphql-ruby 1.9.9。
Reading the tests more closely revealed that before you can call the constructor, you must first call to_graphql
(documented in Extending the GraphQL-Ruby Type Definition System ). 仔细阅读测试后发现,在调用构造函数之前,必须首先调用
to_graphql
(在扩展GraphQL-Ruby类型定义系统中进行了介绍 )。 This method appears to bake out some schema/metaprogramming data. 该方法似乎可以烘焙一些模式/元编程数据。 Adding this line before the
new
call makes it work as I expect: 在
new
呼叫之前添加此行使其按预期工作:
Foo.to_graphql
However, the constructor doesn't appear to run any of the type coercions for the arguments: values are returned from the accessor methods verbatim. 但是,构造函数似乎没有对参数运行任何类型强制:值从访问方法中逐字返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.