Group methods related to text generation for GraphQL documentation into a module

This commit is contained in:
Alberto Miedes Garcés
2017-01-26 10:47:12 +01:00
parent e7f55b10e2
commit 83267330a7
10 changed files with 51 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
class Comment < ActiveRecord::Base
include Flaggable
include HasPublicAuthor
include Graphqlable
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases

View File

@@ -0,0 +1,32 @@
module Graphqlable
extend ActiveSupport::Concern
class_methods do
def graphql_field_name
self.name.gsub('::', '_').underscore.to_sym
end
def graphql_pluralized_field_name
self.name.gsub('::', '_').underscore.pluralize.to_sym
end
def graphql_field_description
"Find one #{self.model_name.human} by ID"
end
def graphql_pluralized_field_description
"Find all #{self.model_name.human.pluralize}"
end
def graphql_type_name
self.name.gsub('::', '_')
end
def graphql_type_description
"#{self.model_name.human}"
end
end
end

View File

@@ -8,6 +8,7 @@ class Debate < ActiveRecord::Base
include Searchable
include Filterable
include HasPublicAuthor
include Graphqlable
acts_as_votable
acts_as_paranoid column: :hidden_at

View File

@@ -1,4 +1,7 @@
class Geozone < ActiveRecord::Base
include Graphqlable
has_many :proposals
has_many :spending_proposals
has_many :debates

View File

@@ -7,6 +7,7 @@ class Proposal < ActiveRecord::Base
include Searchable
include Filterable
include HasPublicAuthor
include Graphqlable
acts_as_votable
acts_as_paranoid column: :hidden_at

View File

@@ -1,4 +1,7 @@
class ProposalNotification < ActiveRecord::Base
include Graphqlable
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
belongs_to :proposal

View File

@@ -9,6 +9,8 @@ class User < ActiveRecord::Base
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
include Graphqlable
has_one :administrator
has_one :moderator
has_one :valuator

View File

@@ -1,5 +1,7 @@
class Vote < ActsAsVotable::Vote
include Graphqlable
def self.public_for_api
joins("FULL OUTER JOIN debates ON votable_type = 'Debate' AND votable_id = debates.id").
joins("FULL OUTER JOIN proposals ON votable_type = 'Proposal' AND votable_id = proposals.id").

View File

@@ -39,8 +39,8 @@ module GraphQL
created_type = GraphQL::ObjectType.define do
name(model.name)
description("#{model.model_name.human}")
name model.graphql_type_name
description model.graphql_type_description
# Make a field for each column, association or method
fields.each do |field_name, field_type|

View File

@@ -18,16 +18,16 @@ module GraphQL
query_type_creator.created_api_types.each do |model, created_type|
if created_type.fields['id']
field model.name.underscore.to_sym do
field model.graphql_field_name do
type created_type
description "Find one #{model.model_name.human} by ID"
description model.graphql_field_description
argument :id, !types.ID
resolve -> (object, arguments, context) { model.public_for_api.find_by(id: arguments['id'])}
end
end
connection model.name.underscore.pluralize.to_sym, created_type.connection_type do
description "Find all #{model.model_name.human.pluralize}"
connection model.graphql_pluralized_field_name, created_type.connection_type do
description model.graphql_pluralized_field_description
resolve -> (object, arguments, context) { model.public_for_api }
end