graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

Converting custom scalars from a schema that is loaded over introspection

Open lukel97 opened this issue 4 years ago • 1 comments

I have a schema that is loaded over HTTP like this:

  HTTP = GraphQL::Client::HTTP.new('http://localhost:8080')
  Schema = GraphQL::Client.load_schema(HTTP)

But there is a scalar type in this schema, timestamptz, that I would like to convert to a Time object. Is it possible to attach a custom ScalarType to this, similar to the way that can be done if constructing the schema by hand?

  Timestamp = GraphQL::ScalarType.define do
    name "timestamptz"
    coerce_input ->(value) do
      Time.iso8601(value)
    end
    coerce_result ->(value) do
      value.tc.iso8601
    end
  end

lukel97 avatar Nov 30 '20 15:11 lukel97

I have the same problem and solve it by the following hack. Our GraphQL server has a scalar type Time that returns ISO8601 format date and time. Adding its type definition into built-in types before loading schema can deal with the Time as Ruby's Time.

  GraphQL::Schema::BUILT_IN_TYPES = GraphQL::Schema::BUILT_IN_TYPES.merge(
    'Time' => GraphQL::Types::ISO8601DateTime,
  )
  HTTP = GraphQL::Client::HTTP.new('http://localhost:8080')
  Schema = GraphQL::Client.load_schema(HTTP)

kamatama41 avatar May 13 '21 19:05 kamatama41