Last Updated: May 08, 2019
·
3.302K
· yourivdlans

Compare Graphql schemas for breaking changes

Want to make sure your Graphql schema doesn't have any breaking changes?

Install this gem: https://github.com/xuorig/graphql-schema_comparator

Add a test file which compares a schema snapshot present in version control with a snapshot of the current schema (not in version control).

require 'test_helper'

class MySchemaTest < ActiveSupport::TestCase
  test 'schema should not have changed' do
    schema = GraphQL::Schema::Printer.print_schema(MySchema)
    current_schema_path = Rails.root.join('tmp', 'current_my_schema.graphql')
    File.write(current_schema_path, schema)

    snapshot_schema_path = file_fixture('my_schema.graphql')
    snapshot = snapshot_schema_path.read

    result = GraphQL::SchemaComparator.compare(snapshot, schema)

    assert_not result.breaking?, "Graphql schema has breaking changes:\n" \
                                 "please run `bin/schema_comparator compare #{snapshot_schema_path} #{current_schema_path}` to see the changes"
    assert result.identical?, "Graphql schema has changed:\n" \
                              'please run `bin/rails graphql:schema:dump` to update the snapshot'
  end
end

Add the following to the bottom of your Rakefile

require 'graphql/rake_task'
GraphQL::RakeTask.new(
  schema_name: 'MySchema',
  idl_outfile: 'test/fixtures/files/my_schema.graphql',
  json_outfile: 'test/fixtures/files/my_schema.json'
)