Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #17 from thauma9/master
Browse files Browse the repository at this point in the history
ADD categories retrieval
  • Loading branch information
a-barbieri authored Dec 5, 2018
2 parents 5b26767 + 7d7af86 commit b8bd033
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 8 deletions.
9 changes: 9 additions & 0 deletions app/graphql/binda/api/types/category_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Binda::Api::Types::CategoryType = GraphQL::ObjectType.define do |context|
name 'Binda_Category'
context.field :id, context.types.ID
context.field :name, context.types.String
context.field :slug, context.types.String
context.field :position, context.types.Int

::Binda::Api::Fields::SHARED_FIELDS.call(context)
end
2 changes: 1 addition & 1 deletion app/graphql/binda/api/types/component_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
context.field :publish_state, context.types.String
context.field :structure, ::Binda::Api::Types::StructureType
context.field :position, context.types.Int

context.field :categories, ::Binda::Api::Types::CategoryType
::Binda::Api::Fields::SHARED_FIELDS.call(context)
end
8 changes: 7 additions & 1 deletion app/graphql/binda/api/types/structure_type.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Binda::Api::Types::StructureType = GraphQL::ObjectType.define do
Binda::Api::Types::StructureType = GraphQL::ObjectType.define do |context|
name "Binda_Structure"

field :id, !types.ID
field :name, !types.String

context.field :get_categories, ::Binda::Api::Types::CategoryType.to_list_type do
resolve ->(obj, args, ctx) {
obj.categories.order("binda_categories.position")
}
end
end
5 changes: 5 additions & 0 deletions lib/binda/api/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ module Fields
obj.repeaters.order("binda_repeaters.position").select{ |t| t.field_setting_id == ::Binda::FieldSetting.get_id( args[:slug] ) }
}
end
context.field :get_categories, ::Binda::Api::Types::CategoryType.to_list_type do
resolve ->(obj, args, ctx) {
obj.categories.order("binda_categories.position")
}
end
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/factories/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
sequence(:category_name) { |n| "Test Category ##{n}" }

factory :category, class: Binda::Category do
name { generate :category_name }
association :structure
end
end
19 changes: 14 additions & 5 deletions spec/factories/structures.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
FactoryBot.define do

sequence(:article_structure_title) { |n| "N.#{n} Article" }
sequence(:structure_name) { |n| "##{n} structure" }

# Structure
factory :structure, class: Binda::Structure do
factory :structure, class: Binda::Structure do
name { generate :structure_name }
slug { "#{name}".parameterize }
slug { "#{name}".parameterize }
instance_type 'component'
end

Expand Down Expand Up @@ -41,7 +40,7 @@
components_count 3
end
after(:create) do |structure, evaluator|
# get default field group which is generated by default for each structure
# get default fiel sgroup which is generated by default for each structure
default_field_group = structure.field_groups.first
# Generate some field settings
create :string_setting, field_group: default_field_group
Expand All @@ -50,8 +49,18 @@
create :radio_setting_with_choices, field_group: default_field_group
create :selection_setting_with_choices, field_group: default_field_group
create :checkbox_setting_with_choices, field_group: default_field_group
create_list( :article_component, evaluator.components_count, structure: structure)
create_list(:article_component, evaluator.components_count, structure: structure)
end
end

factory :article_structure_with_components_and_fields_and_categories,
parent: :article_structure_with_components_and_fields do
transient do
categories_count 3
end

after(:create) do |structure, evaluator|
create_list(:category, evaluator.categories_count, structure: structure)
end
end
end
48 changes: 47 additions & 1 deletion spec/requests/graphql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe "GraphQL API" do

before(:context) do
@structure = create(:article_structure_with_components_and_fields)
@structure = create(:article_structure_with_components_and_fields_and_categories)
@structure.components.each do |component|
component.strings.each{|string| string.update(content: "Content of string #{string.field_setting.slug}") }
end
Expand Down Expand Up @@ -219,5 +219,51 @@
end
end

it 'can get categories from structures' do
data= '{
structures {
edges {
node {
categories: get_categories {
id
name
position
slug
}
}
}
}
}'

post graphql_path(query: data, api_key: @authorized_user.api_key)
edges = json['data']['structures']['edges']
expect(edges.first['node']['categories'].count).to be > 0
end

it 'gets component category' do
first_structure_category = @structure.categories.first
expect(@component.categories.length).to eq(0)
@component.categories << first_structure_category
@component.save!

data= '{
component: component_by_slug(slug: "'+@component.slug+'") {
categories: get_categories {
id
name
position
slug
}
}
}'

post graphql_path(query: data, api_key: @authorized_user.api_key)
categories = json['data']['component']['categories']
expect(categories.first['id'].to_i).to eq(first_structure_category['id'])
expect(categories.first['name']).to eq(first_structure_category['name'])
expect(categories.first['position']).to eq(first_structure_category['position'])
expect(categories.first['slug']).to eq(first_structure_category['slug'])
end

end

0 comments on commit b8bd033

Please sign in to comment.