Embed apollo_upload_server

This commit is contained in:
Pirate Praveen 2019-08-03 11:04:32 +05:30
parent 84f8cb92ee
commit 57e40bf3b0
21 changed files with 536 additions and 0 deletions

View file

@ -0,0 +1,8 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

View file

@ -0,0 +1 @@
--require spec_helper

View file

@ -0,0 +1,9 @@
language: ruby
cache: bundler
rvm:
- 2.3.8
- 2.4
- 2.4.3
- 2.5
install: bundle install --jobs=3 --retry=3
script: bundle exec rspec

View file

@ -0,0 +1,74 @@
# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of experience,
nationality, personal appearance, race, religion, or sexual identity and
orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment
include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at ifuelen@gmail.com. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at [http://contributor-covenant.org/version/1/4][version]
[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/

View file

@ -0,0 +1,6 @@
source 'https://rubygems.org'
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
# Specify your gem's dependencies in apollo_upload_server.gemspec
gemspec

View file

@ -0,0 +1,144 @@
PATH
remote: .
specs:
apollo_upload_server (2.0.0.beta.3)
graphql (>= 1.8)
rails (>= 4.2)
GEM
remote: https://rubygems.org/
specs:
actioncable (5.2.2)
actionpack (= 5.2.2)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailer (5.2.2)
actionpack (= 5.2.2)
actionview (= 5.2.2)
activejob (= 5.2.2)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.2.2)
actionview (= 5.2.2)
activesupport (= 5.2.2)
rack (~> 2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.2.2)
activesupport (= 5.2.2)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activejob (5.2.2)
activesupport (= 5.2.2)
globalid (>= 0.3.6)
activemodel (5.2.2)
activesupport (= 5.2.2)
activerecord (5.2.2)
activemodel (= 5.2.2)
activesupport (= 5.2.2)
arel (>= 9.0)
activestorage (5.2.2)
actionpack (= 5.2.2)
activerecord (= 5.2.2)
marcel (~> 0.3.1)
activesupport (5.2.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (9.0.0)
builder (3.2.3)
concurrent-ruby (1.1.4)
crass (1.0.4)
diff-lcs (1.3)
erubi (1.8.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
graphql (1.8.13)
i18n (1.5.3)
concurrent-ruby (~> 1.0)
loofah (2.2.3)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (0.9.2)
mimemagic (0.3.3)
mini_mime (1.0.1)
mini_portile2 (2.4.0)
minitest (5.11.3)
nio4r (2.3.1)
nokogiri (1.10.1)
mini_portile2 (~> 2.4.0)
rack (2.0.6)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (5.2.2)
actioncable (= 5.2.2)
actionmailer (= 5.2.2)
actionpack (= 5.2.2)
actionview (= 5.2.2)
activejob (= 5.2.2)
activemodel (= 5.2.2)
activerecord (= 5.2.2)
activestorage (= 5.2.2)
activesupport (= 5.2.2)
bundler (>= 1.3.0)
railties (= 5.2.2)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.0.4)
loofah (~> 2.2, >= 2.2.2)
railties (5.2.2)
actionpack (= 5.2.2)
activesupport (= 5.2.2)
method_source
rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0)
rake (10.5.0)
rspec (3.8.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-core (3.8.0)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.1)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (0.20.3)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
PLATFORMS
ruby
DEPENDENCIES
apollo_upload_server!
bundler (~> 1.16)
rake (~> 10.0)
rspec (~> 3.5)
BUNDLED WITH
1.17.2

View file

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2017 Artur Plysyuk
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View file

@ -0,0 +1,53 @@
# ApolloUploadServer
Middleware which allows you to upload files using [graphql-ruby](https://github.com/rmosolgo/graphql-ruby), [apollo-upload-client](https://github.com/jaydenseric/apollo-upload-client) and Ruby on Rails.
Note: this implementation uses [v2 of the GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec/tree/v2.0.0-alpha.2), so you should use apollo-upload-client library >= v7.0.0-alpha.3. If you need support for [v1 of the GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec/tree/v1.0.0), you must
use [version 1.0.0](https://github.com/jetruby/apollo_upload_server-ruby/tree/1.0.0) of this gem.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'apollo_upload_server', '2.0.0.beta.3'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install apollo_upload_server
Middleware will be used automatically.
Gem adds custom `Upload` type to your GraphQL types.
Use `ApolloUploadServer::Upload` type for your file as input field:
```ruby
input_field :file, ApolloUploadServer::Upload
```
That's all folks!
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/jetruby/apollo_upload_server-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the ApolloUploadServer projects codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jetruby/apollo_upload_server-ruby/blob/master/CODE_OF_CONDUCT.md).
## About JetRuby
ApolloUploadServer is maintained and founded by JetRuby Agency.
We love open source software!
See [our projects][portfolio] or
[contact us][contact] to design, develop, and grow your product.
[portfolio]: http://jetruby.com/portfolio/
[contact]: http://jetruby.com/#contactUs

View file

@ -0,0 +1,2 @@
require 'bundler/gem_tasks'
task default: :spec

View file

@ -0,0 +1,29 @@
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'apollo_upload_server/version'
Gem::Specification.new do |spec|
spec.name = 'apollo_upload_server'
spec.version = ApolloUploadServer::VERSION
spec.authors = ['JetRuby']
spec.email = ['engineering@jetruby.com']
spec.summary = 'Middleware which allows you to upload files using graphql and multipart/form-data.'
spec.description = 'apollo-upload-server implementation for Ruby on Rails as middleware.'
spec.homepage = 'https://github.com/jetruby/apollo_upload_server-ruby'
spec.license = 'MIT'
spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.bindir = 'bin'
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ['lib']
spec.add_dependency 'rails', '>= 4.2'
spec.add_dependency 'graphql', '>= 1.8'
spec.add_development_dependency 'bundler', '~> 1.16'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.5'
end

View file

@ -0,0 +1,14 @@
#!/usr/bin/env ruby
require 'bundler/setup'
require 'apollo_upload_server'
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start
require 'irb'
IRB.start(__FILE__)

View file

@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx
bundle install
# Do any other automated setup that you need to do here

View file

@ -0,0 +1,5 @@
require 'apollo_upload_server/graphql_data_builder'
require 'apollo_upload_server/middleware'
require 'apollo_upload_server/version'
require 'apollo_upload_server/railtie'
require 'apollo_upload_server/upload'

View file

@ -0,0 +1,80 @@
# frozen_string_literal: true
require 'json'
require 'apollo_upload_server/wrappers/uploaded_file'
module ApolloUploadServer
class GraphQLDataBuilder
def call(params)
operations = safe_json_parse(params['operations'])
file_mapper = safe_json_parse(params['map'])
return nil if operations.nil? || file_mapper.nil?
if operations.is_a?(Hash)
single_transformation(file_mapper, operations, params)
else
{ '_json' => multiple_transformation(file_mapper, operations, params) }
end
end
private
def single_transformation(file_mapper, operations, params)
operations = operations.dup
file_mapper.each do |file_index, paths|
paths.each do |path|
splited_path = path.split('.')
# splited_path => 'variables.input.profile_photo'; splited_path[0..-2] => ['variables', 'input']
# dig from first to penultimate key, and merge last key with value as file
field = get_parent_field(operations, splited_path)
assign_file(field, splited_path, params[file_index])
end
end
operations
end
def multiple_transformation(file_mapper, operations, params)
operations = operations.dup
file_mapper.each do |file_index, paths|
paths.each do |path|
splited_path = path.split('.')
# dig from second to penultimate key, and merge last key with value as file to operation with first key index
field = operations[splited_path.first.to_i].dig(*splited_path[1..-2])
assign_file(field, splited_path, params[file_index])
end
end
operations
end
def safe_json_parse(data)
JSON.parse(data)
rescue JSON::ParserError
nil
end
def get_parent_field(operations, splited_path)
# returns parent element of file field
splited_path[0..-2].inject(operations) do |element, key|
case element
when Array
element[Integer(key)]
else
element[key]
end
end
end
def assign_file(field, splited_path, file)
wrapped_file = Wrappers::UploadedFile.new(file)
if field.is_a? Hash
field.merge!(splited_path.last => wrapped_file)
elsif field.is_a? Array
field[splited_path.last.to_i] = wrapped_file
end
end
end
end

View file

@ -0,0 +1,23 @@
require 'apollo_upload_server/graphql_data_builder'
module ApolloUploadServer
class Middleware
def initialize(app)
@app = app
end
def call(env)
request = ActionDispatch::Request.new(env)
params = request.params
if env['CONTENT_TYPE'].to_s.include?('multipart/form-data') && params['operations'].present? && params['map'].present?
result = GraphQLDataBuilder.new.call(request.params)
result&.each do |key, value|
request.update_param(key, value)
end
end
@app.call(env)
end
end
end

View file

@ -0,0 +1,9 @@
require 'apollo_upload_server/middleware'
module ApolloUploadServer
class Railtie < Rails::Railtie
initializer 'apollo_upload_server.apply_middleware' do |app|
app.middleware.use Middleware
end
end
end

View file

@ -0,0 +1,17 @@
# frozen_string_literal: true
require 'graphql'
module ApolloUploadServer
class Upload < GraphQL::Schema::Scalar
graphql_name "Upload"
def self.coerce_input(value, _ctx)
value
end
def self.coerse_result(value, _ctx)
value
end
end
end

View file

@ -0,0 +1,3 @@
module ApolloUploadServer
VERSION = '2.0.0.beta.3'.freeze
end

View file

@ -0,0 +1,18 @@
# frozen_string_literal: true
require 'delegate'
require 'action_dispatch/http/upload'
module ApolloUploadServer
module Wrappers
class UploadedFile < DelegateClass(::ActionDispatch::Http::UploadedFile)
def initialize(wrapped_foo)
super
end
def as_json(options = nil)
instance_values.except('tempfile').as_json(options)
end
end
end
end

View file

@ -0,0 +1,11 @@
--- a/Gemfile
+++ b/Gemfile
@@ -92,7 +92,7 @@
# GraphQL API
gem 'graphql', '~> 1.8'
gem 'graphiql-rails', '~> 1.4', '>= 1.4.10'
-gem 'apollo_upload_server', '~> 2.0', '>= 2.0.0.beta3'
+gem 'apollo_upload_server', '~> 2.0.0.beta3', path: 'vendor/gems/apollo-upload-server-2.0.0.beta.3/'
# Disable strong_params so that Mash does not respond to :permitted?
gem 'hashie-forbidden_attributes'

View file

@ -31,3 +31,4 @@
bump-devise-to-4-6.patch
0800-embed-omniauth0ultraauth.patch
0810-embed-omniauth-salesforce.patch
0820-embed-apollo-upload-server.patch