webmock-graphql 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: 00b6f39ab6614ae9e285b286f37df1c62c4f25c3e37223c51a66b5c399b43878
4
+ data.tar.gz: e985a530359a1ee5967a8c8fa080156ea4f78b67376ce333d34a4feeae835e50
5
+ SHA512:
6
+ metadata.gz: 80b5f077015dbd880805cec55dbe5af810ab6103e3be7f3f3b1a8efb5a3c29e49aab0bf9480581286711535906be0bc5591b3e17d2a6efc9a70f4058a64f7c77
7
+ data.tar.gz: 2406f0b3be70d087a51f9fc52478b9ce7c1b09782d82826170e2085e6b765c9803b7f8c7964f80527beb10d9f1986207706fd48d8c3689b1e9cba048fad85797
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 3.2
@@ -0,0 +1,6 @@
1
+ {
2
+ "editor.formatOnSave": true,
3
+ "[ruby]": {
4
+ "editor.defaultFormatter": "testdouble.vscode-standard-ruby"
5
+ }
6
+ }
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-06-09
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in webmock-graphql.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "standard", "~> 1.3"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 qsona
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Webmock::Graphql
2
+
3
+ ## Installation
4
+
5
+ Install the gem and add to the application's Gemfile by executing:
6
+
7
+ $ bundle add webmock-graphql
8
+
9
+ If bundler is not being used to manage dependencies, install the gem by executing:
10
+
11
+ $ gem install webmock-graphql
12
+
13
+ ## Usage
14
+
15
+ TODO: Write usage instructions here
16
+
17
+ ## Development
18
+
19
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
20
+
21
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
22
+
23
+ ## Contributing
24
+
25
+ Bug reports and pull requests are welcome on GitHub at https://github.com/qsona/webmock-graphql.
26
+
27
+ ## License
28
+
29
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
File without changes
@@ -0,0 +1,5 @@
1
+ require_relative "graphql/stub_graphql_request"
2
+
3
+ RSpec.configure do |config|
4
+ config.include Webmock::Graphql::StubGraphqlRequest
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Webmock
4
+ module Graphql
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "graphql/version"
4
+
5
+ module Webmock
6
+ module Graphql
7
+ # Example:
8
+ #
9
+ # Webmock::Graphql.register(:hoge, "query($foo: Int!) { bar baz }") do
10
+ # params do
11
+ # foo
12
+ # bar { 1 } # default value
13
+ # end
14
+ # variables do
15
+ # { foo: foo }
16
+ # end
17
+ # data do
18
+ # { bar: bar, baz: 2 }
19
+ # end
20
+ # end
21
+ class BuilderContextClassFactory
22
+ def self.new(query)
23
+ Class.new do
24
+ @query = query
25
+
26
+ def initialize(**args)
27
+ args.each do |name, value|
28
+ instance_variable_set("@#{name}", value)
29
+ end
30
+ end
31
+
32
+ def variables
33
+ pr = self.class.variables_proc
34
+ pr && instance_exec(&pr)
35
+ end
36
+
37
+ def data
38
+ pr = self.class.data_proc
39
+ pr && instance_exec(&pr)
40
+ end
41
+
42
+ def errors
43
+ pr = self.class.errors_proc
44
+ pr && instance_exec(&pr)
45
+ end
46
+
47
+ class << self
48
+ attr_reader :query
49
+ attr_accessor :variables_proc, :data_proc, :errors_proc
50
+ end
51
+
52
+ def query
53
+ self.class.query
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ class ParamsContext
60
+ def initialize(builder_context_class)
61
+ @builder_context_class = builder_context_class
62
+ end
63
+ attr_reader :builder_context_class
64
+
65
+ def method_missing(name, &block)
66
+ builder_context_class.define_method(name) do
67
+ # self == builder_context instance
68
+ vname = "@#{name}"
69
+ if instance_variable_defined?(vname)
70
+ instance_variable_get(vname)
71
+ elsif block
72
+ instance_exec(&block)
73
+ else
74
+ raise "#{name} is not passed"
75
+ end
76
+ end
77
+ end
78
+
79
+ def respond_to_missing?(sym, include_private)
80
+ true
81
+ end
82
+ end
83
+
84
+ class RegisterContext
85
+ # attr_reader :variables_proc, :data_proc, :errors_proc
86
+ attr_reader :builder_context_class
87
+ def initialize(query)
88
+ @builder_context_class = BuilderContextClassFactory.new(query)
89
+ end
90
+
91
+ def params(&block)
92
+ params_context = ParamsContext.new(@builder_context_class)
93
+ params_context.instance_exec(&block)
94
+ end
95
+
96
+ def variables(&block)
97
+ builder_context_class.variables_proc = block
98
+ end
99
+
100
+ def data(&block)
101
+ builder_context_class.data_proc = block
102
+ end
103
+
104
+ def errors(&block)
105
+ builder_context_class.errors_proc = block
106
+ end
107
+ end
108
+
109
+ @stub_hash = {}
110
+ class << self
111
+ attr_accessor :default_url
112
+ attr_reader :stub_hash
113
+
114
+ def register(name, query, &block)
115
+ raise "stub #{name} is already registered" if stub_hash[name]
116
+
117
+ register_context = RegisterContext.new(query)
118
+ register_context.instance_exec(&block)
119
+
120
+ stub_hash[name] = register_context.builder_context_class
121
+ end
122
+
123
+ def reset!
124
+ self.default_url = nil
125
+ @stub_hash = {}
126
+ end
127
+ end
128
+
129
+ module StubGraphqlRequest
130
+ # stub_graphql_request(:hoge, a: 1, b: 2)
131
+ def stub_graphql_request(name, url = nil, **args)
132
+ builder_context_class = Webmock::Graphql.stub_hash[name]
133
+ raise "stub #{name} is not registered" unless builder_context_class
134
+
135
+ stub_graphql_context = builder_context_class.new(**args)
136
+
137
+ url ||= Webmock::Graphql.default_url
138
+ raise "url is not set" if url.nil?
139
+
140
+ WebMock.stub_request(:post, url).with(
141
+ body: {
142
+ query: stub_graphql_context.query,
143
+ variables: stub_graphql_context.variables
144
+ }
145
+ ).to_return(
146
+ body: {
147
+ data: stub_graphql_context.data,
148
+ errors: stub_graphql_context.errors
149
+ }.to_json,
150
+ headers: {"Content-Type" => "application/json"}
151
+ )
152
+ end
153
+ end
154
+
155
+ # enable `Webmock::Graphql.stub_graphql_request`
156
+ extend StubGraphqlRequest
157
+ end
158
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webmock-graphql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - qsona
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-06-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: webmock
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Library for stubbing graphql request (from ruby to other services), wrapper
28
+ of webmock
29
+ email:
30
+ - mori.jmk@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rspec"
36
+ - ".standard.yml"
37
+ - ".vscode/settings.json"
38
+ - CHANGELOG.md
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/webmock/graphql.rb
44
+ - lib/webmock/graphql/builder_context.rb
45
+ - lib/webmock/graphql/rspec.rb
46
+ - lib/webmock/graphql/version.rb
47
+ homepage: https://github.com/qsona/webmock-graphql
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ homepage_uri: https://github.com/qsona/webmock-graphql
52
+ source_code_uri: https://github.com/qsona/webmock-graphql
53
+ changelog_uri: https://github.com/qsona/webmock-graphql/tree/master/CHANGELOG.md
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 3.2.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.4.10
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Library for stubbing graphql request
73
+ test_files: []