我的目标是在ruby中测试我的GraphQL模式的类型,我正在使用graphql-ruby gem。
我找不到任何最佳实践,所以我想知道什么是测试架构的字段和类型的最佳方法。
该gem建议不要直接测试该模式http://graphql-ruby.org/schema/testing.html,但是我仍然觉得知道模式何时意外更改很有价值。
具有这样的类型:
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
Run Code Online (Sandbox Code Playgroud)
我的第一种方法是fields在GraphQL :: Schema :: Object类型中使用哈希,例如:
Types::DeskType.fields['location'].type.to_s => 'String!'
Run Code Online (Sandbox Code Playgroud)
创建一个RSpec匹配器,我可以提出如下测试:
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
Run Code Online (Sandbox Code Playgroud)
但是,这种方法有一些缺点:
小智 4
看来您想测试您的架构,因为您想知道它是否会破坏客户端。基本上你应该避免这种情况。
相反,您可以使用 gems like:graphql-schema_comparator来打印重大更改。
| 归档时间: |
|
| 查看次数: |
1837 次 |
| 最近记录: |