Graphql-ruby Use types to format data outside of graphql

82 Views Asked by At

I was wondering, is there a way to apply the types declared in our Graphql API to format data the same way graphql does it with the return value if a query.

There are other ways to do that, (like having an actual formatting service that would sit between the returned value of the query and the type itself) but I really would like to know how to format from the type directly (without making my own internal gql query).

I currently do it by executing an internal graphql query that is fired internally but that's not really what I'd like.

Edit: Add example of what I have in mind

class Types::UserType < Types::BaseObject
  field :id, ID, null: false
  field :custom_id, ID, null: false
  
  def custom_id
    object.id + context["id_suffix"]
  end
end

typed_object = Types::UserType.new(User.find(1), { "id_suffix" => "foo" }) # this is pseudo code, that's the part I'd like to know how to do
typed_object.custom_id #=> "1foo"
typed_object.to_h #=> { "id" => 1, "custom_id" => "1foo" }
0

There are 0 best solutions below