artemis 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +36 -0
  5. data/Appraisals +32 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +10 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +181 -0
  10. data/Rakefile +14 -0
  11. data/artemis.gemspec +28 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/gemfiles/.bundle/config +2 -0
  15. data/gemfiles/rails_42.gemfile +12 -0
  16. data/gemfiles/rails_50.gemfile +11 -0
  17. data/gemfiles/rails_51.gemfile +11 -0
  18. data/gemfiles/rails_52.gemfile +11 -0
  19. data/gemfiles/rails_edge.gemfile +14 -0
  20. data/lib/artemis.rb +3 -0
  21. data/lib/artemis/adapters.rb +25 -0
  22. data/lib/artemis/adapters/abstract_adapter.rb +46 -0
  23. data/lib/artemis/adapters/curb_adapter.rb +56 -0
  24. data/lib/artemis/adapters/net_http_adapter.rb +51 -0
  25. data/lib/artemis/adapters/net_http_persistent_adapter.rb +46 -0
  26. data/lib/artemis/adapters/test_adapter.rb +29 -0
  27. data/lib/artemis/client.rb +245 -0
  28. data/lib/artemis/exceptions.rb +19 -0
  29. data/lib/artemis/graphql_endpoint.rb +54 -0
  30. data/lib/artemis/railtie.rb +50 -0
  31. data/lib/artemis/version.rb +3 -0
  32. data/lib/generators/artemis/USAGE +11 -0
  33. data/lib/generators/artemis/install_generator.rb +59 -0
  34. data/lib/generators/artemis/templates/.gitkeep +0 -0
  35. data/lib/generators/artemis/templates/client.rb +10 -0
  36. data/lib/generators/artemis/templates/graphql.yml +19 -0
  37. data/lib/tasks/artemis.rake +47 -0
  38. data/spec/adapters_spec.rb +106 -0
  39. data/spec/autoloading_spec.rb +146 -0
  40. data/spec/callbacks_spec.rb +46 -0
  41. data/spec/client_spec.rb +161 -0
  42. data/spec/endpoint_spec.rb +45 -0
  43. data/spec/fixtures/metaphysics.rb +2 -0
  44. data/spec/fixtures/metaphysics/_artist_fragment.graphql +4 -0
  45. data/spec/fixtures/metaphysics/artist.graphql +7 -0
  46. data/spec/fixtures/metaphysics/artists.graphql +8 -0
  47. data/spec/fixtures/metaphysics/artwork.graphql +8 -0
  48. data/spec/fixtures/metaphysics/schema.json +49811 -0
  49. data/spec/spec_helper.rb +48 -0
  50. metadata +219 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c44b0b7dd0ed93d3c8c64f8a63dbaae919c90a04eee6a476d83add0253ffbb39
