Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
Created January 4, 2022 14:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmosolgo/8db09f3372c5fb5a1aabb0614da0aa3b to your computer and use it in GitHub Desktop.
Save rmosolgo/8db09f3372c5fb5a1aabb0614da0aa3b to your computer and use it in GitHub Desktop.
Versioning a global ID field in GraphQL-Ruby
require "bundler/inline"
gemfile do
gem "graphql", "1.13.2"
gem "graphql-enterprise", "1.1.0"
end
class BaseField < GraphQL::Schema::Field
include GraphQL::Enterprise::Changeset::FieldIntegration
end
class Thing < GraphQL::Schema::Object
field_class BaseField
implements GraphQL::Types::Relay::Node
def id
"old-id"
end
def global_id
context.schema.id_from_object(object, self, context)
end
end
class Query < GraphQL::Schema::Object
field :thing, Thing
def thing
Object.new
end
end
class MigrateId < GraphQL::Enterprise::Changeset
release "2022-01-01"
modifies Thing do
field :id, ID, null: false, description: "ID of the object.", resolver_method: :global_id
end
end
class Schema < GraphQL::Schema
query(Query)
use GraphQL::Enterprise::Changeset::Release, changesets: [MigrateId]
def self.id_from_object(obj, type, ctx)
"new-id"
end
end
pp Schema.execute("{ thing { id } }", context: { changeset_version: nil }).to_h
# => {"data"=>{"thing"=>{"id"=>"old-id"}}}
pp Schema.execute("{ thing { id } }", context: { changeset_version: "2022-01-01" }).to_h
# => {"data"=>{"thing"=>{"id"=>"new-id"}}}
p [:same_definition?, Schema.to_definition == Schema.to_definition(context: { changeset_version: "2022-01-01"} )]
# => [:same_definition?, true]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment