我并不是唯一一个不知道如何在GraphQL:https://github.com/rmosolgo/graphql-ruby-demo/issues/27中使用https://github.com/rmosolgo/graphql-ruby-demo/issues/27类型的人,正如您所看到的,有18个人像我一样。
我如何在这样的字段中使用日期时间?
Types::PlayerType = GraphQL::ObjectType.define do
name 'Player'
field :id, !types.ID
field :birth_date, types.Datetime #or what?
field :death_date, !types.Datetime #or what?
field :note, types.String
end也许我必须使用这个(type.rb):
date_time_type.rb
Types::DateTimeType = GraphQL::ScalarType.define do
name 'DateTime'
coerce_input ->(value, _ctx) { Time.zone.parse(value) }
coerce_result ->(value, _ctx) { value.utc.iso8601 }
end有人能解释得更好吗?
发布于 2018-07-22 16:14:45
GraphQL::Types::ISO8601DateTime和GraphQL::Types::ISO8601Date最近被添加到graphql库(拉请求)中。
用法示例:
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :starts_on, GraphQL::Types::ISO8601Date, null: false发布于 2018-02-01 13:27:21
类型声明应该正常工作。请记住,您粘贴的DateTimeType位于模块"Types“中。像这样使用它:
Types::PlayerType = GraphQL::ObjectType.define do
name 'Player'
field :id, !types.ID
field :birth_date, Types::DateTimeType
field :death_date, Types::DateTimeType
field :note, types.String
end发布于 2018-02-08 01:58:37
根据源代码,只有5种类型(Int、字符串、浮点数、布尔值、ID)被定义为方法,这意味着您不能使用types.DateTime。
要自定义(date_time_type.rb),,您可以引用它的gem的文档,并按照前面提到的关于gem的文档来实现,然后您可以这样使用它:
field :birth_date, Types::DateTimeType
https://stackoverflow.com/questions/47960194
复制相似问题