我正在尝试从使用Rails REST API迁移到使用GraphQL。我们尝试替换的REST端点之一使用了序列化器。在该序列化程序中,我们将根据字符串是“ true”还是“ false”返回字符串或布尔值。我在创建可以处理此问题的graphql对象时遇到了麻烦。我尝试创建自定义类型和自定义标量无济于事。我正在尝试使用rspec来测试graphql查询,但是当我尝试使用自定义标量或自定义类型时,我总是收到运行时错误消息:
no implicit conversion of nil into String
无益的部分是我没有一种好的方法来调试导致此错误的原因。
- 是否有一种简单的方法来定义可以返回布尔值或字符串的graphQL字段?
- 有人对如何调试通过rspec收到的错误有任何建议吗?我曾尝试在不同的地方使用
byebug
,但是byebug
的声明从未受到打击。
这是我要转换为graphql的序列化程序的示例
class ModelToSerialize < activeModel::Serializer
attributes :id,:customization_key,:value
# value is stored as a string in the relational database
# customization_key is stored as an integer in the relational database
def value
if object.customization_key == 1
return true if object.value == true || object.value =~ /^(true|t|yes|y|1)$/i
return false
else
object.value
end
end
end
这是我尝试创建的GraphQL类型:
module Types
class ModelToSerializeType < Types::BaseObject
field :id,Int,null: false
field :value,String,null: false # this should return boolean OR string
field :customization_key,null: false,camelize: false
end
end
上面的方法不起作用,因为我将类型限制为String
,但是我希望它返回布尔值或字符串。即使我将序列化程序中的代码复制/粘贴到类型定义中,它仍然只会返回一个字符串