graphql_util 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +181 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +66 -0
- data/LICENSE.txt +21 -0
- data/README.md +155 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/graphql_util/client.rb +17 -0
- data/lib/graphql_util/http.rb +21 -0
- data/lib/graphql_util/schema.rb +32 -0
- data/lib/graphql_util/version.rb +5 -0
- data/lib/graphql_util.rb +61 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: aa249e09e5755630deaa4b7638f4bf5e03e144386fed0280bbb0638ab944c9da
|
4
|
+
data.tar.gz: b55751528e31214081eb07585d5ed205949376eedf2ed19fa04cb2a1d06d654e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5ab65f496ca6b6502309508f7551956798a6a0e859b8627721eaf57c090460354da28b55445e791ba9ac02d26f8cd16d5af21c2453b68bec7a9bddba2a1ee83e
|
7
|
+
data.tar.gz: 10a82c850ac9af82ff90cc0d52656305fd3029f95b0df108c4a1f8e51413c7c99d929489427c644d08d881fdb815cc64e301142595d9ac38b4978f2779a6afdc
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
# Allow longer lines and methods
|
2
|
+
Layout/LineLength:
|
3
|
+
Max: 500
|
4
|
+
|
5
|
+
Metrics/MethodLength:
|
6
|
+
Max: 600
|
7
|
+
|
8
|
+
# Tests are declarative, no block length test
|
9
|
+
Metrics/BlockLength:
|
10
|
+
IgnoredMethods: ["describe", "context", "namespace"]
|
11
|
+
|
12
|
+
|
13
|
+
AllCops:
|
14
|
+
TargetRubyVersion: 2.5
|
15
|
+
# RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
|
16
|
+
# to ignore them, so only the ones explicitly set in this file are enabled.
|
17
|
+
DisabledByDefault: true
|
18
|
+
Exclude:
|
19
|
+
- "**/templates/**/*"
|
20
|
+
- "**/vendor/**/*"
|
21
|
+
- "**/vendor/**/.*"
|
22
|
+
- "**/node_modules/**/*"
|
23
|
+
- "actionpack/lib/action_dispatch/journey/parser.rb"
|
24
|
+
|
25
|
+
# Prefer &&/|| over and/or.
|
26
|
+
Style/AndOr:
|
27
|
+
Enabled: true
|
28
|
+
|
29
|
+
# Align `when` with `case`.
|
30
|
+
Layout/CaseIndentation:
|
31
|
+
Enabled: true
|
32
|
+
|
33
|
+
# Align comments with method definitions.
|
34
|
+
Layout/CommentIndentation:
|
35
|
+
Enabled: true
|
36
|
+
|
37
|
+
Layout/ElseAlignment:
|
38
|
+
Enabled: true
|
39
|
+
|
40
|
+
# Align `end` with the matching keyword or starting expression except for
|
41
|
+
# assignments, where it should be aligned with the LHS.
|
42
|
+
Layout/EndAlignment:
|
43
|
+
Enabled: true
|
44
|
+
EnforcedStyleAlignWith: variable
|
45
|
+
AutoCorrect: true
|
46
|
+
|
47
|
+
Layout/EmptyLineAfterMagicComment:
|
48
|
+
Enabled: true
|
49
|
+
|
50
|
+
# In a regular class definition, no empty lines around the body.
|
51
|
+
Layout/EmptyLinesAroundClassBody:
|
52
|
+
Enabled: true
|
53
|
+
|
54
|
+
# In a regular method definition, no empty lines around the body.
|
55
|
+
Layout/EmptyLinesAroundMethodBody:
|
56
|
+
Enabled: true
|
57
|
+
|
58
|
+
# In a regular module definition, no empty lines around the body.
|
59
|
+
Layout/EmptyLinesAroundModuleBody:
|
60
|
+
Enabled: true
|
61
|
+
|
62
|
+
Layout/FirstArgumentIndentation:
|
63
|
+
Enabled: true
|
64
|
+
|
65
|
+
# Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
|
66
|
+
Style/HashSyntax:
|
67
|
+
Enabled: true
|
68
|
+
|
69
|
+
# Method definitions after `private` or `protected` isolated calls need one
|
70
|
+
# extra level of indentation.
|
71
|
+
Layout/IndentationConsistency:
|
72
|
+
Enabled: true
|
73
|
+
EnforcedStyle: indented_internal_methods
|
74
|
+
|
75
|
+
# Two spaces, no tabs (for indentation).
|
76
|
+
Layout/IndentationWidth:
|
77
|
+
Enabled: true
|
78
|
+
|
79
|
+
Layout/LeadingCommentSpace:
|
80
|
+
Enabled: true
|
81
|
+
|
82
|
+
Layout/SpaceAfterColon:
|
83
|
+
Enabled: true
|
84
|
+
|
85
|
+
Layout/SpaceAfterComma:
|
86
|
+
Enabled: true
|
87
|
+
|
88
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
89
|
+
Enabled: true
|
90
|
+
|
91
|
+
Layout/SpaceAroundKeyword:
|
92
|
+
Enabled: true
|
93
|
+
|
94
|
+
Layout/SpaceAroundOperators:
|
95
|
+
Enabled: true
|
96
|
+
|
97
|
+
Layout/SpaceBeforeComma:
|
98
|
+
Enabled: true
|
99
|
+
|
100
|
+
Layout/SpaceBeforeFirstArg:
|
101
|
+
Enabled: true
|
102
|
+
|
103
|
+
Style/DefWithParentheses:
|
104
|
+
Enabled: true
|
105
|
+
|
106
|
+
# Defining a method with parameters needs parentheses.
|
107
|
+
Style/MethodDefParentheses:
|
108
|
+
Enabled: true
|
109
|
+
|
110
|
+
# Style/FrozenStringLiteralComment:
|
111
|
+
# Enabled: true
|
112
|
+
# EnforcedStyle: always
|
113
|
+
# Exclude:
|
114
|
+
# - "actionview/test/**/*.builder"
|
115
|
+
# - "actionview/test/**/*.ruby"
|
116
|
+
# - "actionpack/test/**/*.builder"
|
117
|
+
# - "actionpack/test/**/*.ruby"
|
118
|
+
# - "activestorage/db/migrate/**/*.rb"
|
119
|
+
# - "db/migrate/**/*.rb"
|
120
|
+
# - "db/*.rb"
|
121
|
+
|
122
|
+
# Use `foo {}` not `foo{}`.
|
123
|
+
Layout/SpaceBeforeBlockBraces:
|
124
|
+
Enabled: true
|
125
|
+
|
126
|
+
# Use `foo { bar }` not `foo {bar}`.
|
127
|
+
Layout/SpaceInsideBlockBraces:
|
128
|
+
Enabled: true
|
129
|
+
|
130
|
+
# Use `{ a: 1 }` not `{a:1}`.
|
131
|
+
Layout/SpaceInsideHashLiteralBraces:
|
132
|
+
Enabled: true
|
133
|
+
|
134
|
+
Layout/SpaceInsideParens:
|
135
|
+
Enabled: true
|
136
|
+
|
137
|
+
# Check quotes usage according to lint rule below.
|
138
|
+
Style/StringLiterals:
|
139
|
+
Enabled: true
|
140
|
+
EnforcedStyle: single_quotes
|
141
|
+
|
142
|
+
# Detect hard tabs, no hard tabs.
|
143
|
+
Layout/IndentationStyle:
|
144
|
+
Enabled: true
|
145
|
+
|
146
|
+
# Blank lines should not have any spaces.
|
147
|
+
Layout/TrailingEmptyLines:
|
148
|
+
Enabled: true
|
149
|
+
|
150
|
+
# No trailing whitespace.
|
151
|
+
Layout/TrailingWhitespace:
|
152
|
+
Enabled: true
|
153
|
+
|
154
|
+
# Use quotes for string literals when they are enough.
|
155
|
+
Style/RedundantPercentQ:
|
156
|
+
Enabled: true
|
157
|
+
|
158
|
+
# Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
|
159
|
+
Lint/RequireParentheses:
|
160
|
+
Enabled: true
|
161
|
+
|
162
|
+
Lint/RedundantStringCoercion:
|
163
|
+
Enabled: true
|
164
|
+
|
165
|
+
# Supports --auto-correct
|
166
|
+
Style/RedundantSelf:
|
167
|
+
Description: Don't use self where it's not needed.
|
168
|
+
StyleGuide: https://github.com/rubocop-hq/ruby-style-guide#no-self-unless-required
|
169
|
+
Enabled: true
|
170
|
+
|
171
|
+
Style/RedundantReturn:
|
172
|
+
Enabled: true
|
173
|
+
AllowMultipleReturnValues: true
|
174
|
+
|
175
|
+
Style/Semicolon:
|
176
|
+
Enabled: true
|
177
|
+
AllowAsExpressionSeparator: true
|
178
|
+
|
179
|
+
# Prefer Foo.method over Foo::method
|
180
|
+
Style/ColonMethodCall:
|
181
|
+
Enabled: true
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
graphql_util (0.1.2)
|
5
|
+
graphql-client (~> 0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (7.0.0)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (>= 1.6, < 2)
|
13
|
+
minitest (>= 5.1)
|
14
|
+
tzinfo (~> 2.0)
|
15
|
+
addressable (2.8.0)
|
16
|
+
public_suffix (>= 2.0.2, < 5.0)
|
17
|
+
byebug (11.1.3)
|
18
|
+
concurrent-ruby (1.1.9)
|
19
|
+
crack (0.4.5)
|
20
|
+
rexml
|
21
|
+
diff-lcs (1.4.4)
|
22
|
+
graphql (1.13.2)
|
23
|
+
graphql-client (0.17.0)
|
24
|
+
activesupport (>= 3.0)
|
25
|
+
graphql (~> 1.10)
|
26
|
+
hashdiff (1.0.1)
|
27
|
+
i18n (1.8.11)
|
28
|
+
concurrent-ruby (~> 1.0)
|
29
|
+
minitest (5.15.0)
|
30
|
+
public_suffix (4.0.6)
|
31
|
+
rake (12.3.3)
|
32
|
+
rexml (3.2.5)
|
33
|
+
rspec (3.10.0)
|
34
|
+
rspec-core (~> 3.10.0)
|
35
|
+
rspec-expectations (~> 3.10.0)
|
36
|
+
rspec-mocks (~> 3.10.0)
|
37
|
+
rspec-core (3.10.1)
|
38
|
+
rspec-support (~> 3.10.0)
|
39
|
+
rspec-expectations (3.10.1)
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
+
rspec-support (~> 3.10.0)
|
42
|
+
rspec-mocks (3.10.2)
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
+
rspec-support (~> 3.10.0)
|
45
|
+
rspec-support (3.10.2)
|
46
|
+
tzinfo (2.0.4)
|
47
|
+
concurrent-ruby (~> 1.0)
|
48
|
+
vcr (6.0.0)
|
49
|
+
webmock (3.14.0)
|
50
|
+
addressable (>= 2.8.0)
|
51
|
+
crack (>= 0.3.2)
|
52
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
53
|
+
|
54
|
+
PLATFORMS
|
55
|
+
ruby
|
56
|
+
|
57
|
+
DEPENDENCIES
|
58
|
+
byebug
|
59
|
+
graphql_util!
|
60
|
+
rake (~> 12.0)
|
61
|
+
rspec (~> 3.0)
|
62
|
+
vcr
|
63
|
+
webmock
|
64
|
+
|
65
|
+
BUNDLED WITH
|
66
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Elisacci Lapo
|
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,155 @@
|
|
1
|
+
![logo](https://user-images.githubusercontent.com/50866745/138700181-01b54097-b19c-4655-b032-237c1a77a1fb.png)
|
2
|
+
|
3
|
+
![](https://img.shields.io/badge/Ruby-CC342D?style=for-the-badge&logo=ruby&logoColor=white)
|
4
|
+
# GraphQL Util
|
5
|
+
|
6
|
+
The gem wraps the Ruby [graphql-client](https://github.com/github/graphql-client) gem, to dinamically define GraphQL Clients.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'graphql_util'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle install
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install graphql_util
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Define a GraphQL client class, like so:
|
27
|
+
|
28
|
+
```Ruby
|
29
|
+
class Client
|
30
|
+
GraphqlUtil.act_as_graphql_client(
|
31
|
+
self, # Required
|
32
|
+
endpoint: 'https://api.github.com/graphql', # Required
|
33
|
+
path: __dir__, # Required, (Recommended: __dir__)
|
34
|
+
headers: {} # Optional
|
35
|
+
)
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
The `act_as_graphql_client` method accepts the following parameters:
|
40
|
+
|
41
|
+
1. `self`, which allows GraphqlUtil to inject the required code inside the class;
|
42
|
+
2. `endpoint`, which has to be the URL of the GraphQL endpoint;
|
43
|
+
3. `path`, which has to be the location where to store operations files and the Schema dump. (`__dir__` is the suggested one, but any valid path will do);
|
44
|
+
4. `headers`, a Hash of headers to be attached to any performed HTTP request.
|
45
|
+
|
46
|
+
### Schema
|
47
|
+
|
48
|
+
The very first time the Client class loads, an Introspection Query will be performed agains the GraphQL endpoint to dump the relative Schema.
|
49
|
+
You'll find your `schema.json` dump file under the above mentioned `path`.
|
50
|
+
|
51
|
+
(Remember that the Graphql Schema may change over time, therefore whenever you need to update the dump, simply delete the file. The new version will get created later.)
|
52
|
+
|
53
|
+
### Queries and Mutations
|
54
|
+
|
55
|
+
The Graphql operations you want your client to perform must be defined inside `.graphql` files under any `dir` subdirectory.
|
56
|
+
|
57
|
+
### Example and Best practise
|
58
|
+
|
59
|
+
Let's take the Github Graphql API as an example.
|
60
|
+
|
61
|
+
1. First we create a directory (a Ruby module) named `github` to contain all the necessary code.
|
62
|
+
2. We then create a file called `client.rb` , just like this:
|
63
|
+
|
64
|
+
```Ruby
|
65
|
+
module Github
|
66
|
+
class Client
|
67
|
+
GraphqlUtil.act_as_graphql_client(
|
68
|
+
self,
|
69
|
+
endpoint: 'https://api.github.com/graphql',
|
70
|
+
path: __dir__,
|
71
|
+
headers: { # You can place any HTTP Header here
|
72
|
+
'Authorization': GITHUB_TOKEN
|
73
|
+
}
|
74
|
+
)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
3. Now we need to define our Queries / Mutations, to do so, place each operation definition under one (or many) subdirectory(ies), like this:
|
80
|
+
|
81
|
+
`github/queries/user_info.graphql`
|
82
|
+
```GraphQL
|
83
|
+
query userInfo($username: String!) {
|
84
|
+
user(login: $username) {
|
85
|
+
followers(first: 1) {
|
86
|
+
totalCount
|
87
|
+
}
|
88
|
+
}
|
89
|
+
}
|
90
|
+
```
|
91
|
+
|
92
|
+
or
|
93
|
+
|
94
|
+
`github/mutations/add_comment.graphql`
|
95
|
+
```GraphQL
|
96
|
+
mutation addComment($input: AddCommentInput!) {
|
97
|
+
clientMutaitonId
|
98
|
+
}
|
99
|
+
```
|
100
|
+
|
101
|
+
4. Now we have a file structure that looks like this:
|
102
|
+
|
103
|
+
```Code
|
104
|
+
github
|
105
|
+
├── queries
|
106
|
+
│ └── user_info.graphql
|
107
|
+
├── mutations
|
108
|
+
│ └── add_comment.graphql
|
109
|
+
├── client.rb
|
110
|
+
└── schema.json
|
111
|
+
```
|
112
|
+
|
113
|
+
5. Each defined operation generates a class method inside our client to perform the relative request. In our example `.user_info` & `.add_comment`.
|
114
|
+
6. Each defined method will accept arguments to be passed as variables.
|
115
|
+
|
116
|
+
```Ruby
|
117
|
+
Github::Client.user_info(username: 'LapoElisacci')
|
118
|
+
```
|
119
|
+
|
120
|
+
or
|
121
|
+
|
122
|
+
```Ruby
|
123
|
+
Github::Client.add_comment(input: { body: 'This gem is awesome!', subjectId: '12345678' })
|
124
|
+
```
|
125
|
+
|
126
|
+
## Constraints
|
127
|
+
|
128
|
+
The client Class as well as the sudirectories names are up to you, but only one level nesting is allowed.
|
129
|
+
Something like `anywhere/anything/whatever/whatever.graphql` won't produce the relative method, but `anywhere/anything/whatever.graphql` will, as long as `anywhere/whatever.rb` is the class that "act_as_graphql_client".
|
130
|
+
|
131
|
+
```Code
|
132
|
+
anywhere
|
133
|
+
├── anything
|
134
|
+
│ ├── wont_work
|
135
|
+
│ │ └── wont_work.graphql
|
136
|
+
│ └── anything_that_works.graphql
|
137
|
+
├── anything_2
|
138
|
+
│ └── anything_that_works_too.graphql
|
139
|
+
├── whatever.rb
|
140
|
+
└── schema.json
|
141
|
+
```
|
142
|
+
|
143
|
+
You can find more details about the `graphql-client` [here](https://github.com/github/graphql-client).
|
144
|
+
|
145
|
+
## Development
|
146
|
+
|
147
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
148
|
+
|
149
|
+
## Contributing
|
150
|
+
|
151
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/LapoElisacci/graphql_util.
|
152
|
+
|
153
|
+
## License
|
154
|
+
|
155
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "graphql_util"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'graphql/client'
|
4
|
+
|
5
|
+
class GraphqlUtil::Client < GraphQL::Client
|
6
|
+
#
|
7
|
+
# Performs a GraphQL Query and handles the response
|
8
|
+
#
|
9
|
+
# @param [String] parsed_query GraphQL parsed query
|
10
|
+
# @param [Hash] variables GraphQL query params
|
11
|
+
#
|
12
|
+
# @return [GraphQL::Client::Response] Request Response
|
13
|
+
#
|
14
|
+
def query(parsed_query, variables: {})
|
15
|
+
super(parsed_query, variables: variables, context: {})
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'graphql/client/http'
|
4
|
+
|
5
|
+
class GraphqlUtil::Http < GraphQL::Client::HTTP
|
6
|
+
attr_accessor :token
|
7
|
+
#
|
8
|
+
# Returns the GraphQL::Client::HTTP instance injecting the PulsarAuthUtil authentication Token
|
9
|
+
#
|
10
|
+
# @param [String] endpoint GraphQL API Endpoint
|
11
|
+
# @param [Hash] headers HTTP Request headers
|
12
|
+
#
|
13
|
+
def initialize(endpoint:, headers: {})
|
14
|
+
@headers = headers
|
15
|
+
super(endpoint) do
|
16
|
+
def headers(context)
|
17
|
+
@headers
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'fileutils'
|
4
|
+
require 'graphql/client'
|
5
|
+
|
6
|
+
class GraphqlUtil::Schema
|
7
|
+
#
|
8
|
+
# Initialize the GraphqlUtil::Schema to generate or retrieve the GraphQL Schema dump
|
9
|
+
#
|
10
|
+
# @param [GraphqlUtil::Http] http HTTP Client instance
|
11
|
+
# @param [String] path Path to the client Class
|
12
|
+
#
|
13
|
+
def initialize(http, path:)
|
14
|
+
@http = http
|
15
|
+
@path = path
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
# Loads the GraphQL Endpoint Introspection Schema from a dumped file if present, or dumps itself if needed
|
20
|
+
#
|
21
|
+
# @return [Class] GraphQL Schema
|
22
|
+
#
|
23
|
+
def load_schema
|
24
|
+
if !File.exist?(@path)
|
25
|
+
schema_dir = File.dirname(@path)
|
26
|
+
FileUtils.mkdir_p(schema_dir) unless File.directory?(schema_dir)
|
27
|
+
GraphQL::Client.dump_schema(@http, @path)
|
28
|
+
end
|
29
|
+
|
30
|
+
GraphQL::Client.load_schema(@path)
|
31
|
+
end
|
32
|
+
end
|
data/lib/graphql_util.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'graphql_util/version'
|
4
|
+
require 'graphql_util/client'
|
5
|
+
require 'graphql_util/http'
|
6
|
+
require 'graphql_util/schema'
|
7
|
+
|
8
|
+
module GraphqlUtil
|
9
|
+
#
|
10
|
+
# Allows a Ruby class to behave like a GraphQL Client by extending it with the required methods and constants to perform GraphQL Queries / Mutations.
|
11
|
+
#
|
12
|
+
# * Extends the class with the required methods and constants to perform GraphQL Queries / Mutations
|
13
|
+
# * Dumps the GraphQL Schema into the passed folder
|
14
|
+
# * Dynamically defines constants and methods to perform the queries defined as .graphql files under under the passed path subdirectories
|
15
|
+
#
|
16
|
+
# @param [Class] base Class
|
17
|
+
# @param [String] endpoint GraphQL API Endpoint
|
18
|
+
# @param [String] path GraphQL Schema / Queries definitions path
|
19
|
+
# @param [Hash] headers HTTP Request Headers
|
20
|
+
#
|
21
|
+
# @return [Boolean] true
|
22
|
+
#
|
23
|
+
def self.act_as_graphql_client(base, endpoint:, path:, headers: {})
|
24
|
+
raise 'GraphqlUtil - Constant GRAPHQL_UTIL_HTTP is already defined' if defined?(base::GRAPHQL_UTIL_HTTP)
|
25
|
+
raise 'GraphqlUtil - Constant GRAPHQL_UTIL_SCHEMA is already defined' if defined?(base::GRAPHQL_UTIL_SCHEMA)
|
26
|
+
raise 'GraphqlUtil - Constant GRAPHQL_UTIL_CLIENT is already defined' if defined?(base::GRAPHQL_UTIL_CLIENT)
|
27
|
+
raise 'GraphqlUtil - Constant GRAPHQL_UTIL_SCHEMA_DUMP is already defined' if defined?(base::GRAPHQL_UTIL_SCHEMA_DUMP)
|
28
|
+
|
29
|
+
base.const_set('GRAPHQL_UTIL_SCHEMA_DUMP', "#{path}/schema.json")
|
30
|
+
base.const_set('GRAPHQL_UTIL_HTTP', GraphqlUtil::Http.new(endpoint: endpoint, headers: headers))
|
31
|
+
base.const_set('GRAPHQL_UTIL_SCHEMA', GraphqlUtil::Schema.new(base::GRAPHQL_UTIL_HTTP, path: base::GRAPHQL_UTIL_SCHEMA_DUMP))
|
32
|
+
base.const_set('GRAPHQL_UTIL_CLIENT', GraphqlUtil::Client.new(schema: base::GRAPHQL_UTIL_SCHEMA.load_schema, execute: base::GRAPHQL_UTIL_HTTP))
|
33
|
+
base.extend GraphqlUtilMethods
|
34
|
+
|
35
|
+
Dir["#{path}/**/*.graphql"].each do |filename|
|
36
|
+
const_name = filename.split('/').last.gsub('.graphql', '')
|
37
|
+
raise "GraphqlUtil - #{const_name} is already defined, please use a different filename." if (base.const_get(const_name.upcase.to_sym).present? rescue false)
|
38
|
+
|
39
|
+
base.const_set(const_name.upcase, base::GRAPHQL_UTIL_CLIENT.parse(File.open(filename).read))
|
40
|
+
base.define_singleton_method(const_name.downcase.to_sym) do |variables = {}|
|
41
|
+
base.query(base.const_get(const_name.upcase.to_sym), variables: variables)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
true
|
46
|
+
end
|
47
|
+
|
48
|
+
module GraphqlUtilMethods
|
49
|
+
#
|
50
|
+
# Calls the Client query method to perform the passed GraphQL Query / Mutation request with variables
|
51
|
+
#
|
52
|
+
# @param [GraphQL::Client::OperationDefinition] query GraphQL Operation
|
53
|
+
# @param [Hash] variables Variables to be passed to the GraphQL Query / Mutation
|
54
|
+
#
|
55
|
+
# @return [GraphQL::Client::Response] Request Response
|
56
|
+
#
|
57
|
+
def query(query, variables: {})
|
58
|
+
self::GRAPHQL_UTIL_CLIENT.query(query, variables: variables)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: graphql_util
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Elisacci Lapo
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-12-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: graphql-client
|
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
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: GraphqlUtil allows you to dynamically define GraphQL Client
|
42
|
+
email:
|
43
|
+
- lapoelisacci@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".rspec"
|
49
|
+
- ".rubocop.yml"
|
50
|
+
- CHANGELOG.md
|
51
|
+
- Gemfile
|
52
|
+
- Gemfile.lock
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- bin/console
|
57
|
+
- bin/setup
|
58
|
+
- lib/graphql_util.rb
|
59
|
+
- lib/graphql_util/client.rb
|
60
|
+
- lib/graphql_util/http.rb
|
61
|
+
- lib/graphql_util/schema.rb
|
62
|
+
- lib/graphql_util/version.rb
|
63
|
+
homepage: https://github.com/LapoElisacci/graphql_util
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata:
|
67
|
+
homepage_uri: https://github.com/LapoElisacci/graphql_util
|
68
|
+
source_code_uri: https://github.com/LapoElisacci/graphql_util
|
69
|
+
changelog_uri: https://github.com/LapoElisacci/graphql_util/blob/main/CHANGELOG.md
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 2.4.0
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubygems_version: 3.1.2
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Ruby GraphQL Client utility
|
89
|
+
test_files: []
|