diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-07-10 17:19:45 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-07-25 19:37:12 +0300 |
commit | 3bcb04f100f5e982379fbeae37a30a191581d1ef (patch) | |
tree | e01065b8a6728bcc75af16166baafcbddd1a6cf5 /spec/support | |
parent | 9fe58f5a23f2960f666c4d641b463c744138d29c (diff) |
Add mutation toggling WIP state of merge requests
This is mainly the setup of mutations for GraphQL. Including
authorization and basic return type-structure.
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/helpers/graphql_helpers.rb | 61 | ||||
-rw-r--r-- | spec/support/matchers/graphql_matchers.rb | 9 | ||||
-rw-r--r-- | spec/support/shared_examples/requests/graphql_shared_examples.rb | 2 |
3 files changed, 63 insertions, 9 deletions
diff --git a/spec/support/helpers/graphql_helpers.rb b/spec/support/helpers/graphql_helpers.rb index b9322975b5a..75827df80dc 100644 --- a/spec/support/helpers/graphql_helpers.rb +++ b/spec/support/helpers/graphql_helpers.rb @@ -1,4 +1,6 @@ module GraphqlHelpers + MutationDefinition = Struct.new(:query, :variables) + # makes an underscored string look like a fieldname # "merge_request" => "mergeRequest" def self.fieldnamerize(underscored_field_name) @@ -41,6 +43,37 @@ module GraphqlHelpers QUERY end + def graphql_mutation(name, input, fields = nil) + mutation_name = GraphqlHelpers.fieldnamerize(name) + input_variable_name = "$#{input_variable_name_for_mutation(name)}" + mutation_field = GitlabSchema.mutation.fields[mutation_name] + fields ||= all_graphql_fields_for(mutation_field.type) + + query = <<~MUTATION + mutation(#{input_variable_name}: #{mutation_field.arguments['input'].type}) { + #{mutation_name}(input: #{input_variable_name}) { + #{fields} + } + } + MUTATION + variables = variables_for_mutation(name, input) + + MutationDefinition.new(query, variables) + end + + def variables_for_mutation(name, input) + graphql_input = input.map { |name, value| [GraphqlHelpers.fieldnamerize(name), value] }.to_h + { input_variable_name_for_mutation(name) => graphql_input }.to_json + end + + def input_variable_name_for_mutation(mutation_name) + mutation_name = GraphqlHelpers.fieldnamerize(mutation_name) + mutation_field = GitlabSchema.mutation.fields[mutation_name] + input_type = field_type(mutation_field.arguments['input']) + + GraphqlHelpers.fieldnamerize(input_type) + end + def query_graphql_field(name, attributes = {}, fields = nil) fields ||= all_graphql_fields_for(name.classify) attributes = attributes_to_graphql(attributes) @@ -73,8 +106,12 @@ module GraphqlHelpers end.join(", ") end - def post_graphql(query, current_user: nil) - post api('/', current_user, version: 'graphql'), query: query + def post_graphql(query, current_user: nil, variables: nil) + post api('/', current_user, version: 'graphql'), query: query, variables: variables + end + + def post_graphql_mutation(mutation, current_user: nil) + post_graphql(mutation.query, current_user: current_user, variables: mutation.variables) end def graphql_data @@ -82,7 +119,11 @@ module GraphqlHelpers end def graphql_errors - json_response['data'] + json_response['errors'] + end + + def graphql_mutation_response(mutation_name) + graphql_data[GraphqlHelpers.fieldnamerize(mutation_name)] end def nested_fields?(field) @@ -102,10 +143,14 @@ module GraphqlHelpers end def field_type(field) - if field.type.respond_to?(:of_type) - field.type.of_type - else - field.type - end + field_type = field.type + + # The type could be nested. For example `[GraphQL::STRING_TYPE]`: + # - List + # - String! + # - String + field_type = field_type.of_type while field_type.respond_to?(:of_type) + + field_type end end diff --git a/spec/support/matchers/graphql_matchers.rb b/spec/support/matchers/graphql_matchers.rb index be6fa4c71a0..7be84838e00 100644 --- a/spec/support/matchers/graphql_matchers.rb +++ b/spec/support/matchers/graphql_matchers.rb @@ -34,6 +34,15 @@ RSpec::Matchers.define :have_graphql_field do |field_name| end end +RSpec::Matchers.define :have_graphql_mutation do |mutation_class| + match do |mutation_type| + field = mutation_type.fields[GraphqlHelpers.fieldnamerize(mutation_class.graphql_name)] + + expect(field).to be_present + expect(field.resolver).to eq(mutation_class) + end +end + RSpec::Matchers.define :have_graphql_arguments do |*expected| include GraphqlHelpers diff --git a/spec/support/shared_examples/requests/graphql_shared_examples.rb b/spec/support/shared_examples/requests/graphql_shared_examples.rb index fe7b7bc306f..04140cad3f0 100644 --- a/spec/support/shared_examples/requests/graphql_shared_examples.rb +++ b/spec/support/shared_examples/requests/graphql_shared_examples.rb @@ -5,7 +5,7 @@ shared_examples 'a working graphql query' do it 'returns a successful response', :aggregate_failures do expect(response).to have_gitlab_http_status(:success) - expect(graphql_errors['errors']).to be_nil + expect(graphql_errors).to be_nil expect(json_response.keys).to include('data') end end |