ActiveStorage was launched with Rails 6.0 that is a built-in file manager for local, S3, or any other storage platforms. ActiveStorage own documentation is quite extensive to explain how it manages file uploads and how to call them.
While working with GraphQL in Rails, there, arises a problem with uploading a file to the system. One way is to upload the file through REST API but what if we want solely in GraphQL then there is a gem to do that. ApolloUploadServer.
I usually create a polymorphic associated document model for file uploading and then attach them after upload. So, my model looks like this:
This model also verifies the content type for images and pdfs uploading only. With
documentable, we can use polymorphism with other models.
Now we are creating the GraphQL type for document output. Mine looks like below:
url, and I am using s3 service so showing the service url. But in other environment I am using local storage, that is why I have different document URLs for the environment. All other parameters are fairly descriptive.
Now, comes the point to create a mutation resolver for uploading the file to the system. The complete documentation of ApolloUploadServer is here. After adding the gem to the Gemfile now we can create our mutation. My mutation looks like this:
doc is the active storage parameter saving the file, so, it is being used of ApolloUploadServer. The output will be the type we created before.