Last active
February 29, 2024 12:25
-
-
Save sulmanweb/aae082a30c3b7f2264895f7376ecdca8 to your computer and use it in GitHub Desktop.
ActiveStorage file uploading in GraphQL API in Rails
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
# app/graphql/types/objects/document_type.rb | |
module Mutations | |
class CreateDocument < BaseMutation | |
description "creates a document for the user in the system" | |
argument :doc, ApolloUploadServer::Upload, required: true | |
field :document, Types::Objects::DocumentType, null: false | |
def resolve(doc:) | |
authenticate_user | |
user = context[:current_user] | |
document = user.documents.build(doc: doc) | |
if document.save | |
return {document: document} | |
else | |
raise GraphQL::ExecutionError.new(document.errors.full_messages.join(', ')) | |
end | |
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
# app/models/document.rb | |
class Document < ApplicationRecord | |
## RELATIONSHIPS | |
has_one_attached :doc | |
belongs_to :documentable, polymorphic: true, optional: true | |
belongs_to :user, optional: true | |
## VALIDATIONS | |
validate :doc_presence, on: :create | |
# This validation validates that attachment can be any image of pdf | |
def doc_presence | |
pattern = %r{^(image|application)/(.)+$} | |
unless doc.attached? && pattern.match?(doc.attachment.blob.content_type) | |
errors.add(:doc, I18n.t("errors.models.document.file_presence")) | |
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
# app/graphql/types/objects/document_type.rb | |
module Types | |
module Objects | |
class DocumentType < Types::BaseObject | |
field :id, Integer, null: false | |
field :documentable_type, String, null: true | |
field :documentable_id, Integer, null: true | |
field :content_type, String, null: true | |
field :url, String, null: false | |
field :created_at, GraphQL::Types::ISO8601DateTime, null: false | |
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false | |
def url | |
if Rails.env.production? | |
object.doc.service_url | |
else | |
Rails.application.routes.url_helpers.rails_blob_url(object.doc) | |
end | |
end | |
def content_type | |
if object.doc.present? | |
object.doc.content_type | |
else | |
nil | |
end | |
end | |
end | |
end | |
end |
I'm sorry; I mostly work on Backend. I won't be able to help much. But my front-end colleagues solved it in VueJS. But it will be difficult to ask them.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@happycamper1129 https://github.com/jaydenseric/apollo-upload-client this is client part of ApolloUploadServer Ruby library