graphql_client 0.3.3

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.
Files changed (73) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/.rubocop-http---shopify-github-io-ruby-style-guide-rubocop-yml +1133 -0
  4. data/.rubocop.yml +24 -0
  5. data/.travis.yml +6 -0
  6. data/CHANGELOG.md +3 -0
  7. data/CODE_OF_CONDUCT.md +45 -0
  8. data/CONTRIBUTING.md +28 -0
  9. data/CONTRIBUTING_DEVELOPER_CERTIFICATE_OF_ORIGIN.txt +37 -0
  10. data/Gemfile +8 -0
  11. data/LICENSE.md +21 -0
  12. data/README.md +96 -0
  13. data/Rakefile +13 -0
  14. data/bin/graphql-client +79 -0
  15. data/bin/rake +17 -0
  16. data/circle.yml +3 -0
  17. data/dev.yml +7 -0
  18. data/graphql_ruby_client.gemspec +26 -0
  19. data/lib/graphql_client.rb +46 -0
  20. data/lib/graphql_client/adapters/http_adapter.rb +72 -0
  21. data/lib/graphql_client/base.rb +53 -0
  22. data/lib/graphql_client/config.rb +42 -0
  23. data/lib/graphql_client/deserialization.rb +36 -0
  24. data/lib/graphql_client/error.rb +22 -0
  25. data/lib/graphql_client/graph_connection.rb +21 -0
  26. data/lib/graphql_client/graph_node.rb +24 -0
  27. data/lib/graphql_client/graph_object.rb +56 -0
  28. data/lib/graphql_client/introspection_query.rb +80 -0
  29. data/lib/graphql_client/query/add_inline_fragment.rb +42 -0
  30. data/lib/graphql_client/query/argument.rb +30 -0
  31. data/lib/graphql_client/query/document.rb +86 -0
  32. data/lib/graphql_client/query/field.rb +91 -0
  33. data/lib/graphql_client/query/fragment.rb +41 -0
  34. data/lib/graphql_client/query/has_selection_set.rb +72 -0
  35. data/lib/graphql_client/query/inline_fragment.rb +35 -0
  36. data/lib/graphql_client/query/mutation_document.rb +20 -0
  37. data/lib/graphql_client/query/operation.rb +46 -0
  38. data/lib/graphql_client/query/operations/mutation_operation.rb +17 -0
  39. data/lib/graphql_client/query/operations/query_operation.rb +17 -0
  40. data/lib/graphql_client/query/query_document.rb +20 -0
  41. data/lib/graphql_client/query/selection_set.rb +53 -0
  42. data/lib/graphql_client/response.rb +21 -0
  43. data/lib/graphql_client/response_connection.rb +18 -0
  44. data/lib/graphql_client/response_object.rb +32 -0
  45. data/lib/graphql_client/schema_patches.rb +17 -0
  46. data/lib/graphql_client/version.rb +7 -0
  47. data/shipit.rubygems.yml +1 -0
  48. data/shipit.yml +4 -0
  49. data/test/graphql_client/adapters/http_adapter_test.rb +111 -0
  50. data/test/graphql_client/config_test.rb +68 -0
  51. data/test/graphql_client/graph_connection_test.rb +8 -0
  52. data/test/graphql_client/graph_node_test.rb +8 -0
  53. data/test/graphql_client/graph_object_query_transforming_test.rb +156 -0
  54. data/test/graphql_client/graph_object_test.rb +142 -0
  55. data/test/graphql_client/graphql_client_test.rb +41 -0
  56. data/test/graphql_client/http_client_test.rb +52 -0
  57. data/test/graphql_client/query/add_inline_fragment_test.rb +57 -0
  58. data/test/graphql_client/query/arguments_test.rb +89 -0
  59. data/test/graphql_client/query/document_test.rb +246 -0
  60. data/test/graphql_client/query/field_test.rb +163 -0
  61. data/test/graphql_client/query/fragment_test.rb +45 -0
  62. data/test/graphql_client/query/inline_fragment_test.rb +37 -0
  63. data/test/graphql_client/query/mutation_document_test.rb +30 -0
  64. data/test/graphql_client/query/mutation_operation_test.rb +89 -0
  65. data/test/graphql_client/query/query_document_test.rb +30 -0
  66. data/test/graphql_client/query/query_operation_test.rb +262 -0
  67. data/test/graphql_client/query/selection_set_test.rb +116 -0
  68. data/test/graphql_client/response_connection_test.rb +50 -0
  69. data/test/graphql_client/response_object_test.rb +109 -0
  70. data/test/graphql_client/response_test.rb +67 -0
  71. data/test/support/fixtures/schema.json +13710 -0
  72. data/test/test_helper.rb +37 -0
  73. metadata +227 -0
