forked from fs/task_tracker_itis_2023
-
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.
Prepare changes for lesson 12 (fs#130)
* prepare changes for lesson 12 * fix cops * fix cops 2 * add information for second lesson * add enum status
- Loading branch information
1 parent
ff6ec56
commit 4865c60
Showing
36 changed files
with
379 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,7 @@ jobs: | |
|
||
- name: Run rubocop | ||
run: bin/rubocop | ||
|
||
- name: Run tests | ||
run: bin/rspec | ||
... |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 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 | ||
|
||
skip_verify_authorized | ||
|
||
def execute | ||
variables = prepare_variables(params[:variables]) | ||
query = params[:query] | ||
operation_name = params[:operationName] | ||
context = { | ||
current_user: current_user | ||
} | ||
result = TaskTrackerItisSchema.execute(query, variables: variables, context: context, operation_name: operation_name) | ||
render json: result | ||
rescue StandardError => 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 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,11 @@ | ||
module GraphqlErrors | ||
def formatted_errors(model) | ||
model.errors.map do |error| | ||
path = ["attributes", error.attribute.to_s.camelize(:lower)] | ||
{ | ||
path: path, | ||
message: error.message | ||
} | ||
end | ||
end | ||
end |
Empty file.
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
class BaseMutation < GraphQL::Schema::Mutation | ||
include GraphqlErrors | ||
|
||
argument_class Types::BaseArgument | ||
field_class Types::BaseField | ||
object_class Types::BaseObject | ||
|
||
def current_user | ||
@context[:current_user] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Mutations | ||
class CreateProject < BaseMutation | ||
argument :input, Types::Inputs::CreateProjectInput, required: true | ||
|
||
type Types::Payloads::CreateProjectPayload | ||
|
||
def resolve(input:) | ||
result = Projects::Create.call(project_params: input.to_h, user: current_user) | ||
|
||
result.to_h.merge(errors: formatted_errors(result.project)) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Resolvers | ||
class Base < GraphQL::Schema::Resolver | ||
argument_class Types::BaseArgument | ||
|
||
def current_user | ||
@context[:current_user] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Resolvers | ||
class Project < Resolvers::Base | ||
argument :id, ID, required: true | ||
|
||
type Types::ProjectType, null: true | ||
|
||
def resolve(**options) | ||
::Project.find_by(id: options[:id]) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Resolvers | ||
class Projects < Resolvers::Base | ||
type [Types::ProjectType], null: false | ||
|
||
def resolve(**_options) | ||
::Project.all | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
class TaskTrackerItisSchema < 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 | ||
|
||
# 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 |
Empty file.
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,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
class BaseArgument < GraphQL::Schema::Argument | ||
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,8 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
class BaseConnection < Types::BaseObject | ||
# add `nodes` and `pageInfo` fields, as well as `edge_type(...)` and `node_nullable(...)` overrides | ||
include GraphQL::Types::Relay::ConnectionBehaviors | ||
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,8 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
class BaseEdge < Types::BaseObject | ||
# add `node` and `cursor` fields, as well as `node_type(...)` override | ||
include GraphQL::Types::Relay::EdgeBehaviors | ||
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,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
class BaseEnum < GraphQL::Schema::Enum | ||
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,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
class BaseField < GraphQL::Schema::Field | ||
argument_class Types::BaseArgument | ||
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,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
class BaseInputObject < GraphQL::Schema::InputObject | ||
argument_class Types::BaseArgument | ||
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
module BaseInterface | ||
include GraphQL::Schema::Interface | ||
edge_type_class(Types::BaseEdge) | ||
connection_type_class(Types::BaseConnection) | ||
|
||
field_class Types::BaseField | ||
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
class BaseObject < GraphQL::Schema::Object | ||
edge_type_class(Types::BaseEdge) | ||
connection_type_class(Types::BaseConnection) | ||
field_class Types::BaseField | ||
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,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
class BaseScalar < GraphQL::Schema::Scalar | ||
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,8 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
class BaseUnion < GraphQL::Schema::Union | ||
edge_type_class(Types::BaseEdge) | ||
connection_type_class(Types::BaseConnection) | ||
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,8 @@ | ||
module Types | ||
module Inputs | ||
class CreateProjectInput < Types::BaseInputObject | ||
argument :name, String, required: true | ||
argument :description, String, required: false | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
class MutationType < Types::BaseObject | ||
field :create_project, mutation: Mutations::CreateProject | ||
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
module NodeType | ||
include Types::BaseInterface | ||
# Add the `id` field | ||
include GraphQL::Types::Relay::NodeBehaviors | ||
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,8 @@ | ||
module Types | ||
module Payloads | ||
class CreateProjectPayload < Types::BaseObject | ||
field :project, ProjectType, null: true | ||
field :errors, [Types::UserError], null: true | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Types | ||
class ProjectType < Types::BaseObject | ||
field :id, ID, null: false | ||
field :name, String, null: false | ||
field :description, String, null: false | ||
field :created_at, GraphQL::Types::ISO8601DateTime, null: false | ||
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false | ||
field :tasks, [TaskType], null: false | ||
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,8 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
class QueryType < Types::BaseObject | ||
field :project, resolver: Resolvers::Project | ||
field :projects, resolver: Resolvers::Projects | ||
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,7 @@ | ||
module Types | ||
class TaskStatusType < Types::BaseEnum | ||
value "UNSTARTED", value: "unstarted", description: "Unstarted" | ||
value "STARTED", value: "started", description: "Started" | ||
value "FINISHED", value: "finished", description: "Finished" | ||
end | ||
end |
Oops, something went wrong.