From d38acaab98483fbe1b6051a3ab5723a077e7598d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Geonizeli?= Date: Thu, 21 Jul 2022 14:57:57 -0300 Subject: [PATCH] add subjects and reviwers query --- .../resolvers/reviewers_query_resolver.rb | 17 +++++++++++++ .../resolvers/subjects_query_resolver.rb | 12 +++++++++ app/graphql/sources/active_record.rb | 14 +++++++++++ app/graphql/types/axis_type.rb | 9 +++++++ app/graphql/types/category_type.rb | 9 +++++++ app/graphql/types/query_type.rb | 12 +++++++-- app/graphql/types/subject_type.rb | 25 +++++++++++++++++++ app/policies/subject_policy.rb | 7 ++++++ app/policies/user_policy.rb | 7 ++++++ 9 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 app/graphql/resolvers/reviewers_query_resolver.rb create mode 100644 app/graphql/resolvers/subjects_query_resolver.rb create mode 100644 app/graphql/sources/active_record.rb create mode 100644 app/graphql/types/axis_type.rb create mode 100644 app/graphql/types/category_type.rb create mode 100644 app/graphql/types/subject_type.rb create mode 100644 app/policies/subject_policy.rb create mode 100644 app/policies/user_policy.rb diff --git a/app/graphql/resolvers/reviewers_query_resolver.rb b/app/graphql/resolvers/reviewers_query_resolver.rb new file mode 100644 index 0000000..a61fe5e --- /dev/null +++ b/app/graphql/resolvers/reviewers_query_resolver.rb @@ -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 diff --git a/app/graphql/resolvers/subjects_query_resolver.rb b/app/graphql/resolvers/subjects_query_resolver.rb new file mode 100644 index 0000000..2f1e511 --- /dev/null +++ b/app/graphql/resolvers/subjects_query_resolver.rb @@ -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 diff --git a/app/graphql/sources/active_record.rb b/app/graphql/sources/active_record.rb new file mode 100644 index 0000000..953a680 --- /dev/null +++ b/app/graphql/sources/active_record.rb @@ -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 diff --git a/app/graphql/types/axis_type.rb b/app/graphql/types/axis_type.rb new file mode 100644 index 0000000..d64b30a --- /dev/null +++ b/app/graphql/types/axis_type.rb @@ -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 diff --git a/app/graphql/types/category_type.rb b/app/graphql/types/category_type.rb new file mode 100644 index 0000000..fa4db3f --- /dev/null +++ b/app/graphql/types/category_type.rb @@ -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 diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 4bc8c33..540431b 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -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 diff --git a/app/graphql/types/subject_type.rb b/app/graphql/types/subject_type.rb new file mode 100644 index 0000000..31de97d --- /dev/null +++ b/app/graphql/types/subject_type.rb @@ -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 diff --git a/app/policies/subject_policy.rb b/app/policies/subject_policy.rb new file mode 100644 index 0000000..7de109c --- /dev/null +++ b/app/policies/subject_policy.rb @@ -0,0 +1,7 @@ +class SubjectPolicy < ApplicationPolicy + class Scope < Scope + def resolve + scope.all + end + end +end diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb new file mode 100644 index 0000000..f7e25c1 --- /dev/null +++ b/app/policies/user_policy.rb @@ -0,0 +1,7 @@ +class UserPolicy < ApplicationPolicy + class Scope < Scope + def resolve + scope.all + end + end +end