add subjects and reviwers query
This commit is contained in:
17
app/graphql/resolvers/reviewers_query_resolver.rb
Normal file
17
app/graphql/resolvers/reviewers_query_resolver.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
# frozen_string_literal: true
|
||||
module Resolvers
|
||||
class ReviewersQueryResolver
|
||||
def initialize(context)
|
||||
@context = context
|
||||
end
|
||||
|
||||
def resolve
|
||||
UserPolicy::Scope.new(@context[:current_user], User)
|
||||
.resolve
|
||||
.joins(:roles)
|
||||
.where(roles: { name: %i[teacher nde] })
|
||||
.where.not(id: @context[:current_user].id)
|
||||
.distinct
|
||||
end
|
||||
end
|
||||
end
|
||||
12
app/graphql/resolvers/subjects_query_resolver.rb
Normal file
12
app/graphql/resolvers/subjects_query_resolver.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
module Resolvers
|
||||
class SubjectsQueryResolver
|
||||
def initialize(context)
|
||||
@context = context
|
||||
end
|
||||
|
||||
def resolve
|
||||
SubjectPolicy::Scope.new(@context[:current_user], Subject).resolve
|
||||
end
|
||||
end
|
||||
end
|
||||
14
app/graphql/sources/active_record.rb
Normal file
14
app/graphql/sources/active_record.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Sources
|
||||
class ActiveRecord < GraphQL::Dataloader::Source
|
||||
def initialize(model_class)
|
||||
@model_class = model_class
|
||||
end
|
||||
|
||||
def fetch(ids)
|
||||
records = @model_class.where(id: ids).index_by(&:id)
|
||||
records.slice(*ids).values
|
||||
end
|
||||
end
|
||||
end
|
||||
9
app/graphql/types/axis_type.rb
Normal file
9
app/graphql/types/axis_type.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Types
|
||||
class AxisType < Types::BaseObject
|
||||
field :id, ID, null: false
|
||||
field :name, String, null: false
|
||||
field :subjects, [SubjectType], null: false
|
||||
end
|
||||
end
|
||||
9
app/graphql/types/category_type.rb
Normal file
9
app/graphql/types/category_type.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Types
|
||||
class CategoryType < Types::BaseObject
|
||||
field :id, ID, null: false
|
||||
field :name, String, null: false
|
||||
field :subjects, [SubjectType], null: false
|
||||
end
|
||||
end
|
||||
@@ -1,19 +1,27 @@
|
||||
module Types
|
||||
class QueryType < Types::BaseObject
|
||||
# Add `node(id: ID!) and `nodes(ids: [ID!]!)`
|
||||
include GraphQL::Types::Relay::HasNodeField
|
||||
include GraphQL::Types::Relay::HasNodesField
|
||||
|
||||
field :questions, QuestionType.connection_type, null: false do
|
||||
argument :where, Inputs::QuestionWhereInput, required: false
|
||||
end
|
||||
|
||||
field :subjects, SubjectType.connection_type, null: false
|
||||
field :reviewers, UserType.connection_type, null: false
|
||||
field :current_user, Types::UserType, null: true
|
||||
|
||||
def questions(where: nil)
|
||||
Resolvers::QuestionsQueryResolver.new(Question, context: context, where: where).resolve
|
||||
end
|
||||
|
||||
def subjects
|
||||
Resolvers::SubjectsQueryResolver.new(context).resolve
|
||||
end
|
||||
|
||||
def reviewers
|
||||
Resolvers::ReviewersQueryResolver.new(context).resolve
|
||||
end
|
||||
|
||||
def current_user
|
||||
context[:current_user]
|
||||
end
|
||||
|
||||
25
app/graphql/types/subject_type.rb
Normal file
25
app/graphql/types/subject_type.rb
Normal file
@@ -0,0 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Types
|
||||
class SubjectType < Types::BaseObject
|
||||
field :id, ID, null: false
|
||||
field :name, String, null: false
|
||||
|
||||
field :axis, AxisType, null: false
|
||||
def axis
|
||||
dataloader.with(Sources::ActiveRecord, Axis).load(object.axis_id)
|
||||
end
|
||||
|
||||
field :category, CategoryType, null: false
|
||||
def category
|
||||
dataloader.with(Sources::ActiveRecord, Category).load(object.category_id)
|
||||
end
|
||||
|
||||
field :questions, QuestionType.connection_type, null: false do
|
||||
argument :where, Inputs::QuestionWhereInput, required: false
|
||||
end
|
||||
def questions(where: nil)
|
||||
Resolvers::QuestionsQueryResolver.new(object.questions, context: context, where: where).resolve
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user