diff options
Diffstat (limited to 'spec/support/helpers/graphql_helpers.rb')
-rw-r--r-- | spec/support/helpers/graphql_helpers.rb | 61 |
1 files changed, 53 insertions, 8 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 |