@@ -0,0 +1,24 @@
1
+ # This file strictly follows the rules defined in the Ruby style guide:
2
+ # http://shopify.github.io/ruby-style-guide/
3
+ # Before updating anything please sync-up with #ruby-style-guide on Slack.
4
+ inherit_from:
5
+ - http://shopify.github.io/ruby-style-guide/rubocop.yml
6
+
7
+ AllCops:
8
+ TargetRubyVersion: 2.3
9
+
10
+ Style/WordArray:
11
+ Enabled: false
12
+
13
+ Style/RegexpLiteral:
14
+ Exclude:
15
+ - test/**/*
16
+
17
+ Style/ClassAndModuleChildren:
18
+ Exclude:
19
+ - test/**/*
20
+
21
+ Metrics/LineLength:
22
+ Max: 120
23
+ Exclude:
24
+ - test/integration/checkout_externals_test/**/*
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ cache: bundler
3
+ sudo: false
4
+
5
+ rvm:
6
+ - 2.4.0
@@ -0,0 +1,3 @@
1
+ ## 0.3.3
2
+
3
+ This is the initial public release of the GraphQL Ruby client.
@@ -0,0 +1,45 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This Code of Conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at <opensource@shopify.com>. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the Contributor Covenant, version 1.3.0,
45
+ available from http://contributor-covenant.org/version/1/3/0/
@@ -0,0 +1,28 @@
1
+ # How to contribute
2
+ We ❤️ pull requests. If you'd like to fix a bug, contribute a feature or
3
+ just correct a typo, please feel free to do so, as long as you follow
4
+ our [Code of Conduct](https://github.com/Shopify/graphql-ruby-client/blob/master/CODE_OF_CONDUCT.md)
5
+ and the [Contributing Developer Certificate of Origin](https://github.com/Shopify/graphql-ruby-client/blob/master/CONTRIBUTING_DEVELOPER_CERTIFICATE_OF_ORIGIN.txt).
6
+
7
+ If you're thinking of adding a big new feature, consider opening an
8
+ issue first to discuss it to ensure it aligns to the direction of the
9
+ project (and potentially save yourself some time!).
10
+
11
+ ## Getting Started
12
+ To start working on the codebase, first fork the repo, then clone it:
13
+ ```
14
+ git clone git@github.com:your-username/graphql-ruby-client.git
15
+ ```
16
+ *Note: replace "your-username" with your Github handle*
17
+
18
+ Install the project's dependencies:
19
+ ```
20
+ bundle install
21
+ ```
22
+
23
+ Write some features and add the corresponding tests (or in the other order for
24
+ bonus points!) Run the tests with:
25
+
26
+ ```
27
+ bin/rake test
28
+ ```
@@ -0,0 +1,37 @@
1
+ Developer Certificate of Origin
2
+ Version 1.1
3
+
4
+ Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
5
+ 1 Letterman Drive
6
+ Suite D4700
7
+ San Francisco, CA, 94129
8
+
9
+ Everyone is permitted to copy and distribute verbatim copies of this
10
+ license document, but changing it is not allowed.
11
+
12
+
13
+ Developer's Certificate of Origin 1.1
14
+
15
+ By making a contribution to this project, I certify that:
16
+
17
+ (a) The contribution was created in whole or in part by me and I
18
+ have the right to submit it under the open source license
19
+ indicated in the file; or
20
+
21
+ (b) The contribution is based upon previous work that, to the best
22
+ of my knowledge, is covered under an appropriate open source
23
+ license and I have the right under that license to submit that
24
+ work with modifications, whether created in whole or in part
25
+ by me, under the same open source license (unless I am
26
+ permitted to submit under a different license), as indicated
27
+ in the file; or
28
+
29
+ (c) The contribution was provided directly to me by some other
30
+ person who certified (a), (b) or (c) and I have not modified
31
+ it.
32
+
33
+ (d) I understand and agree that this project and the contribution
34
+ are public and that a record of the contribution (including all
35
+ personal information I submit with it, including my sign-off) is
36
+ maintained indefinitely and may be redistributed consistent with
37
+ this project or the open source license(s) involved.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org/'
2
+
3
+ gemspec
4
+
5
+ group :deployment do
6
+ gem 'package_cloud'
7
+ gem 'rake'
8
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2016 Shopify Inc.
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
20
+ OR OTHER DEALINGS IN THE SOFTWARE.
21
+
@@ -0,0 +1,96 @@
1
+ # A Ruby GraphQL Client
2
+
3
+ [![Build Status](https://travis-ci.org/Shopify/graphql-ruby-client.svg?branch=master)](https://travis-ci.org/Shopify/graphql-ruby-client)
4
+
5
+ This is an early stage attempt at a *generic* GraphQL client in Ruby.
6
+
7
+ This client offers two APIs:
8
+
9
+ 1. Query Builder
10
+ 2. Raw Queries
11
+
12
+ The Query Builder is considered unstable and should be used with caution.
13
+
14
+ We recommend start with raw queries since it offers an easy migration path to another API or library. With the raw queries, you are just writing plain GraphQL queries as strings.
15
+
16
+ Below you'll find some usage examples.
17
+
18
+ ## Usage
19
+
20
+ Create a client:
21
+
22
+ ```ruby
23
+ client = GraphQL::Client.new(Pathname.new('path/to/schema.json')) do
24
+ configure do |c|
25
+ c.url = "https://#{shopify_domain}/admin/api/graphql.json"
26
+ c.read_timeout = 1 # 5 seconds is the default
27
+ c.headers = {
28
+ 'X-Shopify-Access-Token' => shopify_token
29
+ }
30
+ end
31
+ end
32
+ ```
33
+
34
+ ### Raw Queries
35
+
36
+ ```ruby
37
+ client.raw_query('
38
+ query {
39
+ shop {
40
+ name
41
+ }
42
+ }
43
+ ')
44
+ ```
45
+
46
+ ### Query Builder
47
+
48
+ ```ruby
49
+ query = client.build_query do |q|
50
+ q.add_field('shop') do |shop|
51
+ shop.add_field('name')
52
+ end
53
+ end
54
+
55
+ client.query(query)
56
+ ```
57
+
58
+ More complex query using a connection:
59
+
60
+ ```ruby
61
+ query = client.build_query do |q|
62
+ q.add_field('product', id: 'gid://Product/1') do |product|
63
+ product.add_connection('images', first: 10) do |connection|
64
+ connection.add_field('src')
65
+ end
66
+ end
67
+
68
+ q.add_field('shop') do |shop|
69
+ shop.add_field('name')
70
+
71
+ shop.add_field('billingAddress') do |billing_address|
72
+ billing_address.add_fields('city', 'country')
73
+ end
74
+ end
75
+ end
76
+
77
+ client.query(query)
78
+ ```
79
+
80
+ ## Responses
81
+
82
+ Both `query` and `raw_query` methods return a response object that converts the JSON to Ruby objects offering easy method access instead of via a Hash.
83
+
84
+ Example:
85
+
86
+ ```ruby
87
+ response = client.raw_query('
88
+ query {
89
+ shop {
90
+ name
91
+ }
92
+ }
93
+ ')
94
+
95
+ puts response.shop.name
96
+ ```
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.warning = false
6
+ t.libs << 'test'
7
+
8
+ t.test_files = FileList.new do |test_files|
9
+ test_files.include('test/**/*_test.rb')
10
+ end
11
+ end
12
+
13
+ task default: :test
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ Bundler.require
5
+
6
+ require 'irb'
7
+ require 'irb/completion'
8
+ require 'optparse'
9
+ require 'net/http'
10
+ require_relative '../lib/graphql_client'
11
+
12
+ JSON_MIME_TYPE = 'application/json'.freeze
13
+ DEFAULT_HEADERS = { 'Accept' => JSON_MIME_TYPE, 'Content-Type' => JSON_MIME_TYPE }
14
+
15
+ options = {}
16
+
17
+ op = OptionParser.new do |opts|
18
+ opts.banner = 'Usage: bin/graphql-client <url> [options]'
19
+
20
+ opts.on('-u', '--username [USERNAME]', 'HTTP Basic Auth Username') do |arg|
21
+ options[:username] = arg
22
+ end
23
+
24
+ opts.on('-p', '--password [PASSWORD]', 'HTTP Basic Auth Password') do |arg|
25
+ options[:password] = arg
26
+ end
27
+
28
+ opts.on('-h', '--headers [HEADERS]', Array, 'Comma separated string of headers (eg: -h"X-TOKEN=token,X-FOO=bar")') do |arg|
29
+ options[:headers] = arg.map { |header| header.split('=') }.to_h
30
+ end
31
+
32
+ opts.on('-v', '--verbose', 'Verbose mode: show debugging info') do |arg|
33
+ options[:verbose] = arg
34
+ end
35
+ end
36
+
37
+ op.parse!
38
+
39
+ if ARGV.empty?
40
+ puts op.help
41
+ exit(-1)
42
+ end
43
+
44
+ uri = URI.parse(ARGV.pop)
45
+
46
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
47
+ headers = DEFAULT_HEADERS.merge(options[:headers].to_h)
48
+
49
+ request = Net::HTTP::Post.new(uri, headers).tap do |req|
50
+ if options[:username] || options[:password]
51
+ req.basic_auth(options[:username], options[:password])
52
+ end
53
+
54
+ req.body = { query: GraphQL::Client::INTROSPECTION_QUERY }.to_json
55
+ end
56
+
57
+ http.request(request)
58
+ end
59
+
60
+ case response
61
+ when Net::HTTPOK then
62
+ schema = JSON.parse(response.body)
63
+ config = GraphQL::Client::Config.new(options)
64
+ else
65
+ abort("Error fetching the schema: server responded with #{response.code}")
66
+ end
67
+
68
+ client = GraphQL::Client.new(schema, config: config) do
69
+ configure do |c|
70
+ c.url = uri
71
+ end
72
+ end
73
+
74
+ IRB.setup nil
75
+ IRB.conf[:AUTO_INDENT] = true
76
+ IRB.conf[:MAIN_CONTEXT] = IRB::Irb.new.context
77
+
78
+ require 'irb/ext/multi-irb'
79
+ IRB.irb nil, client
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rake' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("rake", "rake")
@@ -0,0 +1,3 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.3.1
data/dev.yml ADDED
@@ -0,0 +1,7 @@
1
+ name: graphql-ruby-client
2
+ up:
3
+ - ruby: 2.4.0
4
+ - bundler
5
+ commands:
6
+ test: 'bin/rake test'
7
+ build: 'gem build graphql_ruby_client.gemspec'
@@ -0,0 +1,26 @@
1
+ require_relative 'lib/graphql_client/version'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.author = 'Shopify'
5
+ s.description = ''
6
+ s.email = 'developers@jadedpixel.com'
7
+ s.files = `git ls-files`.split("\n")
8
+ s.homepage = 'http://www.shopify.com/partners/apps'
9
+ s.license = 'MIT'
10
+ s.name = 'graphql_client'
11
+ s.required_ruby_version = '>= 2.3'
12
+ s.summary = ''
13
+ s.test_files = `git ls-files -- {test}/*`.split("\n")
14
+ s.version = GraphQL::Client::VERSION
15
+ s.executables << 'graphql-client'
16
+
17
+ s.add_runtime_dependency 'graphql_schema', '~> 0.1.5'
18
+
19
+ s.add_development_dependency 'graphql', '~> 1.6.4'
20
+ s.add_development_dependency 'minitest', '~> 5.8'
21
+ s.add_development_dependency 'pry'
22
+ s.add_development_dependency 'rake'
23
+ s.add_development_dependency 'rubocop'
24
+ s.add_development_dependency 'simplecov'
25
+ s.add_development_dependency 'webmock', '~> 3.1.1'
26
+ end
@@ -0,0 +1,46 @@
1
+ require 'bundler/setup'
2
+ require 'json'
3
+ require 'graphql_schema'
4
+
5
+ require 'graphql_client/schema_patches.rb'
6
+ require 'graphql_client/error.rb'
7
+ require 'graphql_client/config.rb'
8
+ require 'graphql_client/adapters/http_adapter.rb'
9
+ require 'graphql_client/base.rb'
10
+ require 'graphql_client/query/has_selection_set.rb'
11
+ require 'graphql_client/query/add_inline_fragment.rb'
12
+ require 'graphql_client/query/selection_set.rb'
13
+ require 'graphql_client/query/fragment.rb'
14
+ require 'graphql_client/query/inline_fragment.rb'
15
+ require 'graphql_client/query/argument.rb'
16
+ require 'graphql_client/query/field.rb'
17
+ require 'graphql_client/query/query_document.rb'
18
+ require 'graphql_client/query/mutation_document.rb'
19
+ require 'graphql_client/query/document.rb'
20
+ require 'graphql_client/query/operation.rb'
21
+ require 'graphql_client/query/operations/query_operation.rb'
22
+ require 'graphql_client/query/operations/mutation_operation.rb'
23
+ require 'graphql_client/response.rb'
24
+ require 'graphql_client/deserialization.rb'
25
+ require 'graphql_client/response_object.rb'
26
+ require 'graphql_client/response_connection.rb'
27
+ require 'graphql_client/graph_object.rb'
28
+ require 'graphql_client/graph_connection.rb'
29
+ require 'graphql_client/graph_node.rb'
30
+ require 'graphql_client/introspection_query.rb'
31
+
32
+ module GraphQL
33
+ module Client
34
+ class << self
35
+ def dump_schema(file, config: Config.new, adapter: nil)
36
+ adapter ||= Adapters::HTTPAdapter.new(config)
37
+ response = adapter.request(INTROSPECTION_QUERY)
38
+ IO.write(file, JSON.pretty_generate(response.body))
39
+ end
40
+
41
+ def new(schema, config: nil, adapter: nil, &block)
42
+ Base.new(schema, config: config, adapter: adapter, &block)
43
+ end
44
+ end
45
+ end
46
+ end