2017-08-16 13:04:41 +00:00
|
|
|
module GraphqlHelpers
|
|
|
|
# Run a loader's named resolver
|
|
|
|
def resolve(kls, name, obj: nil, args: {}, ctx: {})
|
|
|
|
kls[name].call(obj, args, ctx)
|
|
|
|
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
|
2018-02-23 15:36:40 +00:00
|
|
|
begin
|
|
|
|
BatchLoader::Executor.ensure_current
|
2018-05-21 07:52:24 +00:00
|
|
|
yield
|
2018-02-23 15:36:40 +00:00
|
|
|
ensure
|
|
|
|
BatchLoader::Executor.clear_current
|
2017-08-16 13:04:41 +00:00
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
def all_graphql_fields_for(klass)
|
|
|
|
type = GitlabSchema.types[klass.name]
|
|
|
|
return "" unless type
|
|
|
|
|
|
|
|
type.fields.map do |name, field|
|
|
|
|
if scalar?(field)
|
|
|
|
name
|
|
|
|
else
|
|
|
|
"#{name} { #{all_graphql_fields_for(field_type(field))} }"
|
|
|
|
end
|
|
|
|
end.join("\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_graphql(query)
|
|
|
|
post '/api/graphql', query: query
|
|
|
|
end
|
|
|
|
|
|
|
|
def scalar?(field)
|
|
|
|
field_type(field).kind.scalar?
|
|
|
|
end
|
|
|
|
|
|
|
|
def field_type(field)
|
|
|
|
if field.type.respond_to?(:of_type)
|
|
|
|
field.type.of_type
|
|
|
|
else
|
|
|
|
field.type
|
|
|
|
end
|
|
|
|
end
|
2017-08-16 13:04:41 +00:00
|
|
|
end
|