diff options
author | Nick Thomas <nick@gitlab.com> | 2017-08-16 16:04:41 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-06-05 21:47:42 +0300 |
commit | 9c6c17cbcdb8bf8185fc1b873dcfd08f723e4df5 (patch) | |
tree | 624dba30e87ed0ea39afa0535d92c37c7718daef /spec/support | |
parent | 67dc43db2f30095cce7fe01d7f475d084be936e8 (diff) |
Add a minimal GraphQL API
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/helpers/graphql_helpers.rb | 29 | ||||
-rw-r--r-- | spec/support/matchers/graphql_matchers.rb | 31 |
2 files changed, 60 insertions, 0 deletions
diff --git a/spec/support/helpers/graphql_helpers.rb b/spec/support/helpers/graphql_helpers.rb new file mode 100644 index 00000000000..5bb2cf9dd9e --- /dev/null +++ b/spec/support/helpers/graphql_helpers.rb @@ -0,0 +1,29 @@ +module GraphqlHelpers + # Run a loader's named resolver + def resolve(kls, name, obj: nil, args: {}, ctx: {}) + kls[name].call(obj, args, ctx) + end + + # Runs a block inside a GraphQL::Batch wrapper + def batch(max_queries: nil, &blk) + wrapper = proc do + GraphQL::Batch.batch do + result = yield + + if result.is_a?(Array) + Promise.all(result) + else + result + end + end + end + + if max_queries + result = nil + expect { result = wrapper.call }.not_to exceed_query_limit(max_queries) + result + else + wrapper.call + end + end +end diff --git a/spec/support/matchers/graphql_matchers.rb b/spec/support/matchers/graphql_matchers.rb new file mode 100644 index 00000000000..c0ed16ecaba --- /dev/null +++ b/spec/support/matchers/graphql_matchers.rb @@ -0,0 +1,31 @@ +RSpec::Matchers.define :require_graphql_authorizations do |*expected| + match do |field| + authorizations = field.metadata[:authorize] + + expect(authorizations).to contain_exactly(*expected) + end +end + +RSpec::Matchers.define :have_graphql_fields do |*expected| + match do |kls| + expect(kls.fields.keys).to contain_exactly(*expected.map(&:to_s)) + end +end + +RSpec::Matchers.define :have_graphql_arguments do |*expected| + match do |field| + expect(field.arguments.keys).to contain_exactly(*expected.map(&:to_s)) + end +end + +RSpec::Matchers.define :have_graphql_type do |expected| + match do |field| + expect(field.type).to eq(expected) + end +end + +RSpec::Matchers.define :have_graphql_resolver do |expected| + match do |field| + expect(field.resolve_proc).to eq(expected) + end +end |