forked from consuldemocracy/consuldemocracy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request consuldemocracy#1653 from consul/amiedes-api-dev-P…
…Rs-2 Graphql API
- Loading branch information
Showing
35 changed files
with
1,559 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class GraphqlController < ApplicationController | ||
|
||
skip_before_action :verify_authenticity_token | ||
skip_authorization_check | ||
|
||
class QueryStringError < StandardError; end | ||
|
||
def query | ||
begin | ||
if query_string.nil? then raise GraphqlController::QueryStringError end | ||
response = consul_schema.execute query_string, variables: query_variables | ||
render json: response, status: :ok | ||
rescue GraphqlController::QueryStringError | ||
render json: { message: 'Query string not present' }, status: :bad_request | ||
rescue JSON::ParserError | ||
render json: { message: 'Error parsing JSON' }, status: :bad_request | ||
rescue GraphQL::ParseError | ||
render json: { message: 'Query string is not valid JSON' }, status: :bad_request | ||
rescue | ||
unless Rails.env.production? then raise end | ||
end | ||
end | ||
|
||
private | ||
|
||
def consul_schema | ||
api_types = GraphQL::ApiTypesCreator.create(API_TYPE_DEFINITIONS) | ||
query_type = GraphQL::QueryTypeCreator.create(api_types) | ||
|
||
GraphQL::Schema.define do | ||
query query_type | ||
max_depth 8 | ||
max_complexity 2500 | ||
end | ||
end | ||
|
||
def query_string | ||
if request.headers["CONTENT_TYPE"] == 'application/graphql' | ||
request.body.string # request.body.class => StringIO | ||
else | ||
params[:query] | ||
end | ||
end | ||
|
||
def query_variables | ||
if params[:variables].blank? || params[:variables] == 'null' | ||
{} | ||
else | ||
JSON.parse(params[:variables]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Graphqlable | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
|
||
def graphql_field_name | ||
self.name.gsub('::', '_').underscore.to_sym | ||
end | ||
|
||
def graphql_field_description | ||
"Find one #{self.model_name.human} by ID" | ||
end | ||
|
||
def graphql_pluralized_field_name | ||
self.name.gsub('::', '_').underscore.pluralize.to_sym | ||
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 | ||
|
||
def public_created_at | ||
self.created_at.change(min: 0) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module HasPublicAuthor | ||
def public_author | ||
self.author.public_activity? ? self.author : nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
class Vote < ActsAsVotable::Vote | ||
end | ||
|
||
include Graphqlable | ||
|
||
scope :public_for_api, -> do | ||
where(%{(votes.votable_type = 'Debate' and votes.votable_id in (?)) or | ||
(votes.votable_type = 'Proposal' and votes.votable_id in (?)) or | ||
(votes.votable_type = 'Comment' and votes.votable_id in (?))}, | ||
Debate.public_for_api.pluck(:id), | ||
Proposal.public_for_api.pluck(:id), | ||
Comment.public_for_api.pluck(:id)) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
User: | ||
fields: | ||
id: integer | ||
username: string | ||
public_debates: [Debate] | ||
public_proposals: [Proposal] | ||
public_comments: [Comment] | ||
# organization: Organization | ||
Debate: | ||
fields: | ||
id: integer | ||
title: string | ||
description: string | ||
public_created_at: string | ||
cached_votes_total: integer | ||
cached_votes_up: integer | ||
cached_votes_down: integer | ||
comments_count: integer | ||
hot_score: integer | ||
confidence_score: integer | ||
comments: [Comment] | ||
public_author: User | ||
votes_for: [Vote] | ||
tags: ["ActsAsTaggableOn::Tag"] | ||
Proposal: | ||
fields: | ||
id: integer | ||
title: string | ||
description: string | ||
external_url: string | ||
cached_votes_up: integer | ||
comments_count: integer | ||
hot_score: integer | ||
confidence_score: integer | ||
public_created_at: string | ||
summary: string | ||
video_url: string | ||
geozone_id: integer | ||
retired_at: string | ||
retired_reason: string | ||
retired_explanation: string | ||
geozone: Geozone | ||
comments: [Comment] | ||
proposal_notifications: [ProposalNotification] | ||
public_author: User | ||
votes_for: [Vote] | ||
tags: ["ActsAsTaggableOn::Tag"] | ||
Comment: | ||
fields: | ||
id: integer | ||
commentable_id: integer | ||
commentable_type: string | ||
body: string | ||
public_created_at: string | ||
cached_votes_total: integer | ||
cached_votes_up: integer | ||
cached_votes_down: integer | ||
ancestry: string | ||
confidence_score: integer | ||
public_author: User | ||
votes_for: [Vote] | ||
Geozone: | ||
fields: | ||
id: integer | ||
name: string | ||
ProposalNotification: | ||
fields: | ||
title: string | ||
body: string | ||
proposal_id: integer | ||
public_created_at: string | ||
proposal: Proposal | ||
ActsAsTaggableOn::Tag: | ||
fields: | ||
id: integer | ||
name: string | ||
taggings_count: integer | ||
kind: string | ||
Vote: | ||
fields: | ||
votable_id: integer | ||
votable_type: string | ||
public_created_at: string | ||
vote_flag: boolean | ||
# Organization: | ||
# fields: | ||
# id: integer | ||
# user_id: integer | ||
# name: string |
Oops, something went wrong.