Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions app/graphql/mutations/create_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
module Mutations
class CreateProject < BaseMutation
description 'A mutation to create a new project'
input_object_class Types::CreateProjectInputType
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oo this is nicer 👀


field :project, Types::ProjectType, description: 'The project that has been created'

# rubocop:disable GraphQL/ExtractInputType
argument :components, [Types::ComponentInputType], required: false, description: 'Any project components'
argument :name, String, required: true, description: 'The name of the project'
argument :project_type, String, required: true, description: 'The type of project, e.g. python, html'
# rubocop:enable GraphQL/ExtractInputType

def resolve(**input)
project_hash = input.merge(user_id: context[:current_user_id],
components: input[:components]&.map(&:to_h))
Expand Down
31 changes: 31 additions & 0 deletions app/graphql/mutations/delete_project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Mutations
class DeleteProject < BaseMutation
description 'A mutation to delete an existing project'

argument :id, String, required: true, description: 'The ID of the project to delete'

field :id, String, 'The ID of the project that has been deleted'

def resolve(**input)
project = GlobalID.find(input[:id])

raise GraphQL::ExecutionError, 'Project not found' unless project

unless context[:current_ability].can?(:destroy, project)
raise GraphQL::ExecutionError, 'You are not permitted to delete that project'
end

return { id: project.id } if project.destroy

raise GraphQL::ExecutionError, "Deletion failed for project #{project.identifier}"
end

def ready?(...)
return true if context[:current_ability]&.can?(:destroy, Project, user_id: context[:current_user_id])

raise GraphQL::ExecutionError, 'You are not permitted to delete projects'
end
end
end
31 changes: 31 additions & 0 deletions app/graphql/mutations/update_project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Mutations
class UpdateProject < BaseMutation
description 'A mutation to update an existing project'

input_object_class Types::UpdateProjectInputType

field :project, Types::ProjectType, description: 'The project that has been updated'

def resolve(**input)
project = GlobalID.find(input[:id])
raise GraphQL::ExecutionError, 'Project not found' unless project

unless context[:current_ability].can?(:update, project)
raise GraphQL::ExecutionError,
'You are not permitted to update this project'
end

return { project: } if project.update(input.slice(:project_type, :name))

raise GraphQL::ExecutionError, project.errors.full_messages.join(', ')
end

def ready?(**_args)
return true if context[:current_ability]&.can?(:update, Project, user_id: context[:current_user_id])

raise GraphQL::ExecutionError, 'You are not permitted to update a project'
end
end
end
11 changes: 11 additions & 0 deletions app/graphql/types/create_project_input_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Types
class CreateProjectInputType < Types::BaseInputObject
description 'Represents a project during creation'

argument :components, [Types::ProjectComponentInputType], required: false, description: 'Any project components'
argument :name, String, required: true, description: 'The name of the project'
argument :project_type, String, required: true, description: 'The type of project, e.g. python, html'
end
end
4 changes: 3 additions & 1 deletion app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Types
class MutationType < Types::BaseObject
field :create_project, mutation: Mutations::CreateProject, description: 'A mutation to create a project'
field :create_project, mutation: Mutations::CreateProject, description: 'Create a project, complete with components'
field :delete_project, mutation: Mutations::DeleteProject, description: 'Delete an existing project'
field :update_project, mutation: Mutations::UpdateProject, description: 'Update fields on an existing project'
end
end
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# frozen_string_literal: true

module Types
class ComponentInputType < Types::BaseInputObject
class ProjectComponentInputType < Types::BaseInputObject
description 'Represents a project component during a mutation'

argument :content, String, required: false, description: 'The text content of the component'
argument :default, Boolean, required: true, description: 'If this is the default component on a project'
argument :extension, String, required: true, description: 'The file extension of the component, e.g. html, csv, py'
argument :name, String, required: true, description: 'The name of the file'
argument :project_id, ID, required: false,
description: 'The ID of the project this component should be assocated with'
end
end
11 changes: 11 additions & 0 deletions app/graphql/types/update_project_input_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Types
class UpdateProjectInputType < Types::BaseInputObject
description 'Represents a project during an update'

argument :id, String, required: true, description: 'The ID of the project to update'
argument :name, String, required: false, description: 'The name of the project'
argument :project_type, String, required: false, description: 'The type of project, e.g. python, html'
end
end
139 changes: 112 additions & 27 deletions db/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -84,73 +84,73 @@ type ComponentEdge {
}

"""
Represents a project component during a mutation
Represents a project during creation
"""
input ComponentInput {
"""
The text content of the component
"""
content: String

input CreateProjectInput {
"""
If this is the default component on a project
A unique identifier for the client performing the mutation.
"""
default: Boolean!
clientMutationId: String

"""
The file extension of the component, e.g. html, csv, py
Any project components
"""
extension: String!
components: [ProjectComponentInput!]

"""
The name of the file
The name of the project
"""
name: String!

"""
The ID of the project this component should be assocated with
The type of project, e.g. python, html
"""
projectId: ID
projectType: String!
}

"""
Autogenerated input type of CreateProject
Autogenerated return type of CreateProject.
"""
input CreateProjectInput {
type CreateProjectPayload {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String

"""
Any project components
The project that has been created
"""
components: [ComponentInput!]
project: Project
}

"""
Autogenerated input type of DeleteProject
"""
input DeleteProjectInput {
"""
The name of the project
A unique identifier for the client performing the mutation.
"""
name: String!
clientMutationId: String

"""
The type of project, e.g. python, html
The ID of the project to delete
"""
projectType: String!
id: String!
}

"""
Autogenerated return type of CreateProject.
Autogenerated return type of DeleteProject.
"""
type CreateProjectPayload {
type DeleteProjectPayload {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String

"""
The project that has been created
The ID of the project that has been deleted
"""
project: Project
id: String
}

"""
Expand Down Expand Up @@ -220,14 +220,34 @@ type ImageEdge {

type Mutation {
"""
A mutation to create a project
Create a project, complete with components
"""
createProject(
"""
Parameters for CreateProject
"""
input: CreateProjectInput!
): CreateProjectPayload

"""
Delete an existing project
"""
deleteProject(
"""
Parameters for DeleteProject
"""
input: DeleteProjectInput!
): DeleteProjectPayload

"""
Update fields on an existing project
"""
updateProject(
"""
Parameters for UpdateProject
"""
input: UpdateProjectInput!
): UpdateProjectPayload
}

"""
Expand Down Expand Up @@ -360,6 +380,31 @@ type Project implements Node {
userId: Uuid
}

"""
Represents a project component during a mutation
"""
input ProjectComponentInput {
"""
The text content of the component
"""
content: String

"""
If this is the default component on a project
"""
default: Boolean!

"""
The file extension of the component, e.g. html, csv, py
"""
extension: String!

"""
The name of the file
"""
name: String!
}

"""
The connection type for Project.
"""
Expand Down Expand Up @@ -462,6 +507,46 @@ type Query {
): ProjectConnection
}

"""
Represents a project during an update
"""
input UpdateProjectInput {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String

"""
The ID of the project to update
"""
id: String!

"""
The name of the project
"""
name: String

"""
The type of project, e.g. python, html
"""
projectType: String
}

"""
Autogenerated return type of UpdateProject.
"""
type UpdateProjectPayload {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String

"""
The project that has been updated
"""
project: Project
}

"""
A globally unique ID
"""
Expand Down
Loading