Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ gem 'rack-cors', require: 'rack/cors'
gem "active_model_serializers"
gem "cancancan", "~> 1.10"
gem "faker"
gem "graphql"
gem "graphiql-rails"

gem "rack-cors", require: "rack/cors"
group :production do
gem "rails_12factor"
gem "pg"
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ GEM
thor (~> 0.14)
globalid (0.3.7)
activesupport (>= 4.1.0)
graphiql-rails (1.3.0)
rails
graphql (0.18.11)
hashdiff (0.3.0)
i18n (0.7.0)
json (1.8.3)
Expand Down Expand Up @@ -192,6 +195,8 @@ DEPENDENCIES
faker
faraday
figaro
graphiql-rails
graphql
jwt
pg
pry-rails
Expand Down
13 changes: 13 additions & 0 deletions app/controllers/api/v2/graph_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Api
module V2
class GraphController < ApplicationController
def process_query
@current_user = User.last
query = params[:query]
vars = params[:variables] || {}
result = ProverbSchema.execute(query, variables: vars, context: {current_user: @current_user} )
render json: result
end
end
end
end
25 changes: 25 additions & 0 deletions app/graph/mutations/authenticate_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
AuthenticateUser = GraphQL::Relay::Mutation.define do
name "Authenticate user"
description "Authenticates the user"

input_field :access_token, !types.String
return_field :user, !UserType
return_field :token, !types.String

resolve -> (inputs, ctx){
params = {
fields: FIELDS,
access_token: inputs["access_token"]
}

http_client = Api::V1::Http.new(FB_URL)
response, status = http_client.get_request(params)

if status != "200"
raise GraphQL::ExecutionError, "Could not validate user"
end
user = User.find_or_create_user(response)
token = Authenticate.create_token(fb_id: user.fb_id, email: user.email)
{ token: token, user: user }
}
end
19 changes: 19 additions & 0 deletions app/graph/mutations/create_proverb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CreateProverb = GraphQL::Relay::Mutation.define do
name "Create Proverb"
description "Create proverbs"

input_field :language, !types.String
input_field :body, !types.String
input_field :root_id, types.ID
input_field :tags, types.String

return_field :proverb, ProverbType

resolve -> (inputs, context){
user = context[:current_user]
raise GraphQL::ExecutionError, "you need to be authorized to completed this action" unless user

tags = inputs["tags"] ? inputs.split(",") : []
{proverb: Proverb.create(body: inputs["body"], language: inputs["language"], root_id: inputs["root_id"], all_tags: tags, user: user)}
}
end
9 changes: 9 additions & 0 deletions app/graph/mutations/mutation_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MutationType = GraphQL::ObjectType.define do
name "Root mutation"
description "Entry point for resources changes"

field :create_proverb, field: CreateProverb.field
# field :delete_proverb, field: DeleteProverb.field
# field :update_proverb, field: UpdateProverb.field
field :authenticate_user, field: AuthenticateUser.field
end
9 changes: 9 additions & 0 deletions app/graph/proverb_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ProverbSchema = GraphQL::Schema.define do
query QueryType
mutation MutationType
max_depth 8
end

ProverbSchema.rescue_from(ActiveRecord::RecordInvalid) do |error|
error.record.errors.messages.to_json
end
29 changes: 29 additions & 0 deletions app/graph/types/proverb_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ProverbType = GraphQL::ObjectType.define do
name "Proverb"
description "A proverb"

field :id, types.ID, "Proverb Id"
field :body, types.String, "body of the proverb"
field :language, types.String, "language of the proverb"
field :status, types.String, "status of proverb"
field :created_at, types.String, "date proverb was created"
field :updated_at, types.String, "data proverb was last updated"
field :user do
type UserType
resolve -> (obj, args, ctx){
obj.user
}
end
field :translations do
type types[ProverbType]
resolve -> (obj, args, ctx){
obj.translations
}
end
field :tags do
type types[TagType]
resolve -> (obj, args, ctx){
obj.tags
}
end
end
32 changes: 32 additions & 0 deletions app/graph/types/query_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
directionEnum = GraphQL::EnumType.define do
name "Query Direction"
description "Query order by value"
value "desc", "order in descending order by id"
value "asc", "order in ascending order by id"
end

QueryType = GraphQL::ObjectType.define do
name "Query"
description "the query root for this schema"

