Search code examples
ruby-on-railsgraphql-ruby

Unable to resolve interface type in GraphQL schema in test only


I've created an interface type, like:

module UserErrorType
  include Types::BaseInterface

  definition_methods do
    def resolve_type(object, _ctx)
      ...
    end
  end
end

And a query that looks like:

query {
  thing {
    errors {
      code
      message
      ... on SomeDerivedType {
        blah
      }
    }
  }
}

When I run this locally everything works fine, I'm able to resolve my derived types and get into the resolve_type method.

When I run in rspec:

describe UserErrorType, type: :graphql_object do
  subject { execute(query, context) }
end

I get the following error:

GraphQL::RequiredImplementationMissingError: schema contains Interfaces or Unions, so you must define a resolve_type -> (obj, ctx) { ... } function

From my understanding of the error message it wants me to put the resolve_type method directly on the Schema object though I should be able to define it in the definition_methods directly in the interface as above.


Solution

  • I'm not sure if this is standard graphql-ruby practice or a relic of our implementation, but we have a test support file which will redefine our schema (this could be due to us being built inside of a Rails engine and stitching our schemas together). In this support file, we had something like:

    @schema = OurEngine::Schema.define do
      query(query_type)
    end
    

    Adding the unused resolve_type here gets around this problem:

    @schema = OurEngine::Schema.define do
      query(query_type)
    
      resolve_type -> (_type, _obj, _ctx) do
        raise NoMethodError, 'Need to implement `resolve_type`'
      end
    end
    

    This implementation is not used (it uses the interface-specific resolve_type method) but satisfies the requirements of the validation check.