From eb5caa6a617fc6420902b061585e1bd5a7385c9e Mon Sep 17 00:00:00 2001 From: Rob Sterner Date: Thu, 4 Jan 2024 21:09:04 -0500 Subject: [PATCH] should be good for a first pass --- app/controllers/graphql_controller.rb | 1 + app/graphql/mutations/base_mutation.rb | 7 ++--- app/graphql/mutations/create_work_week.rb | 14 ++++++++++ app/graphql/mutations/set_current_company.rb | 13 +++++++++ app/graphql/mutations/update_work_week.rb | 14 ++++++++++ app/graphql/types/mutation_type.rb | 9 +++---- app/graphql/types/query_type.rb | 27 ++++++++++++++++--- .../types/staff_plan/assignment_type.rb | 4 +-- app/graphql/types/staff_plan/project_type.rb | 2 +- .../types/staff_plan/work_week_type.rb | 3 +-- 10 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 app/graphql/mutations/create_work_week.rb create mode 100644 app/graphql/mutations/set_current_company.rb create mode 100644 app/graphql/mutations/update_work_week.rb diff --git a/app/controllers/graphql_controller.rb b/app/controllers/graphql_controller.rb index b63ba2b3..6ce0ead2 100644 --- a/app/controllers/graphql_controller.rb +++ b/app/controllers/graphql_controller.rb @@ -15,6 +15,7 @@ def execute context = { # Query context goes here, for example: current_user: current_user, + current_company: current_company } result = StaffplanReduxSchema.execute(query, variables: variables, context: context, operation_name: operation_name) render json: result diff --git a/app/graphql/mutations/base_mutation.rb b/app/graphql/mutations/base_mutation.rb index 0ff6c4ee..4f318d4e 100644 --- a/app/graphql/mutations/base_mutation.rb +++ b/app/graphql/mutations/base_mutation.rb @@ -1,10 +1,7 @@ # 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 + class BaseMutation < GraphQL::Schema::Mutation + null false end end diff --git a/app/graphql/mutations/create_work_week.rb b/app/graphql/mutations/create_work_week.rb new file mode 100644 index 00000000..138411b1 --- /dev/null +++ b/app/graphql/mutations/create_work_week.rb @@ -0,0 +1,14 @@ +module Mutations + class CreateWorkWeek < BaseMutation + # arguments passed to the `resolve` method + # argument :description, String, required: true + # argument :url, String, required: true + + # return type from the mutation + type Types::StaffPlan::WorkWeekType + + def resolve + # TODO: implement + end + end +end \ No newline at end of file diff --git a/app/graphql/mutations/set_current_company.rb b/app/graphql/mutations/set_current_company.rb new file mode 100644 index 00000000..c5af4796 --- /dev/null +++ b/app/graphql/mutations/set_current_company.rb @@ -0,0 +1,13 @@ +module Mutations + class SetCurrentCompany < BaseMutation + # arguments passed to the `resolve` method + argument :company_id, ID, required: true + + # return type from the mutation + type Types::StaffPlan::CompanyType + + def resolve + # TODO: implement + end + end +end \ No newline at end of file diff --git a/app/graphql/mutations/update_work_week.rb b/app/graphql/mutations/update_work_week.rb new file mode 100644 index 00000000..cf2b2676 --- /dev/null +++ b/app/graphql/mutations/update_work_week.rb @@ -0,0 +1,14 @@ +module Mutations + class UpdateWorkWeek < BaseMutation + # arguments passed to the `resolve` method + # argument :description, String, required: true + # argument :url, String, required: true + + # return type from the mutation + type Types::StaffPlan::WorkWeekType + + def resolve + # TODO: implement + end + end +end \ No newline at end of file diff --git a/app/graphql/types/mutation_type.rb b/app/graphql/types/mutation_type.rb index 5b9c1ade..321b5dee 100644 --- a/app/graphql/types/mutation_type.rb +++ b/app/graphql/types/mutation_type.rb @@ -2,11 +2,8 @@ module Types class MutationType < Types::BaseObject - # TODO: remove me - field :test_field, String, null: false, - description: "An example field added by the generator" - def test_field - "Hello World" - end + field :set_current_company, mutation: Mutations::SetCurrentCompany + field :create_work_week, mutation: Mutations::CreateWorkWeek + field :update_work_week, mutation: Mutations::UpdateWorkWeek end end diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 4e132232..8a50de20 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -23,9 +23,30 @@ def clients context[:current_user].current_company.clients.all end - field :assignments, [Types::StaffPlan::AssignmentType], null: false - def assignments - context[:current_user].assignments.all + field :project_assignments, [Types::StaffPlan::AssignmentType], null: false do + argument :project_id, ID, required: true, description: "ID of the project to fetch assignments for." + end + + def project_assignments(project_id: nil) + context[:current_company] + .projects + .find(project_id) + .assignments + .all + end + + field :user_assignments, [Types::StaffPlan::AssignmentType], null: false do + argument :user_id, ID, required: false, + description: "ID of the user to fetch assignments for. The current user's assignments will be returned if this argument is not provided." + end + def user_assignments(user_id: nil) + target = if user_id.present? + context[:current_company].users.find(user_id) + else + context[:current_user] + end + + target.assignments.all end field :users, [Types::StaffPlan::UserType], null: false diff --git a/app/graphql/types/staff_plan/assignment_type.rb b/app/graphql/types/staff_plan/assignment_type.rb index 0467a6a7..cdc0ec44 100644 --- a/app/graphql/types/staff_plan/assignment_type.rb +++ b/app/graphql/types/staff_plan/assignment_type.rb @@ -4,8 +4,8 @@ module Types module StaffPlan class AssignmentType < Types::BaseObject field :id, ID, null: false - field :user_id, ID, null: false - field :project_id, ID, null: false + field :user, Types::StaffPlan::UserType, null: false + field :project, Types::StaffPlan::ProjectType, null: false field :status, String, null: false field :created_at, GraphQL::Types::ISO8601DateTime, null: false field :updated_at, GraphQL::Types::ISO8601DateTime, null: false diff --git a/app/graphql/types/staff_plan/project_type.rb b/app/graphql/types/staff_plan/project_type.rb index 0d080664..1a7f7a71 100644 --- a/app/graphql/types/staff_plan/project_type.rb +++ b/app/graphql/types/staff_plan/project_type.rb @@ -4,7 +4,7 @@ module Types module StaffPlan class ProjectType < Types::BaseObject field :id, ID, null: false - field :client_id, ID, null: false + field :client, Types::StaffPlan::ClientType, null: false field :name, String, null: false field :status, String, null: false field :payment_frequency, String, null: false diff --git a/app/graphql/types/staff_plan/work_week_type.rb b/app/graphql/types/staff_plan/work_week_type.rb index 4a90ac26..76fbd2a9 100644 --- a/app/graphql/types/staff_plan/work_week_type.rb +++ b/app/graphql/types/staff_plan/work_week_type.rb @@ -4,6 +4,7 @@ module Types module StaffPlan class WorkWeekType < Types::BaseObject field :id, ID, null: false + field :user, Types::StaffPlan::UserType, null: false field :assignment_id, Integer, null: false field :cweek, Integer, null: false field :year, Integer, null: false @@ -13,8 +14,6 @@ class WorkWeekType < Types::BaseObject field :created_at, GraphQL::Types::ISO8601DateTime, null: false field :updated_at, GraphQL::Types::ISO8601DateTime, null: false - field :user, Types::StaffPlan::UserType, null: false - def user object.user end