graphql_grpc 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 327fc5da0a1c09984847c533c6a0013b6452c9477ef4e61f665f93b1614d7052
4
+ data.tar.gz: 4edee786627cdde0bb952d47f298cf00ecdd549d33cf91c2b796e6e8ef7dc840
5
+ SHA512:
6
+ metadata.gz: bc54e9263b7250e29579ed6608631ed9230a897060d4b07f4a70e9551ee10db7c210919c14904f398010fa19ecbdc7ca9f80745c86544cad9171e6f8f692f9ce
7
+ data.tar.gz: ef4160c62f28dfcf83aaab13ac5987e96a191a517b624271bee83aae204143e5d7043a829799d17c95d16444cdb884010bde6a441431c235cf2863c0cbd14926
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ # Specify your gem's dependencies in graphql_grpc.gemspec
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,145 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ graphql_grpc (0.1.0)
5
+ activesupport
6
+ graphql
7
+ grpc
8
+
9
+ GEM
10
+ specs:
11
+ activesupport (5.2.1)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 0.7, < 2)
14
+ minitest (~> 5.1)
15
+ tzinfo (~> 1.1)
16
+ addressable (2.5.2)
17
+ public_suffix (>= 2.0.2, < 4.0)
18
+ ast (2.4.0)
19
+ bombshell (0.1.6)
20
+ coderay (1.1.2)
21
+ concurrent-ruby (1.1.1)
22
+ diff-lcs (1.3)
23
+ ffi (1.9.25)
24
+ formatador (0.2.5)
25
+ google-protobuf (3.6.1)
26
+ googleapis-common-protos-types (1.0.2)
27
+ google-protobuf (~> 3.0)
28
+ graphql (1.8.11)
29
+ grpc (1.16.0-universal-darwin)
30
+ google-protobuf (~> 3.1)
31
+ googleapis-common-protos-types (~> 1.0.0)
32
+ guard (2.14.2)
33
+ formatador (>= 0.2.4)
34
+ listen (>= 2.7, < 4.0)
35
+ lumberjack (>= 1.0.12, < 2.0)
36
+ nenv (~> 0.1)
37
+ notiffany (~> 0.0)
38
+ pry (>= 0.9.12)
39
+ shellany (~> 0.0)
40
+ thor (>= 0.18.1)
41
+ guard-bundler (2.1.0)
42
+ bundler (~> 1.0)
43
+ guard (~> 2.2)
44
+ guard-compat (~> 1.1)
45
+ guard-compat (1.2.1)
46
+ guard-rspec (4.7.3)
47
+ guard (~> 2.1)
48
+ guard-compat (~> 1.1)
49
+ rspec (>= 2.99.0, < 4.0)
50
+ i18n (1.1.1)
51
+ concurrent-ruby (~> 1.0)
52
+ jaro_winkler (1.5.1-x86_64-darwin-17)
53
+ json-schema (2.8.1)
54
+ addressable (>= 2.4)
55
+ listen (3.1.5)
56
+ rb-fsevent (~> 0.9, >= 0.9.4)
57
+ rb-inotify (~> 0.9, >= 0.9.7)
58
+ ruby_dep (~> 1.2)
59
+ lumberjack (1.0.13)
60
+ method_source (0.9.0)
61
+ mime-types (2.6.2)
62
+ minitest (5.11.3)
63
+ nenv (0.3.0)
64
+ notiffany (0.1.1)
65
+ nenv (~> 0.1)
66
+ shellany (~> 0.0)
67
+ parallel (1.12.1)
68
+ parser (2.5.3.0)
69
+ ast (~> 2.4.0)
70
+ powerpack (0.1.2)
71
+ pry (0.11.3)
72
+ coderay (~> 1.1.0)
73
+ method_source (~> 0.9.0)
74
+ public_suffix (3.0.3)
75
+ rack (1.6.11)
76
+ rack-protection (1.5.5)
77
+ rack
78
+ rainbow (3.0.0)
79
+ rake (10.5.0)
80
+ rb-fsevent (0.10.3)
81
+ rb-inotify (0.9.10)
82
+ ffi (>= 0.5.0, < 2)
83
+ rspec (3.8.0)
84
+ rspec-core (~> 3.8.0)
85
+ rspec-expectations (~> 3.8.0)
86
+ rspec-mocks (~> 3.8.0)
87
+ rspec-core (3.8.0)
88
+ rspec-support (~> 3.8.0)
89
+ rspec-expectations (3.8.2)
90
+ diff-lcs (>= 1.2.0, < 2.0)
91
+ rspec-support (~> 3.8.0)
92
+ rspec-mocks (3.8.0)
93
+ diff-lcs (>= 1.2.0, < 2.0)
94
+ rspec-support (~> 3.8.0)
95
+ rspec-support (3.8.0)
96
+ rubocop (0.60.0)
97
+ jaro_winkler (~> 1.5.1)
98
+ parallel (~> 1.10)
99
+ parser (>= 2.5, != 2.5.1.1)
100
+ powerpack (~> 0.1)
101
+ rainbow (>= 2.2.2, < 4.0)
102
+ ruby-progressbar (~> 1.7)
103
+ unicode-display_width (~> 1.4.0)
104
+ ruby-progressbar (1.10.0)
105
+ ruby_dep (1.5.0)
106
+ ruby_robot (0.1.9)
107
+ bombshell
108
+ bundler (~> 1.16)
109
+ grpc
110
+ json-schema
111
+ sinatra (< 2)
112
+ sinatra-swagger-exposer
113
+ wirble
114
+ shellany (0.0.1)
115
+ sinatra (1.4.8)
116
+ rack (~> 1.5)
117
+ rack-protection (~> 1.4)
118
+ tilt (>= 1.3, < 3)
119
+ sinatra-swagger-exposer (0.4.0)
120
+ mime-types (~> 2.6.2)
121
+ sinatra (~> 1.4)
122
+ thor (0.20.0)
123
+ thread_safe (0.3.6)
124
+ tilt (2.0.8)
125
+ tzinfo (1.2.5)
126
+ thread_safe (~> 0.1)
127
+ unicode-display_width (1.4.0)
128
+ wirble (0.1.3)
129
+
130
+ PLATFORMS
131
+ ruby
132
+
133
+ DEPENDENCIES
134
+ bundler (~> 1.16)
135
+ graphql_grpc!
136
+ guard-bundler
137
+ guard-rspec
138
+ pry
139
+ rake (~> 10.0)
140
+ rubocop
141
+ ruby_robot
142
+ wirble
143
+
144
+ BUNDLED WITH
145
+ 1.16.2
data/Guardfile ADDED
@@ -0,0 +1,75 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ require 'guard/compat/plugin'
28
+
29
+ guard :rspec, cmd: 'bundle exec rspec' do
30
+ require 'guard/rspec/dsl'
31
+ dsl = Guard::RSpec::Dsl.new(self)
32
+
33
+ # Feel free to open issues for suggestions and improvements
34
+
35
+ # RSpec files
36
+ rspec = dsl.rspec
37
+ watch(rspec.spec_helper) { rspec.spec_dir }
38
+ watch(rspec.spec_support) { rspec.spec_dir }
39
+ watch(rspec.spec_files)
40
+
41
+ # Ruby files
42
+ ruby = dsl.ruby
43
+ dsl.watch_spec_files_for(ruby.lib_files)
44
+
45
+ # Rails files
46
+ rails = dsl.rails(view_extensions: %w[erb haml slim])
47
+ dsl.watch_spec_files_for(rails.app_files)
48
+ dsl.watch_spec_files_for(rails.views)
49
+
50
+ watch(rails.controllers) do |m|
51
+ [
52
+ rspec.spec.call("routing/#{m[1]}_routing"),
53
+ rspec.spec.call("controllers/#{m[1]}_controller"),
54
+ rspec.spec.call("acceptance/#{m[1]}")
55
+ ]
56
+ end
57
+
58
+ # Rails config changes
59
+ watch(rails.spec_helper) { rspec.spec_dir }
60
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
61
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
62
+
63
+ # Capybara features specs
64
+ watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
65
+ watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
66
+
67
+ watch(%r{^lib/graphql_grpc/(.+)\.rb}) { |_m| rspec.spec.call('graphql_grpc') }
68
+ watch(%r{^doc/(.+)}) { |_m| rspec.spec.call('graphql_grpc') }
69
+
70
+ # Turnip features and steps
71
+ watch(%r{^spec/acceptance/(.+)\.feature$})
72
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
73
+ Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance'
74
+ end
75
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # graphql_grpc
2
+
3
+ Generate and run a GraphQL-to-gRPC service proxy quickly and easily based on gRPC client stubs. The graphql_grpc gem will build a GraphQL schema based on the gRPC client stub methods and types; queries and mutations can then be executed on the GraphQL schema which passes the requests on to the proxied gRPC service for processing, then returns the gRPC result as a GraphQL response.
4
+
5
+ See bin/example.rb with its comments for example usage, or https://github.com/ateamlunchbox/graphql_grpc_example
6
+ for a Ruby on Rails-based example.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ task default: [:spec]
6
+
7
+ desc 'Run the specs.'
8
+ RSpec::Core::RakeTask.new do |t|
9
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..')
5
+ require 'graphql_grpc'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/example.rb ADDED
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # MIT License
4
+ #
5
+ # Copyright (c) 2018, Dane Avilla
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ require 'bundler/setup'
26
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..')
27
+ require 'graphql_grpc'
28
+ require 'ruby_robot'
29
+
30
+ #
31
+ # This script communicates with the gRPC service packaged up in the 'ruby_robot' gem.
32
+ # Be sure to run `ruby_robot_grpc_server` (a command included with the ruby_robot gem)
33
+ # before attempting to run this script.
34
+ #
35
+
36
+ services = {
37
+ # See: https://grpc.io/docs/tutorials/basic/ruby.html#creating-the-client
38
+ :ruby_robot => ::RubyRobot::RubyRobot::Stub.new('localhost:31310', :this_channel_is_insecure)
39
+ }
40
+
41
+ proxy = GraphqlGrpc::Proxy.new(services, &lambda do |error|
42
+ STDERR.puts "Error in proxy"
43
+ error.backtrace.each { |i| STDERR.puts i }
44
+ STDERR.puts error
45
+ end)
46
+
47
+ STDERR.puts 'Be sure to run ruby_robot_grpc_server before attempting to use this proxy...'
48
+
49
+ # Call a method defined in :ruby_robot on the proxy
50
+ puts 'gRPC #remove result: ' + proxy.remove(nil).to_s
51
+ puts 'gRPC #report result: ' + proxy.report(nil).to_s
52
+
53
+ # GraphQL
54
+ gql_query_doc = <<GRAPHQL_QUERY
55
+ query {
56
+ report {
57
+ error {
58
+ error
59
+ message
60
+ }
61
+ }
62
+ }
63
+ GRAPHQL_QUERY
64
+
65
+ puts "GraphQL query result: " +
66
+ proxy.graphql.execute(GraphQL::Language::Parser.parse(gql_query_doc), {}).to_s
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,47 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'graphql_grpc/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'graphql_grpc'
7
+ spec.version = GraphqlGrpc::VERSION
8
+ spec.authors = ['Zane Claes', 'Dane Avilla']
9
+ spec.email = ['davilla@netflix.com']
10
+ spec.license = 'MIT'
11
+ spec.summary = 'Gem for building GraphQL-to-gRPC gateways (usually using Ruby on Rails).'
12
+ spec.description = 'This is a gem packaging up gRPC proxy code to GraphQL from http://examinedself.com/graphql-grpc/, along with code to generate a GraphQL schema from gRPC stubs.'
13
+ spec.homepage = 'https://github.com/ateamlunchbox/graphql_grpc'
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
19
+ else
20
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
21
+ end
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_runtime_dependency 'activesupport'
33
+ spec.add_runtime_dependency 'graphql'
34
+ spec.add_runtime_dependency 'grpc'
35
+
36
+ spec.add_development_dependency 'bundler', '~> 1.16'
37
+ spec.add_development_dependency 'rake', '~> 10.0'
38
+ spec.add_development_dependency 'wirble'
39
+
40
+ # Add ruby_robot in dev so there's a protobuf definition available
41
+ # within bin/console env.
42
+ spec.add_development_dependency 'guard-bundler'
43
+ spec.add_development_dependency 'guard-rspec'
44
+ spec.add_development_dependency 'pry'
45
+ spec.add_development_dependency 'rubocop'
46
+ spec.add_development_dependency 'ruby_robot'
47
+ end
@@ -0,0 +1,52 @@
1
+ # MIT License
2
+ #
3
+ # Copyright (c) 2018, Dane Avilla
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ module GraphqlGrpc
24
+ module Arrayify
25
+ #
26
+ # Translate from gRPC hashmaps w/ integer keys to arrays of objects with
27
+ # 'key' and 'value' fields.
28
+ #
29
+ # Recursively descend through the hash; if any hashes are encountered where
30
+ # all the keys are integers, convert that hash into an array of hashes with
31
+ # [{ key: <integer_value>, value: <the_value> },
32
+ # { key: <integer_value>, value: <the_value> },...]
33
+ #
34
+ # Example: {1: :hello, 2: :world} => [{key:1, value: :hello}, {key:2, value: :world}]
35
+ #
36
+ def arrayify_hashes(input)
37
+ case input.class.name.to_sym
38
+ when :Array
39
+ input.map { |i| arrayify_hashes(i) }
40
+ when :Hash
41
+ if input.keys.map(&:class).compact.sort.uniq == [Fixnum]
42
+ arr = input.to_a.map { |e| { key: e.first, value: e.last } }
43
+ result = arrayify_hashes(arr)
44
+ else
45
+ input.each { |k,v| input[k] = arrayify_hashes(v) }
46
+ end
47
+ else
48
+ input
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,98 @@
1
+ # MIT License
2
+ #
3
+ # Copyright (c) 2018, Zane Claes
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ require 'graphql'
24
+
25
+ #
26
+ # Hmm...override the InputObject definition so it actually
27
+ # can be turned into JSON vs. a String with:
28
+ # "#<GraphQL::Language::Nodes::InputObject:0x007fc1e140d9b8>"
29
+ #
30
+ ::GraphQL::Language::Nodes::InputObject.class_eval do
31
+ def to_json(*args)
32
+ to_h.to_json(*args)
33
+ end
34
+ end
35
+
36
+ module GraphqlGrpc
37
+ # Storage for an actual function definition.
38
+ # Implements a `call` method so that it may be invoked with a simple hash of params.
39
+ class Function
40
+ attr_reader :service_name, :name, :rpc_desc
41
+
42
+ def initialize(service_name, service_stub, rpc_desc)
43
+ @service_name = service_name
44
+ @service_stub = service_stub
45
+ @rpc_desc = rpc_desc
46
+ @name = ::GRPC::GenericService.underscore(rpc_desc.name.to_s).to_sym
47
+ end
48
+
49
+ def to_s
50
+ "<GrpcFunction #{service_name}##{name} >"
51
+ end
52
+
53
+ def function_args
54
+ result = TypeLibrary.descriptor_for(rpc_desc.input).types(InputTypeLibrary::PREFIX)
55
+ result.any? ? "(#{result.join(', ')})" : ''
56
+ end
57
+
58
+ def input_type
59
+ rpc_desc.input.to_s.split(':').last
60
+ end
61
+
62
+ def output_type
63
+ rpc_desc.output.to_s.split(':').last
64
+ end
65
+
66
+ def to_query_type
67
+ # Turns out the single *Request type should NOT be the single arg.
68
+ #
69
+ # If GrpcFunctionNameRequest has two fields:
70
+ #
71
+ # foo: <type>
72
+ # bar: <type>
73
+ #
74
+ # then the schema needs to show:
75
+ #
76
+ # GrpcFunctionName(foo: <type>, bar: <type>): GrpcFunctionNameResponse
77
+ #
78
+ # instead of:
79
+ #
80
+ # GrpcFunctionName(input: GrpcFunctionNameRequest): GrpcFunctionNameResponse
81
+ #
82
+ "#{rpc_desc.name}#{function_args}: #{output_type}!"
83
+ end
84
+
85
+ def call(params = {}, metadata = {})
86
+ args = [name, arg(params || {}), metadata: metadata]
87
+ result_hash = @service_stub.send(*args).to_hash
88
+ arrayify_hashes(result_hash)
89
+ end
90
+
91
+ private
92
+
93
+ # Build arguments to a func
94
+ def arg(params)
95
+ rpc_desc.input.decode_json(params.reject { |k, _v| k == :selections }.to_json)
96
+ end
97
+ end
98
+ end