4
+ data.tar.gz: f220a99f21d8bc7f24f1ae2dd1140f975707c6e26f74951e703a34edcdbd93c0
5
+ SHA512:
6
+ metadata.gz: fc1ec085d1a695a683268067a5df8c63c8bc4aa5033527d458fc4cf5071e29bfc432ae8badeb132127d27703c5df384e5be8e2893c3335a9252d8a36abb193a0
7
+ data.tar.gz: ca037098da82ff0dbd97f0232319ecebb75714c455dc7ca0b59ea666ba465b3ccf9641b97e1f4390e29ba0a8e1c9f44ad3faa78df82293e68967deedfb063d54
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+ gemfiles/*.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --colour
@@ -0,0 +1,36 @@
1
+ nguage: ruby
2
+ script: bundle exec rake
3
+ cache: bundler
4
+ sudo: false
5
+
6
+ before_install: gem install bundler -v 1.16.1
7
+
8
+ rvm:
9
+ - 2.5.1
10
+ - 2.4.4
11
+ - 2.3.7
12
+ # - 2.2.10
13
+ - ruby-head
14
+ # - jruby-9.2.0.0
15
+ # - jruby-head
16
+
17
+ gemfile:
18
+ - gemfiles/rails_52.gemfile
19
+ - gemfiles/rails_51.gemfile
20
+ - gemfiles/rails_50.gemfile
21
+ - gemfiles/rails_42.gemfile
22
+ - gemfiles/rails_edge.gemfile
23
+
24
+ matrix:
25
+ allow_failures:
26
+ - rvm: ruby-head
27
+ # - rvm: jruby-9.2.0.0
28
+ # - rvm: jruby-head
29
+ - gemfile: gemfiles/rails_edge.gemfile
30
+ exclude:
31
+ - rvm: 2.3.7
32
+ gemfile: gemfiles/rails_edge.gemfile
33
+ # - rvm: 2.2.10
34
+ # gemfile: gemfiles/rails_52.gemfile
35
+ # - rvm: 2.2.10
36
+ # gemfile: gemfiles/rails_edge.gemfile
@@ -0,0 +1,32 @@
1
+ appraise "rails_edge" do
2
+ git 'git://github.com/rails/rails.git' do
3
+ gem "rails"
4
+ gem "railties"
5
+ gem "activesupport"
6
+ end
7
+ end
8
+
9
+ appraise "rails_52" do
10
+ gem "rails", '~> 5.2'
11
+ gem "railties", '~> 5.2'
12
+ gem "activesupport", '~> 5.2'
13
+ end
14
+
15
+ appraise "rails_51" do
16
+ gem "rails", '~> 5.1'
17
+ gem "railties", '~> 5.1'
18
+ gem "activesupport", '~> 5.1'
19
+ end
20
+
21
+ appraise "rails_50" do
22
+ gem "rails", '~> 5.0'
23
+ gem "railties", '~> 5.0'
24
+ gem "activesupport", '~> 5.0'
25
+ end
26
+
27
+ appraise "rails_42" do
28
+ gem "rails", '~> 4.2'
29
+ gem "railties", '~> 4.2'
30
+ gem "activesupport", '~> 4.2'
31
+ gem "minitest", '5.10.3'
32
+ end
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at yk.nishijima@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in artemis.gemspec
6
+ gemspec
7
+
8
+ gem 'rails'
9
+ gem 'pry'
10
+ gem 'pry-byebug', platforms: :mri
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Yuki Nishijima
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.
@@ -0,0 +1,181 @@
1
+ # Artemis [![Build Status](https://travis-ci.org/yuki24/artemis.svg?branch=master)](https://travis-ci.org/yuki24/artemis)
2
+
3
+ * **Convention over Configuration**: You'll never have to make trivial decisions or spend time on boring setup. Start
4
+ making a GraphQL request in literally 30sec.
5
+ * **Performant by default**: You can't do wrong when it comes to performance. All GraphQL files are pre-loaded only
6
+ once in production and it'll never affect runtime performance. Comes with options that enable persistent connections
7
+ and even HTTP/2.0, the next-gen high-performance protocol.
8
+
9
+ ### Getting started
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'artemis'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Once you run `bundle install` on your Rails app, you will be able to run the following command:
22
+
23
+
24
+ ```sh
25
+ $ rails g artemis:install artsy https://metaphysics-production.artsy.net/
26
+ ```
27
+
28
+ It is common that a GraphQL server requires an OAuth access token. If it is the case, use the `--authorization` option
29
+ to assign a token so the installer can properly download the GraphQL schema for the service:
30
+
31
+ ```sh
32
+ $ rails g artemis:install github https://api.github.com/graphql --authorization 'token ...'
33
+ ```
34
+
35
+ ## The convention
36
+
37
+ Artemis assumes that the files related to GraphQL are organized with the following structure:
38
+
39
+ ```
40
+ ├──app/operations
41
+ │   ├── artsy
42
+ │   │   ├── _artist_fragment.graphql
43
+ │   │   ├── artwork.graphql
44
+ │   │   ├── artist.graphql
45
+ │   │   └── artists.graphql
46
+ │   └── artsy.rb
47
+ ├──config/graphql.yml
48
+ └──vendor/graphql/schema/artsy.json
49
+ ```
50
+
51
+ ## Examples
52
+
53
+ ```yml
54
+ # config/graphql.yml
55
+ development:
56
+ artsy:
57
+ url: https://metaphysics-production.artsy.net/
58
+ ```
59
+
60
+ ```ruby
61
+ # app/queries/artsy.rb
62
+ class Artsy < Artemis::Client
63
+ end
64
+ ```
65
+
66
+ ```graphql
67
+ # app/queries/artsy/artwork.graphql
68
+ query($id: String!) {
69
+ artwork(id: $id) {
70
+ title
71
+ }
72
+ }
73
+
74
+ # app/queries/artsy/me.graphql
75
+ query {
76
+ me {
77
+ name
78
+ }
79
+ }
80
+ ```
81
+
82
+ ```ruby
83
+ results = Artsy.artwork(id: "andy-warhol-john-wayne-1986-number-377-cowboys-and-indians")
84
+ results.data
85
+ # => {
86
+ # "data": {
87
+ # "artwork": {
88
+ # "title": "John Wayne, 1986 (#377, Cowboys & Indians)"
89
+ # }
90
+ # }
91
+ # }
92
+
93
+ results = Artsy.with_context(headers: { "X-ACCESS-TOKEN": "..." }).me
94
+ results.data
95
+ # => {
96
+ # "data": {
97
+ # "me": {
98
+ # "name": "Yuki Nishijima"
99
+ # }
100
+ # }
101
+ # }
102
+ ```
103
+
104
+ ## Callbacks
105
+
106
+ Youcan use the `before_execute` callback to intercept outgoing requests and the `after_execute` callback to observe the
107
+ response. A common operation that's done in the `before_execute` hook is assigning a token to the header:
108
+
109
+ ```ruby
110
+ class Artsy < Artemis::Client
111
+ before_execute do |document, operation_name, variables, context|
112
+ context[:headers] = {
113
+ Authorization: "token ..."
114
+ }
115
+ end
116
+ end
117
+ ```
118
+
119
+ Here the `:headers` key is a special context type. The hash object assigned to the `context[:headers]` will be sent as
120
+ the HTTP headers of the request.
121
+
122
+ Another common thing when receiving a response is to check if there's any error in the response and throw and error
123
+ accordingly:
124
+
125
+ ```ruby
126
+ class Artsy < Artemis::Client
127
+ after_execute do |data, errors, extensions|
128
+ raise "GraphQL error: #{errors.to_json}" if errors.present?
129
+ end
130
+ end
131
+ ```
132
+
133
+ ## Configuration
134
+
135
+ You can configure the GraphQL client using the following options. Those configurations are found in the
136
+ `config/graphql.yml`.
137
+
138
+ | Name | Required? | Default | Description |
139
+ | ------------- | --------- | ------------| ----------- |
140
+ | `adapter` | No | `:net_http` | The underlying client library that actually makes an HTTP request. See Adapters for available options.
141
+ | `pool_size` | No | 25 | The number of keep-alive connections. The `:net_http` adapter will ignore this option.
142
+ | `schema_path` | No | See above | The path to the GrapQL schema. Setting an empty value to this will force the client to download the schema upon the first request.
143
+ | `timeout` | No | 10 | HTTP timeout set for the adapter in seconds. This will be set to both `read_timeout` and `write_timeout` and there is no way to configure them with a different value as of writing (PRs welcome!)
144
+ | `url` | Yes | N/A | The URL for the GraphQL endpoint.
145
+
146
+ ### Adapters
147
+
148
+ There are four adapter options available. Choose the adapter that best fits on your use case.
149
+
150
+ | Adapter | Protocol | Keep-alive | Performance | Dependencies |
151
+ | ---------------------- | ------------------------ | ----------- | ----------- | ------------ |
152
+ | `:curb` | HTTP/1.1, **HTTP/2.0** | **Yes** | **Fastest** | [`curb 0.9.6+`](curb)<br>[`libcurl 7.64.0+`](curl)<br>[`nghttp2 1.0.0+`](nghttp)
153
+ | `:net_http` (default) | HTTP/1.1 only | No | Slow | **None**
154
+ | `:net_http_persistent` | HTTP/1.1 only | **Yes** | **Fast** | [`net-http-persistent 3.0.0+`](nhp)
155
+ | `:test` | N/A (See Testing)
156
+
157
+ ## Testing
158
+
159
+ **The testing support is incomplete, but there are some examples [available in Artemis' client spec](https://github.com/yuki24/artemis/blob/74095f3acb050e87251439aed5f8b17778ffdd06/spec/client_spec.rb#L36-L54).**
160
+
161
+ ## Development
162
+
163
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
164
+
165
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
166
+
167
+ ## Contributing
168
+
169
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yuki24/artemis. 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.
170
+
171
+ ## License
172
+
173
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
174
+
175
+ ## Code of Conduct
176
+
177
+ Everyone interacting in the Artemis project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/artemis/blob/master/CODE_OF_CONDUCT.md).
178
+
179
+ [curl]: https://curl.haxx.se/docs/http2.html
180
+ [nghttp]: https://nghttp2.org/
181
+ [nhp]: https://rubygems.org/gems/net-http-persistent
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require 'rspec/core/rake_task'
4
+
5
+ Rake::TestTask.new('test:isolated') do |t|
6
+ t.libs << "test"
7
+ t.libs << "lib"
8
+ t.test_files = FileList['test/**/*_test.rb']
9
+ t.warning = false
10
+ end
11
+
12
+ RSpec::Core::RakeTask.new(:spec)
13
+
14
+ task default: ['spec', 'test:isolated']
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "artemis/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "artemis"
8
+ spec.version = Artemis::VERSION
9
+ spec.authors = ["Yuki Nishijima"]
10
+ spec.email = ["yk.nishijima@gmail.com"]
11
+ spec.summary = %q{GraphQL on Rails}
12
+ spec.description = %q{GraphQL client on Rails + Convention over Configuration = ❤️}
13
+ spec.homepage = "https://github.com/yuki24/artemis"
14
+ spec.license = "MIT"
15
+ spec.files = `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test)/}) }
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_dependency "activesupport", ">= 4.2.0"
19
+ spec.add_dependency "graphql-client", ">= 0.13.0"
20
+ spec.add_dependency "railties", ">= 4.2.0"
21
+
22
+ spec.add_development_dependency "appraisal", "~> 2.2"
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "curb", "~> 0.9.6"
25
+ spec.add_development_dependency "net-http-persistent", "~> 3.0"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.8"
28
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "artemis"
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__)
@@ -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