Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
Created October 3, 2025 17:02
Show Gist options
  • Save rmosolgo/1931ed99ec1bd31156ed346f2f148357 to your computer and use it in GitHub Desktop.
Save rmosolgo/1931ed99ec1bd31156ed346f2f148357 to your computer and use it in GitHub Desktop.
require "bundler/inline"
gemfile do
gem "graphql", "2.5.13"
gem "graphql-enterprise", source: "https://gems.graphql.pro"
end
class MySchema < GraphQL::Schema
class ExampleChangeset < GraphQL::Enterprise::Changeset
release("2025-01-01")
end
class BaseField < GraphQL::Schema::Field
include GraphQL::Enterprise::Changeset::FieldIntegration
end
class Query < GraphQL::Schema::Object
field_class(BaseField)
field :int, Integer, fallback_value: 1, added_in: ExampleChangeset
end
query(Query)
use GraphQL::Enterprise::Changeset::Release, changesets: [ExampleChangeset]
module RequireMatchingVersion
# This is a static set of all released changeset versions.
# Depending on how your app is set up, you might not be able to
# assign it to a constant (eg if the changeset releases haven't been set up yet).
# In that case, you could move this to `def execute_query`
# And do a `.any?` check instead of creating a Set.
ALL_RELEASES = Set.new(MySchema.changesets.map(&:release))
def execute_query(query:)
requested_version = query.context[:changeset_version]
if requested_version && !ALL_RELEASES.include?(requested_version)
message = "Schema version #{requested_version.inspect} not found. Please use a published version number and try again."
query.context.response_extensions["warning"] = message
# Or raise:
# raise GraphQL::ExecutionError, message
end
yield
end
end
trace_with RequireMatchingVersion
end
pp MySchema.execute("{ int }", context: { changeset_version: "2025-01-01"} ).to_h
# {"data" => {"int" => 1}}
pp MySchema.execute("{ int }", context: { changeset_version: "2025-10-01"} ).to_h
# {
# "data" => {"int" => 1},
# "extensions" => {
# "warning" => "Schema version \"2025-10-01\" not found. Please use a published version number and try again."
# }
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment