简体   繁体   English

为什么graphql-ruby的布尔类型在null值上不返回false

[英]why does graphql-ruby's boolean type not return false on null value

In the graphql-ruby repo the Boolean type looks like this: 在graphql-ruby存储库中,布尔类型如下所示:

GraphQL::BOOLEAN_TYPE = GraphQL::ScalarType.define do
  name "Boolean"
  description "Represents `true` or `false` values."

  coerce_input ->(value, _ctx) { (value == true || value == false) ? value : nil }
  coerce_result ->(value, _ctx) { !!value }
  default_scalar true
end

But when i query a boolean field which returns nil from database i get null. 但是,当我查询从数据库返回nil的布尔字段时,我得到null。

笔记查询

In my note type i have directly coupled a graphql field with my boolean attribute in the database. 在我的笔记类型中,我直接将graphql字段与数据库中的boolean属性耦合。

field :is_public, types.Boolean

Can someone please explain this and maybe come up with a solution that does not involve changing all the fields? 有人可以解释一下,也许想出一种不涉及更改所有字段的解决方案?

The default GraphQL::BOOLEAN_TYPE returns nil whenever a non boolean value is given as seen here: https://github.com/rmosolgo/graphql-ruby/blob/7f7e97cc2902046d485adcb6e8e6a5eb462b3faf/spec/graphql/boolean_type_spec.rb 每当给出非布尔值时,默认的GraphQL::BOOLEAN_TYPE返回nil,如下所示: https : //github.com/rmosolgo/graphql-ruby/blob/7f7e97cc2902046d485adcb6e8e6a5eb462b3faf/spec/graphql/boolean_type_spec.rb

So what might be happening here is that types.Boolean is still mapped to the old GraphQL::BOOLEAN_TYPE and not using your new override. 所以这里可能发生的是, types.Boolean仍映射到旧的GraphQL::BOOLEAN_TYPE而不使用新的覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM