Skip to content

Commit

Permalink
refactor: remove crud in favor of gql controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ribeirojose committed Oct 8, 2024
1 parent 87af4af commit f7233c7
Show file tree
Hide file tree
Showing 66 changed files with 8,170 additions and 4,072 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"makefile.configureOnOpen": false,
"typescript.tsdk": "node_modules/typescript/lib"
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def initialize(id)
@data = {}
@nonce = nil
@started_at = nil
@completed_at = nil
end

def start(action_id, action_type, user_id, start_data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ def start_execution(data)

{
state: "started",
step: 0,
stepCount: 1,
nonce: response["nonce"],
message: response["message"]
}
Expand Down
3 changes: 2 additions & 1 deletion apps/govquests-api/rails_app/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ gem "httparty", "~> 0.22.0"
gem "jwt"
gem "memery"
gem "oj"
gem "blueprinter"

gem "graphql", "~> 2.3"
7 changes: 5 additions & 2 deletions apps/govquests-api/rails_app/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ GEM
bcrypt_pbkdf (1.1.1-x86_64-darwin)
benchmark (0.3.0)
bigdecimal (3.1.8)
blueprinter (1.1.2)
bootsnap (1.18.4)
msgpack (~> 1.2)
brakeman (6.2.1)
Expand Down Expand Up @@ -179,11 +178,15 @@ GEM
railties (>= 5.0.0)
faker (3.4.2)
i18n (>= 1.8.11, < 2)
fiber-storage (1.0.0)
fugit (1.11.1)
et-orbi (~> 1, >= 1.2.11)
raabro (~> 1.4)
globalid (1.2.1)
activesupport (>= 6.1)
graphql (2.3.18)
base64
fiber-storage
httparty (0.22.0)
csv
mini_mime (>= 1.0.0)
Expand Down Expand Up @@ -433,13 +436,13 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
blueprinter
bootsnap
brakeman
debug
factory_bot (~> 6.5)
factory_bot_rails (~> 6.4)
faker (~> 3.4)
graphql (~> 2.3)
httparty (~> 0.22.0)
infra!
jwt
Expand Down

This file was deleted.

This file was deleted.

17 changes: 0 additions & 17 deletions apps/govquests-api/rails_app/app/blueprints/quest_blueprint.rb

This file was deleted.

This file was deleted.

51 changes: 51 additions & 0 deletions apps/govquests-api/rails_app/app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

class GraphqlController < ApplicationController
# If accessing from outside this domain, nullify the session
# This allows for outside API access while preventing CSRF attacks,
# but you'll have to authenticate your user separately
# protect_from_forgery with: :null_session

def execute
variables = prepare_variables(params[:variables])
query = params[:query]
operation_name = params[:operationName]
context = {
current_user: current_user
}
result = GovquestsApiSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
render json: result
rescue => e
raise e unless Rails.env.development?
handle_error_in_development(e)
end

private

# Handle variables in form data, JSON body, or a blank value
def prepare_variables(variables_param)
case variables_param
when String
if variables_param.present?
JSON.parse(variables_param) || {}
else
{}
end
when Hash
variables_param
when ActionController::Parameters
variables_param.to_unsafe_hash # GraphQL-Ruby will validate name and type of incoming variables.
when nil
{}
else
raise ArgumentError, "Unexpected parameter: #{variables_param}"
end
end

def handle_error_in_development(e)
logger.error e.message
logger.error e.backtrace.join("\n")

render json: {errors: [{message: e.message, backtrace: e.backtrace}], data: {}}, status: 500
end
end

This file was deleted.

15 changes: 0 additions & 15 deletions apps/govquests-api/rails_app/app/controllers/quests_controller.rb

This file was deleted.

45 changes: 45 additions & 0 deletions apps/govquests-api/rails_app/app/graphql/govquests_api_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

class GovquestsApiSchema < GraphQL::Schema
mutation(Types::MutationType)
query(Types::QueryType)

# For batch-loading (see https://graphql-ruby.org/dataloader/overview.html)
use GraphQL::Dataloader

# GraphQL-Ruby calls this when something goes wrong while running a query:
def self.type_error(err, context)
# if err.is_a?(GraphQL::InvalidNullError)
# # report to your bug tracker here
# return nil
# end
super
end

# Union and Interface Resolution
def self.resolve_type(abstract_type, obj, ctx)
# TODO: Implement this method
# to return the correct GraphQL object type for `obj`
raise(GraphQL::RequiredImplementationMissingError)
end

# Limit the size of incoming queries:
max_query_string_tokens(5000)

# Stop validating when it encounters this many errors:
validate_max_errors(100)

# Relay-style Object Identification:

# Return a string UUID for `object`
def self.id_from_object(object, type_definition, query_ctx)
# For example, use Rails' GlobalID library (https://github.com/rails/globalid):
object.to_gid_param
end

# Given a string UUID, find the object
def self.object_from_id(global_id, query_ctx)
# For example, use Rails' GlobalID library (https://github.com/rails/globalid):
GlobalID.find(global_id)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Mutations
class BaseMutation < GraphQL::Schema::RelayClassicMutation
argument_class Types::BaseArgument
field_class Types::BaseField
input_object_class Types::BaseInputObject
object_class Types::BaseObject
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Mutations
class CompleteActionExecution < BaseMutation
argument :execution_id, ID, required: true
argument :nonce, String, required: true
argument :completion_data, GraphQL::Types::JSON, required: false

field :action_execution, Types::ActionExecutionType, null: true
field :errors, [String], null: false

def resolve(execution_id:, nonce:, completion_data: {})
result = ActionTracking::ActionExecutionService.complete(
execution_id: execution_id,
nonce: nonce,
user_id: context[:current_user]&.user_id,
completion_data: completion_data
)

if result.is_a?(ActionTracking::ActionExecutionReadModel)
{
action_execution: result,
errors: []
}
else
{
action_execution: nil,
errors: [result[:error]]
}
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# app/graphql/mutations/start_action_execution.rb
module Mutations
class StartActionExecution < BaseMutation
argument :action_id, ID, required: true
argument :start_data, GraphQL::Types::JSON, required: false

field :action_execution, Types::ActionExecutionType, null: true
field :errors, [String], null: false

def resolve(action_id:, start_data: {})
result = ActionTracking::ActionExecutionService.start(
action_id: action_id,
user_id: context[:current_user]&.user_id,
start_data: start_data
)

if result.is_a?(ActionTracking::ActionExecutionReadModel)
{
action_execution: result,
errors: []
}
else
{
action_execution: nil,
errors: [result[:error]]
}
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module Resolvers
class BaseResolver < GraphQL::Schema::Resolver
end
end
Loading

0 comments on commit f7233c7

Please sign in to comment.