field :proverb do
type ProverbType
argument :id, !types.ID, "the id of the proverb to retrieve"
resolve -> (obj, args, ctx){
Proverb.find_by(id: args["id"])
}
end

field :proverbs do
type types[ProverbType]
argument :limit, types.Int, default_value: 10
argument :random, types.Boolean, default_value: false
argument :direction, directionEnum, default_value: "desc"
argument :offset, types.Int, default_value: 0
argument :tags, types.String
argument :language, types.String
resolve -> (obj, args, ctx){
Proverb.search(args)
}
end
end
15 changes: 15 additions & 0 deletions app/graph/types/tag_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
TagType = GraphQL::ObjectType.define do
name "Tag"
description "A tag"

field :id, types.ID, "Tag Id"
field :name, types.String, "Name of the tag"
field :created_at, types.String, "Data tag was created"
field :updated_at, types.String, "Data tag was last updated"
field :proverbs do
type !types[!ProverbType]
resolve -> (obj, args, ctx){
obj.proverbs
}
end
end
28 changes: 28 additions & 0 deletions app/graph/types/user_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
UserTypeEnum = GraphQL::EnumType.define do
name "UserType"
description "User types"

value "regular", "default user type"
value "moderator", "can approved proverbs"
value "admin", "super user"
end

UserType = GraphQL::ObjectType.define do
name "User"
description "A user"

field :id, types.ID, "User Id"
field :email, types.String, "User email"
field :first_name, types.String, "User first name"
field :last_name, types.String, "User last name"
field :fb_id, types.String, "User Facebook id"
field :created_at, types.String, "Date user was created"
field :updated_at, types.String, "Date user information was last updated"
field :user_type, UserTypeEnum
field :proverbs do
type types[ProverbType]
resolve -> (obj, args, ctx){
obj.proverbs
}
end
end
15 changes: 15 additions & 0 deletions app/models/proverb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,19 @@ def all_tags=(proverb_tags)
def all_tags
self.tags.map(&:name).join(", ")
end

def self.search(params)
query = self.includes(:user)
if params["tags"]
query = query.joins(:tags).where(tags: {name: params["tags"].split(",")})
end

if params["language"]
query = query.where(language: params["language"].split(","))
end

ord = params["random"] ? "RANDOM()" : "id #{params["direction"]}"

query.limit(params["limit"]).order(ord).offset("#{params["offset"]}")
end
end
8 changes: 2 additions & 6 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ class Application < Rails::Application
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :put, :delete, :options]
end
end
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
Expand All @@ -43,5 +37,7 @@ class Application < Rails::Application
end
end
config.active_record.raise_in_transactional_callbacks = true
config.autoload_paths << Rails.root.join("app", "graph", "types")
config.autoload_paths << Rails.root.join("app", "graph", "mutations")
end
end
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
post "/auth/login", to: "auth#login"
get "/auth/logout", to: "auth#logout"
end

namespace :v2 do
post "/queries", to:"graph#process_query"
end
end
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/api/v2/queries"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"files":{"graphiql/rails/application-03241fa7c1b4556ec1a6022e2ee5bb71f81cbfe9ebdc2323e47ab52dd8e8f7dc.js":{"logical_path":"graphiql/rails/application.js","mtime":"2016-09-13T00:40:10+01:00","size":1909184,"digest":"03241fa7c1b4556ec1a6022e2ee5bb71f81cbfe9ebdc2323e47ab52dd8e8f7dc","integrity":"sha256-AyQfp8G0VW7BpgIuLuW7cfgcv+nr3CMj5Hq1Ldjo99w="},"graphiql/rails/application-ed6a1ab506a370f641807d15150cd24f57e418521ed1f3f726534d2e26b24afc.css":{"logical_path":"graphiql/rails/application.css","mtime":"2016-09-13T00:40:10+01:00","size":28954,"digest":"ed6a1ab506a370f641807d15150cd24f57e418521ed1f3f726534d2e26b24afc","integrity":"sha256-7WoatQajcPZBgH0VFQzST1fkGFIe0fP3JlNNLiaySvw="}},"assets":{"graphiql/rails/application.js":"graphiql/rails/application-03241fa7c1b4556ec1a6022e2ee5bb71f81cbfe9ebdc2323e47ab52dd8e8f7dc.js","graphiql/rails/application.css":"graphiql/rails/application-ed6a1ab506a370f641807d15150cd24f57e418521ed1f3f726534d2e26b24afc.css"}}
Loading