Graphql-ruby, Date or Datetime type

I am not the only one who does not know how to use types Datetimewith GraphQL-Ruby: https://github.com/rmosolgo/graphql-ruby-demo/issues/27 , as you can see, there are 18 people like me.

How can I use datetime for some such fields?

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

Maybe I need to use this ( https://github.com/howtographql/graphql-ruby/blob/master/app/graphql/types/date_time_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

Can anyone explain this better?

+4
source share
3 answers

A type declaration should work correctly. Keep in mind that the DateTimeType you inserted is located in the Types module. Use it like this:

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
+3

, 5 (Int, String, Float, Boolean, ID), , , types.DateTime.

DateTimeType, , (date_time_type.rb), :

field :birth_date, Types::DateTimeType

+1

Not sure if I have a more detailed explanation, but I got it for work by pasting the code above to define my own scalar DateTimeType:

DateTimeType = GraphQL::ScalarType.define do
  name 'DateTime'

  coerce_input ->(value, _ctx) { Time.zone.parse(value) }
  coerce_result ->(value, _ctx) { value.utc.iso8601 }
end

MyType = ::GraphQL::ObjectType.define do
  name 'MyType'
  field :when, type: !DateTimeType
end
0
source

Source: https://habr.com/ru/post/1691303/


All Articles