rubygraphql-ruby

How to test a GraphQL schema with graphql-ruby?


My goal is to test the types of my GraphQL schema in ruby, I'm using the graphql-ruby gem.

I couldn't find any best practice for this, so I'm wondering what's the best way to test the fields and types of a Schema.

The gem recommends against testing the Schema directly http://graphql-ruby.org/schema/testing.html but I still find valuable to be able to know when the schema changes unexpectedly.

Having a type like this:

module Types
  class DeskType < GraphQL::Schema::Object
    field :id, ID, 'Id of this Desk', null: false
    field :location, String, 'Location of the Desk', null: false
    field :custom_id, String, 'Human-readable unique identifier for this desk', null: false
  end
end

My first approach has been to use the fields hash in the GraphQL::Schema::Object type, for example:

Types::DeskType.fields['location'].type.to_s => 'String!'

Creating an RSpec matcher, I could come up with tests that look like this:

RSpec.describe Types::DeskType do
  it 'has the expected schema fields' do
    fields = {
      'id': 'ID!',
      'location': 'String!',
      'customId': 'String!'
    }

    expect(described_class).to match_schema_fields(fields)
  end
end

This approach has some drawbacks though:


Solution

  • It looks you want to test your schema because you want to know if it is going to break the client. Basically you should avoid this.

    Instead you can use gems like: graphql-schema_comparator to print breaking changes.

    1. I suggest to have a rake task for dumping your schema (and commit it in your repo).
    2. You can write some spec to check if the schema was dump - then you will make sure, you have always up-to date schema dump.
    3. Setup your CI to compare schema of current branch with schema on master branch.
    4. Fail your build if schema has dangerous or breaking changes.
    5. You can even generate Schema Changelog using schema-comparator ;) Or you can even use slack notifications to send any schema changes there so your team could easilly track any changes.