Commit bfda56c1 authored by charlie ablett's avatar charlie ablett

Merge branch 'allow-to-pass-a-block-to-graphql_mutation-helper' into 'master'

Allow to pass a block to graphql mutation helper

See merge request gitlab-org/gitlab!32557
parents a01d7c48 d8291378
...@@ -15,16 +15,16 @@ describe 'Setting the status of an alert' do ...@@ -15,16 +15,16 @@ describe 'Setting the status of an alert' do
project_path: project.full_path, project_path: project.full_path,
iid: alert.iid.to_s iid: alert.iid.to_s
} }
graphql_mutation(:update_alert_status, variables.merge(input), graphql_mutation(:update_alert_status, variables.merge(input)) do
<<~QL <<~QL
clientMutationId clientMutationId
errors errors
alert { alert {
iid iid
status status
} }
QL QL
) end
end end
let(:mutation_response) { graphql_mutation_response(:update_alert_status) } let(:mutation_response) { graphql_mutation_response(:update_alert_status) }
......
...@@ -77,10 +77,14 @@ module GraphqlHelpers ...@@ -77,10 +77,14 @@ module GraphqlHelpers
QUERY QUERY
end end
def graphql_mutation(name, input, fields = nil) def graphql_mutation(name, input, fields = nil, &block)
raise ArgumentError, 'Please pass either `fields` parameter or a block to `#graphql_mutation`, but not both.' if fields.present? && block_given?
mutation_name = GraphqlHelpers.fieldnamerize(name) mutation_name = GraphqlHelpers.fieldnamerize(name)
input_variable_name = "$#{input_variable_name_for_mutation(name)}" input_variable_name = "$#{input_variable_name_for_mutation(name)}"
mutation_field = GitlabSchema.mutation.fields[mutation_name] mutation_field = GitlabSchema.mutation.fields[mutation_name]
fields = yield if block_given?
fields ||= all_graphql_fields_for(mutation_field.type.to_graphql) fields ||= all_graphql_fields_for(mutation_field.type.to_graphql)
query = <<~MUTATION query = <<~MUTATION
......
# frozen_string_literal: true
require 'spec_helper'
describe GraphqlHelpers do
include GraphqlHelpers
describe '.graphql_mutation' do
shared_examples 'correct mutation definition' do
it 'returns correct mutation definition' do
query = <<~MUTATION
mutation($updateAlertStatusInput: UpdateAlertStatusInput!) {
updateAlertStatus(input: $updateAlertStatusInput) {
clientMutationId
}
}
MUTATION
variables = %q({"updateAlertStatusInput":{"projectPath":"test/project"}})
is_expected.to eq(GraphqlHelpers::MutationDefinition.new(query, variables))
end
end
context 'when fields argument is passed' do
subject do
graphql_mutation(:update_alert_status, { project_path: 'test/project' }, 'clientMutationId')
end
it_behaves_like 'correct mutation definition'
end
context 'when block is passed' do
subject do
graphql_mutation(:update_alert_status, { project_path: 'test/project' }) do
'clientMutationId'
end
end
it_behaves_like 'correct mutation definition'
end
context 'when both fields and a block are passed' do
subject do
graphql_mutation(:mutation_name, { variable_name: 'variable/value' }, 'fieldName') do
'fieldName'
end
end
it 'raises an ArgumentError' do
expect { subject }.to raise_error(
ArgumentError,
'Please pass either `fields` parameter or a block to `#graphql_mutation`, but not both.'
)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment