khamusa / rspec-graphql_matchers

Collection of rspec matchers to test your graphQL api schema.
MIT License
145 stars 49 forks source link

Problem with `.have_field` and `.of_type` #22

Closed fibrasek closed 5 years ago

fibrasek commented 5 years ago

Hello guys,

The problem I'm facing is related to this small example:

require "rails_helper"

types = GraphQL::Define::TypeDefiner.instance

describe Types::Data::Permission do
  graphql_spec!

  # Attributes
  context 'attribute fields' do
    it { is_expected.to have_field(:id).of_type(types.ID) }
  end
end

When I run this teste, I have the following failure:

F

Failures:

  1) Types::Data::Permission attribute fields should define field `id`, of type `ID`
     Failure/Error: it { is_expected.to have_field(:id).of_type(types.ID) }
       expected Types::Data::Permission to define field `id`, of type `ID`, but the type was `#<GraphQL::Schema::NonNull:0x0000563160c7e718>`.
     # ./spec/graphql/types/data/permission_spec.rb:10:in `block (3 levels) in <top (required)>'
     # ./spec/rails_helper.rb:83:in `block (3 levels) in <top (required)>'
     # /bundle/gems/database_cleaner-1.7.0/lib/database_cleaner/generic/base.rb:16:in `cleaning'
     # /bundle/gems/database_cleaner-1.7.0/lib/database_cleaner/base.rb:100:in `cleaning'
     # /bundle/gems/database_cleaner-1.7.0/lib/database_cleaner/configuration.rb:86:in `block (2 levels) in cleaning'
     # /bundle/gems/database_cleaner-1.7.0/lib/database_cleaner/configuration.rb:87:in `cleaning'
     # ./spec/rails_helper.rb:83:in `block (2 levels) in <top (required)>'
     # /bundle/gems/rspec-retry-0.6.1/lib/rspec/retry.rb:123:in `block in run'
     # /bundle/gems/rspec-retry-0.6.1/lib/rspec/retry.rb:110:in `loop'
     # /bundle/gems/rspec-retry-0.6.1/lib/rspec/retry.rb:110:in `run'
     # /bundle/gems/rspec-retry-0.6.1/lib/rspec_ext/rspec_ext.rb:12:in `run_with_retry'
     # /bundle/gems/rspec-retry-0.6.1/lib/rspec/retry.rb:37:in `block (2 levels) in setup'

Finished in 11 minutes 29 seconds (files took 2.18 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/graphql/types/data/permission_spec.rb:10 # Types::Data::Permission attribute fields should define field `id`, of type `ID`

So I started digging up our code, rspec-graphql_matchers and graphql-ruby source to find any clues to where that object were being passed and not the actual type.

Digging I found out that .of_type actually gets the type using #type on the type object or class (in our case, we're passing the class).

But #type is returning a GraphQL::Schema::NonNull, not the actual type :thinking:

So I ran to the graphql-source to look in that, and it really returns that instance.

And now I'm stuck, and I doing something wrong by defining the subject as the class (in our case, Types::Data::Permission), or is it a bug?

Notes:

We are using the class definitions as suggested by the graphql-ruby gem.

The graphql_spec! method definition is as follow:

  def graphql_spec!
    self.let(:subject) do
      self.described_class
    end
  end
ericraio commented 5 years ago

I am having this issue also

ericraio commented 5 years ago

If it's helpful to anyone, i've been hacking together my own matchers on my fork here. https://github.com/ericraio/rspec-graphql_matchers/

I am also adding a lot more matchers

khamusa commented 5 years ago

These issues have been addressed in the latest releases. Please open a new issue if any of those problems still persists.