graphql_schema 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5baf3ee59c551fd336943f2729bab7711042b8ad
4
+ data.tar.gz: 077522f2b78fde33cf8f4b212c75501ddc8c259e
5
+ SHA512:
6
+ metadata.gz: 24573fc0b1815b5afcebf6d71895f112f8fbd20919dd195940223187eecb453f0d801a09e1cf6d1e9074e94aa9f0b13408c08979e184d8b0b384067c0230accf
7
+ data.tar.gz: a291436003bf8001294dc41056406763cbdb3a2b2da25cde900fbb03d3dde7eee15b3695fcf64d385e74964f4681be47d774f3bca3cf949955a342bd40a210df
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /*.gem
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.3
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,25 @@
1
+ # Contributing
2
+
3
+ Types of contributions we welcome:
4
+
5
+ * Opening issue for bugs
6
+ * Bug fixes
7
+ * Documentation and/or clearer interfaces
8
+
9
+ ## Proposing Features
10
+
11
+ The focus on this project is on providing a convenient way to access
12
+ the schema for code generators targetting different langauges.
13
+ Features that aren't relevant for this use case are less likely to
14
+ be merged. When in doubt, open an issue first to propose the issue
15
+ so that we can confirm that we are interested in a pull request to
16
+ implement it.
17
+
18
+ ## How To Contribute
19
+
20
+ 1. Fork the [repository in github](https://github.com/Shopify/graphql_schema)
21
+ 2. Create your feature branch (`git checkout -b fix-feature`)
22
+ 3. Commit your changes (`git commit -am 'fix: Summarize change'`)
23
+ 3. Make sure all tests pass (`bundle exec rake`)
24
+ 4. Push to the branch (`git push origin fix-feature`)
25
+ 5. [Create new pull request](https://github.com/Shopify/graphql_schema/pulls)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Shopify
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,50 @@
1
+ # GraphQL Schema
2
+
3
+ Classes to more conveniently access the GraphQL instrospection
4
+ result rather than using the parsed json directly as a hash.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'graphql_schema'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install graphql_schema
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'graphql_schema'
26
+ require 'json'
27
+
28
+ introspection_result = JSON.parse(File.read("schema.json"))
29
+ schema = GraphQLSchema.new(introspection_result)
30
+ schema.types.select(&:object_type?).each do |type|
31
+ type.fields.each do |field|
32
+ puts "#{field.name} -> #{field.type.name}"
33
+ end
34
+ end
35
+ ```
36
+
37
+ ## Development
38
+
39
+ 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.
40
+
41
+ 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).
42
+
43
+ ## Contributing
44
+
45
+ See our [contributing guidelines](CONTRIBUTING.md) for more information.
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
50
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "graphql_schema"
5
+
6
+ require "irb"
7
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'graphql_schema/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "graphql_schema"
8
+ spec.version = GraphQLSchema::VERSION
9
+ spec.authors = ["Dylan Thacker-Smith"]
10
+ spec.email = ["gems@shopify.com"]
11
+
12
+ spec.summary = "Classes for convenient use of GraphQL introspection result"
13
+ spec.homepage = "https://github.com/Shopify/graphql_schema"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "graphql", "~> 1.2"
24
+ spec.add_development_dependency "byebug", '~> 9.0' if RUBY_ENGINE == 'ruby'
25
+ spec.add_development_dependency "bundler", "~> 1.13"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ end
@@ -0,0 +1,3 @@
1
+ class GraphQLSchema
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,222 @@
1
+ require "graphql_schema/version"
2
+ require 'set'
3
+
4
+ class GraphQLSchema
5
+ def initialize(instrospection_result)
6
+ @hash = instrospection_result.fetch('data').fetch('__schema')
7
+ end
8
+
9
+ def root_name?(type_name)
10
+ type_name == query_root_name || type_name == mutation_root_name
11
+ end
12
+
13
+ def query_root_name
14
+ @query_root_name ||= @hash.fetch('queryType').fetch('name')
15
+ end
16
+
17
+ def mutation_root_name
18
+ @mutation_root_name ||= @hash.fetch('mutationType', {}).fetch('name')
19
+ end
20
+
21
+ def types
22
+ @types ||= @hash.fetch('types').map{ |type_hash| TypeDefinition.new(type_hash) }.sort_by(&:name)
23
+ end
24
+
25
+ module NamedHash
26
+ def name
27
+ @hash.fetch('name')
28
+ end
29
+
30
+ def camelize_name
31
+ @camelize_name ||= begin
32
+ words = split_name.map(&:capitalize)
33
+ words[0] = words[0].downcase
34
+ words.join
35
+ end
36
+ end
37
+
38
+ def classify_name
39
+ @classify_name ||= split_name.map(&:capitalize).join
40
+ end
41
+
42
+ def upcase_name
43
+ @upcase_name ||= split_name.join("_").upcase
44
+ end
45
+
46
+ private
47
+
48
+ def split_name
49
+ @split_name ||= name.gsub(/([a-z])([A-Z0-9])/) { "#{$1}_#{$2}" }.split("_")
50
+ end
51
+ end
52
+
53
+ module Deprecatable
54
+ def deprecated?
55
+ @hash.fetch('isDeprecated')
56
+ end
57
+
58
+ def deprecation_reason
59
+ @hash.fetch('deprecationReason')
60
+ end
61
+ end
62
+
63
+ class Argument
64
+ include NamedHash
65
+
66
+ def initialize(arg_hash)
67
+ @hash = arg_hash
68
+ end
69
+
70
+ def type
71
+ @type ||= TypeDeclaration.new(@hash.fetch('type'))
72
+ end
73
+ end
74
+
75
+ class Field
76
+ include NamedHash
77
+ include Deprecatable
78
+
79
+ def initialize(field_hash)
80
+ @hash = field_hash
81
+ end
82
+
83
+ def args
84
+ @args ||= @hash.fetch('args').map{ |arg_hash| Argument.new(arg_hash) }
85
+ end
86
+
87
+ def required_args
88
+ @required_args ||= args.select{ |arg| arg.type.non_null? }
89
+ end
90
+
91
+ def optional_args
92
+ @optional_args ||= args.reject{ |arg| arg.type.non_null? }
93
+ end
94
+
95
+ def type
96
+ @type ||= TypeDeclaration.new(@hash.fetch('type'))
97
+ end
98
+
99
+ def subfields?
100
+ type.subfields?
101
+ end
102
+ end
103
+
104
+ class EnumValue
105
+ include NamedHash
106
+ include Deprecatable
107
+
108
+ def initialize(enum_value_hash)
109
+ @hash = enum_value_hash
110
+ end
111
+ end
112
+
113
+ class Type
114
+ include NamedHash
115
+
116
+ BUILTIN = %w(Int Float String Boolean ID).to_set
117
+
118
+ def initialize(type_hash)
119
+ @hash = type_hash
120
+ end
121
+
122
+ def kind
123
+ @hash.fetch('kind')
124
+ end
125
+
126
+ def scalar?
127
+ kind == 'SCALAR'
128
+ end
129
+
130
+ def object?
131
+ kind == 'OBJECT'
132
+ end
133
+
134
+ def input_object?
135
+ kind == 'INPUT_OBJECT'
136
+ end
137
+
138
+ def interface?
139
+ kind == 'INTERFACE'
140
+ end
141
+
142
+ def union?
143
+ kind == 'UNION'
144
+ end
145
+
146
+ def list?
147
+ kind == 'LIST'
148
+ end
149
+
150
+ def builtin?
151
+ name.start_with?("__") || BUILTIN.include?(name)
152
+ end
153
+ end
154
+
155
+ class TypeDeclaration < Type
156
+ def of_type
157
+ @of_type ||= TypeDeclaration.new(@hash.fetch('ofType'))
158
+ end
159
+
160
+ def non_null?
161
+ kind == 'NON_NULL'
162
+ end
163
+
164
+ def unwrap
165
+ case kind
166
+ when 'NON_NULL', 'LIST'
167
+ of_type.unwrap
168
+ else
169
+ self
170
+ end
171
+ end
172
+
173
+ def unwrap_non_null
174
+ non_null? ? of_type.unwrap_non_null : self
175
+ end
176
+
177
+ def subfields?
178
+ case unwrap.kind
179
+ when 'OBJECT', 'INTERFACE', 'UNION'
180
+ true
181
+ else
182
+ false
183
+ end
184
+ end
185
+ end
186
+
187
+ class TypeDefinition < Type
188
+ def fields(include_deprecated: false)
189
+ @fields ||= @hash.fetch('fields').map{ |field_hash| Field.new(field_hash) }
190
+ include_deprecated ? @fields : @fields.reject(&:deprecated?)
191
+ end
192
+
193
+ def input_fields
194
+ @input_fields ||= @hash.fetch('inputFields').map{ |field_hash| Field.new(field_hash) }
195
+ end
196
+
197
+ def required_input_fields
198
+ @required_fields ||= input_fields.select{ |field| field.type.non_null? }
199
+ end
200
+
201
+ def optional_input_fields
202
+ @optional_fields ||= input_fields.reject{ |field| field.type.non_null? }
203
+ end
204
+
205
+ def interfaces
206
+ @interfaces ||= @hash.fetch('interfaces').map{ |type_hash| TypeDeclaration.new(type_hash) }.sort_by(&:name)
207
+ end
208
+
209
+ def implement?(interface_name)
210
+ interfaces.map(&:name).include?(interface_name)
211
+ end
212
+
213
+ def possible_types
214
+ @possible_types ||= @hash.fetch('possibleTypes').map{ |type_hash| TypeDeclaration.new(type_hash) }.sort_by(&:name)
215
+ end
216
+
217
+ def enum_values(include_deprecated: false)
218
+ @enum_values ||= @hash.fetch('enumValues').map{ |value_hash| EnumValue.new(value_hash) }.sort_by(&:name)
219
+ include_deprecated ? @enum_values : @enum_values.reject(&:deprecated?)
220
+ end
221
+ end
222
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphql_schema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dylan Thacker-Smith
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: graphql
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '9.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '9.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description:
84
+ email:
85
+ - gems@shopify.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - CONTRIBUTING.md
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - graphql_schema.gemspec
100
+ - lib/graphql_schema.rb
101
+ - lib/graphql_schema/version.rb
102
+ homepage: https://github.com/Shopify/graphql_schema
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.5.1
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Classes for convenient use of GraphQL introspection result
126
+ test_files: []