2017-08-16 13:04:41 +00:00
|
|
|
module GraphqlHelpers
|
2018-07-10 14:19:45 +00:00
|
|
|
MutationDefinition = Struct.new(:query, :variables)
|
|
|
|
|
2018-05-23 07:55:14 +00:00
|
|
|
# makes an underscored string look like a fieldname
|
|
|
|
# "merge_request" => "mergeRequest"
|
|
|
|
def self.fieldnamerize(underscored_field_name)
|
|
|
|
graphql_field_name = underscored_field_name.to_s.camelize
|
|
|
|
graphql_field_name[0] = graphql_field_name[0].downcase
|
|
|
|
|
|
|
|
graphql_field_name
|
|
|
|
end
|
|
|
|
|
2017-08-16 13:04:41 +00:00
|
|
|
# Run a loader's named resolver
|
2018-05-23 07:55:14 +00:00
|
|
|
def resolve(resolver_class, obj: nil, args: {}, ctx: {})
|
|
|
|
resolver_class.new(object: obj, context: ctx).resolve(args)
|
2017-08-16 13:04:41 +00:00
|
|
|
end
|
|
|
|
|
2018-02-23 15:36:40 +00:00
|
|
|
# Runs a block inside a BatchLoader::Executor wrapper
|
2017-08-16 13:04:41 +00:00
|
|
|
def batch(max_queries: nil, &blk)
|
|
|
|
wrapper = proc do
|
2019-03-13 13:42:43 +00:00
|
|
|
BatchLoader::Executor.ensure_current
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
BatchLoader::Executor.clear_current
|
2017-08-16 13:04:41 +00:00
|
|
|
end
|
2018-05-21 07:52:24 +00:00
|
|
|
|
2017-08-16 13:04:41 +00:00
|
|
|
if max_queries
|
|
|
|
result = nil
|
|
|
|
expect { result = wrapper.call }.not_to exceed_query_limit(max_queries)
|
|
|
|
result
|
|
|
|
else
|
|
|
|
wrapper.call
|
|
|
|
end
|
|
|
|
end
|
2018-05-21 07:52:24 +00:00
|
|
|
|
2018-05-23 07:55:14 +00:00
|
|
|
def graphql_query_for(name, attributes = {}, fields = nil)
|
2018-06-14 13:06:53 +00:00
|
|
|
<<~QUERY
|
|
|
|
{
|
|
|
|
#{query_graphql_field(name, attributes, fields)}
|
|
|
|
}
|
|
|
|
QUERY
|
|
|
|
end
|
|
|
|
|
2018-07-10 14:19:45 +00:00
|
|
|
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
|
|
|
|
|
2018-06-14 13:06:53 +00:00
|
|
|
def query_graphql_field(name, attributes = {}, fields = nil)
|
2018-05-23 07:55:14 +00:00
|
|
|
fields ||= all_graphql_fields_for(name.classify)
|
|
|
|
attributes = attributes_to_graphql(attributes)
|
2019-01-24 16:23:57 +00:00
|
|
|
attributes = "(#{attributes})" if attributes.present?
|
2018-05-23 07:55:14 +00:00
|
|
|
<<~QUERY
|
2019-03-03 12:46:42 +00:00
|
|
|
#{name}#{attributes}
|
|
|
|
#{wrap_fields(fields)}
|
2018-05-23 07:55:14 +00:00
|
|
|
QUERY
|
|
|
|
end
|
|
|
|
|
2019-03-03 12:46:42 +00:00
|
|
|
def wrap_fields(fields)
|
2019-04-08 19:02:22 +00:00
|
|
|
fields = Array.wrap(fields).join("\n")
|
|
|
|
return unless fields.present?
|
2019-03-03 12:46:42 +00:00
|
|
|
|
|
|
|
<<~FIELDS
|
|
|
|
{
|
|
|
|
#{fields}
|
|
|
|
}
|
|
|
|
FIELDS
|
|
|
|
end
|
|
|
|
|
2019-01-30 19:00:30 +00:00
|
|
|
def all_graphql_fields_for(class_name, parent_types = Set.new)
|
2019-03-27 20:02:25 +00:00
|
|
|
allow_unlimited_graphql_complexity
|
|
|
|
|
2018-05-23 07:55:14 +00:00
|
|
|
type = GitlabSchema.types[class_name.to_s]
|
2018-05-21 07:52:24 +00:00
|
|
|
return "" unless type
|
|
|
|
|
|
|
|
type.fields.map do |name, field|
|
2018-06-14 13:06:53 +00:00
|
|
|
# We can't guess arguments, so skip fields that require them
|
2018-06-26 16:31:05 +00:00
|
|
|
next if required_arguments?(field)
|
2018-06-14 13:06:53 +00:00
|
|
|
|
2019-01-30 19:00:30 +00:00
|
|
|
singular_field_type = field_type(field)
|
|
|
|
|
|
|
|
# If field type is the same as parent type, then we're hitting into
|
|
|
|
# mutual dependency. Break it from infinite recursion
|
|
|
|
next if parent_types.include?(singular_field_type)
|
|
|
|
|
2018-06-26 16:31:05 +00:00
|
|
|
if nested_fields?(field)
|
2019-01-30 19:00:30 +00:00
|
|
|
fields =
|
|
|
|
all_graphql_fields_for(singular_field_type, parent_types | [type])
|
|
|
|
|
|
|
|
"#{name} { #{fields} }"
|
2018-06-26 16:31:05 +00:00
|
|
|
else
|
|
|
|
name
|
2018-05-21 07:52:24 +00:00
|
|
|
end
|
2018-06-14 13:06:53 +00:00
|
|
|
end.compact.join("\n")
|
2018-05-21 07:52:24 +00:00
|
|
|
end
|
|
|
|
|
2018-05-23 07:55:14 +00:00
|
|
|
def attributes_to_graphql(attributes)
|
|
|
|
attributes.map do |name, value|
|
|
|
|
"#{GraphqlHelpers.fieldnamerize(name.to_s)}: \"#{value}\""
|
|
|
|
end.join(", ")
|
|
|
|
end
|
|
|
|
|
2019-03-03 12:46:42 +00:00
|
|
|
def post_graphql(query, current_user: nil, variables: nil, headers: {})
|
|
|
|
post api('/', current_user, version: 'graphql'), params: { query: query, variables: variables }, headers: headers
|
2018-07-10 14:19:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def post_graphql_mutation(mutation, current_user: nil)
|
|
|
|
post_graphql(mutation.query, current_user: current_user, variables: mutation.variables)
|
2018-05-23 07:55:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def graphql_data
|
|
|
|
json_response['data']
|
|
|
|
end
|
|
|
|
|
|
|
|
def graphql_errors
|
2018-07-10 14:19:45 +00:00
|
|
|
json_response['errors']
|
|
|
|
end
|
|
|
|
|
|
|
|
def graphql_mutation_response(mutation_name)
|
|
|
|
graphql_data[GraphqlHelpers.fieldnamerize(mutation_name)]
|
2018-05-21 07:52:24 +00:00
|
|
|
end
|
|
|
|
|
2018-06-26 16:31:05 +00:00
|
|
|
def nested_fields?(field)
|
|
|
|
!scalar?(field) && !enum?(field)
|
|
|
|
end
|
|
|
|
|
2018-05-21 07:52:24 +00:00
|
|
|
def scalar?(field)
|
|
|
|
field_type(field).kind.scalar?
|
|
|
|
end
|
|
|
|
|
2018-06-26 16:31:05 +00:00
|
|
|
def enum?(field)
|
|
|
|
field_type(field).kind.enum?
|
|
|
|
end
|
|
|
|
|
|
|
|
def required_arguments?(field)
|
|
|
|
field.arguments.values.any? { |argument| argument.type.non_null? }
|
|
|
|
end
|
|
|
|
|
2018-05-21 07:52:24 +00:00
|
|
|
def field_type(field)
|
2018-07-10 14:19:45 +00:00
|
|
|
field_type = field.type
|
|
|
|
|
|
|
|
# The type could be nested. For example `[GraphQL::STRING_TYPE]`:
|
|
|
|
# - List
|
|
|
|
# - String!
|
|
|
|
# - String
|
2019-01-16 12:09:29 +00:00
|
|
|
field_type = field_type.of_type while field_type.respond_to?(:of_type)
|
2018-07-10 14:19:45 +00:00
|
|
|
|
|
|
|
field_type
|
2018-05-21 07:52:24 +00:00
|
|
|
end
|
2019-03-27 20:02:25 +00:00
|
|
|
|
|
|
|
# for most tests, we want to allow unlimited complexity
|
|
|
|
def allow_unlimited_graphql_complexity
|
|
|
|
allow_any_instance_of(GitlabSchema).to receive(:max_complexity).and_return nil
|
|
|
|
allow(GitlabSchema).to receive(:max_query_complexity).with(any_args).and_return nil
|
|
|
|
end
|
2017-08-16 13:04:41 +00:00
|
|
|
end
|