仅在测试中无法解析 GraphQL 模式中的接口类型

Unable to resolve interface type in GraphQL schema in test only

我创建了一个接口类型,例如:

module UserErrorType
  include Types::BaseInterface

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

查询如下:

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

当我 运行 在本地一切正常时,我能够解析我的派生类型并进入 resolve_type 方法。

当我 运行 在 rspec:

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

我收到以下错误:

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

根据我对错误消息的理解,它希望我将 resolve_type 方法直接放在 Schema 对象上,尽管我应该能够直接在 definition_methods 中定义它界面如上

我不确定这是标准 graphql-ruby 实践还是我们实施的遗留物,但我们有一个测试支持文件将重新定义我们的模式(这可能是因为我们是在Rails 引擎并将我们的模式拼接在一起)。在这个支持文件中,我们有类似的东西:

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

在此处添加未使用的 resolve_type 可以解决此问题:

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

  resolve_type -> (_type, _obj, _ctx) do
    raise NoMethodError, 'Need to implement `resolve_type`'
  end
end

这个实现没有被使用(它使用特定于接口的resolve_type方法)但是满足验证检查的要求。