From 0f14b628c4e9624c42e783c0e6620e8b00073ab6 Mon Sep 17 00:00:00 2001 From: Brett Walker Date: Mon, 20 May 2019 16:19:00 -0500 Subject: Use BatchModelLoader for parent in GroupType --- app/graphql/types/group_type.rb | 6 +++-- spec/graphql/resolvers/group_resolver_spec.rb | 32 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 spec/graphql/resolvers/group_resolver_spec.rb diff --git a/app/graphql/types/group_type.rb b/app/graphql/types/group_type.rb index a2d615ee732..530aecc2bf9 100644 --- a/app/graphql/types/group_type.rb +++ b/app/graphql/types/group_type.rb @@ -8,14 +8,16 @@ module Types expose_permissions Types::PermissionTypes::Group - field :web_url, GraphQL::STRING_TYPE, null: true + field :web_url, GraphQL::STRING_TYPE, null: false field :avatar_url, GraphQL::STRING_TYPE, null: true, resolve: -> (group, args, ctx) do group.avatar_url(only_path: false) end if ::Group.supports_nested_objects? - field :parent, GroupType, null: true + field :parent, GroupType, + null: true, + resolve: -> (obj, _args, _ctx) { Gitlab::Graphql::Loaders::BatchModelLoader.new(Group, obj.parent_id).find } end end end diff --git a/spec/graphql/resolvers/group_resolver_spec.rb b/spec/graphql/resolvers/group_resolver_spec.rb new file mode 100644 index 00000000000..5eb9cd06d15 --- /dev/null +++ b/spec/graphql/resolvers/group_resolver_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Resolvers::GroupResolver do + include GraphqlHelpers + + set(:group1) { create(:group) } + set(:group2) { create(:group) } + + describe '#resolve' do + it 'batch-resolves groups by full path' do + paths = [group1.full_path, group2.full_path] + + result = batch(max_queries: 1) do + paths.map { |path| resolve_group(path) } + end + + expect(result).to contain_exactly(group1, group2) + end + + it 'resolves an unknown full_path to nil' do + result = batch { resolve_group('unknown/project') } + + expect(result).to be_nil + end + end + + def resolve_group(full_path) + resolve(described_class, args: { full_path: full_path }) + end +end -- cgit v1.2.1