better-graphql-detect-invalid-id-field 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/.editorconfig +9 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/.travis.yml +7 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +61 -0
- data/LICENSE.txt +21 -0
- data/README.md +50 -0
- data/Rakefile +7 -0
- data/better-graphql-detect-invalid-id-field.gemspec +41 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/better_graphql.rb +6 -0
- data/lib/better_graphql/detect_invalid_id_field.rb +83 -0
- data/lib/better_graphql/version.rb +4 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5e5b5448ac015f65f160b4f2fe10c11bea05999b62418616a31bdb44aff293da
|
4
|
+
data.tar.gz: 30ab5b7c59e7db294e9b23668a8cb3e6ebb0d9fd81f62fba85326f9350232388
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f10d9a316501b99e5e8b4cfcfb32ed1951ec9ed0367069462584d05776ccecb9a1982105804ed39e83ef2cf6d65e462c51be06f8214652763a04e3e966b972b2
|
7
|
+
data.tar.gz: 907b60adac34c295fd925171044d146c6e295b375e79845af725c8f2f9f477089c3ca1b2463fda7fa0e3bd2d5a7f7c1b869fb971fad7bcbddde77cce8151c803
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
better-graphql-detect-invalid-id-field (0.1.0)
|
5
|
+
graphql (>= 1.11)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.1)
|
11
|
+
diff-lcs (1.4.4)
|
12
|
+
graphql (1.11.6)
|
13
|
+
parallel (1.20.1)
|
14
|
+
parser (2.7.2.0)
|
15
|
+
ast (~> 2.4.1)
|
16
|
+
rainbow (3.0.0)
|
17
|
+
rake (10.5.0)
|
18
|
+
regexp_parser (2.0.0)
|
19
|
+
rexml (3.2.4)
|
20
|
+
rspec (3.10.0)
|
21
|
+
rspec-core (~> 3.10.0)
|
22
|
+
rspec-expectations (~> 3.10.0)
|
23
|
+
rspec-mocks (~> 3.10.0)
|
24
|
+
rspec-core (3.10.0)
|
25
|
+
rspec-support (~> 3.10.0)
|
26
|
+
rspec-expectations (3.10.0)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.10.0)
|
29
|
+
rspec-mocks (3.10.0)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.10.0)
|
32
|
+
rspec-support (3.10.0)
|
33
|
+
rubocop (1.6.1)
|
34
|
+
parallel (~> 1.10)
|
35
|
+
parser (>= 2.7.1.5)
|
36
|
+
rainbow (>= 2.2.2, < 4.0)
|
37
|
+
regexp_parser (>= 1.8, < 3.0)
|
38
|
+
rexml
|
39
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
40
|
+
ruby-progressbar (~> 1.7)
|
41
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
42
|
+
rubocop-ast (1.3.0)
|
43
|
+
parser (>= 2.7.1.5)
|
44
|
+
rubocop-shopify (1.0.7)
|
45
|
+
rubocop (~> 1.4)
|
46
|
+
ruby-progressbar (1.10.1)
|
47
|
+
unicode-display_width (1.7.0)
|
48
|
+
|
49
|
+
PLATFORMS
|
50
|
+
ruby
|
51
|
+
|
52
|
+
DEPENDENCIES
|
53
|
+
better-graphql-detect-invalid-id-field!
|
54
|
+
bundler (~> 1.17)
|
55
|
+
rake (~> 10.0)
|
56
|
+
rspec (~> 3.0)
|
57
|
+
rubocop (~> 1.4)
|
58
|
+
rubocop-shopify (~> 1.0.7)
|
59
|
+
|
60
|
+
BUNDLED WITH
|
61
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 gtkatakura
|
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
|
+
[![Build Status](https://travis-ci.com/gtkatakura/better-graphql-detect-invalid-id-field.svg?branch=master)](https://travis-ci.com/gtkatakura/better-graphql-detect-invalid-id-field)
|
2
|
+
|
3
|
+
# BetterGraphQL::DetectInvalidIdField
|
4
|
+
|
5
|
+
Detect invalid id field for GraphQL types by detecting divergent fields between nodes representing the same object.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'better-graphql-detect-invalid-id-field'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install better-graphql-detect-invalid-id-field
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Add to your schema:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class YourSchema < GraphQL::Schema
|
29
|
+
query(QueryType)
|
30
|
+
|
31
|
+
if Rails.env.test? || Rails.env.development?
|
32
|
+
require 'better_graphql/detect_invalid_id_field'
|
33
|
+
use ::BetterGraphQL::DetectInvalidIdField
|
34
|
+
end
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
## Development
|
39
|
+
|
40
|
+
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.
|
41
|
+
|
42
|
+
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).
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/gtkatakura/better-graphql-detect-invalid-id-field.
|
47
|
+
|
48
|
+
## License
|
49
|
+
|
50
|
+
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,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative 'lib/better_graphql/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = 'better-graphql-detect-invalid-id-field'
|
6
|
+
spec.version = BetterGraphQL::VERSION
|
7
|
+
spec.authors = ['gtkatakura']
|
8
|
+
spec.email = ['gt.katakura@gmail.com']
|
9
|
+
|
10
|
+
spec.summary = 'Detect invalid id field for GraphQL types'
|
11
|
+
spec.description = 'Detect invalid id field for GraphQL types by detecting divergent fields between nodes representing the same object'
|
12
|
+
spec.homepage = 'http://github.com/gtkatakura/better-graphql-detect-invalid-id-field'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
if spec.respond_to?(:metadata)
|
16
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = 'http://github.com/gtkatakura/better-graphql-detect-invalid-id-field'
|
19
|
+
spec.metadata['changelog_uri'] = 'http://github.com/gtkatakura/better-graphql-detect-invalid-id-field/blob/main/CHANGELOG.md'
|
20
|
+
else
|
21
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
22
|
+
end
|
23
|
+
|
24
|
+
# Specify which files should be added to the gem when it is released.
|
25
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
26
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
27
|
+
%x(git ls-files -z).split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
28
|
+
end
|
29
|
+
|
30
|
+
spec.bindir = 'exe'
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ['lib']
|
33
|
+
|
34
|
+
spec.add_development_dependency('bundler', '~> 1.17')
|
35
|
+
spec.add_development_dependency('rake', '~> 10.0')
|
36
|
+
spec.add_development_dependency('rspec', '~> 3.0')
|
37
|
+
spec.add_development_dependency('rubocop', '~> 1.4')
|
38
|
+
spec.add_development_dependency('rubocop-shopify', '~> 1.0.7')
|
39
|
+
|
40
|
+
spec.add_runtime_dependency('graphql', '>= 1.11')
|
41
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'better_graphql/detect_invalid_id_field'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module BetterGraphQL
|
3
|
+
class DetectInvalidIdField
|
4
|
+
def self.use(schema)
|
5
|
+
tracer = new
|
6
|
+
schema.tracer(tracer)
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@cache = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def trace(key, data)
|
14
|
+
@cache = {} if key == 'parse'
|
15
|
+
|
16
|
+
return yield if key != 'execute_field'
|
17
|
+
|
18
|
+
yield.tap do |resolved_value|
|
19
|
+
next unless comparable?(resolved_value)
|
20
|
+
|
21
|
+
if data[:context]
|
22
|
+
parent_type = data[:context].parent_type
|
23
|
+
field = data[:context].field
|
24
|
+
path = data[:context].path
|
25
|
+
else
|
26
|
+
parent_type = data[:owner]
|
27
|
+
field = data[:field]
|
28
|
+
path = data[:path]
|
29
|
+
end
|
30
|
+
|
31
|
+
cache_by_type = cache[parent_type] ||= {}
|
32
|
+
nodes_by_path = cache_by_type[:nodes_by_path] ||= {}
|
33
|
+
nodes_by_id = cache_by_type[:nodes_by_id] ||= {}
|
34
|
+
|
35
|
+
field_name = field.name.to_sym
|
36
|
+
parent_path = path[0..-2]
|
37
|
+
|
38
|
+
resolving_object = nodes_by_path[parent_path] ||= {
|
39
|
+
__metadata__: { parent_path: parent_path },
|
40
|
+
}
|
41
|
+
|
42
|
+
resolving_object[field_name] = resolved_value
|
43
|
+
|
44
|
+
if field_name == :id
|
45
|
+
nodes_by_id[resolved_value] ||= []
|
46
|
+
nodes_by_id[resolved_value] << resolving_object
|
47
|
+
next
|
48
|
+
end
|
49
|
+
|
50
|
+
if resolving_object.key?(:id)
|
51
|
+
all_nodes_with_same_id = nodes_by_id[resolving_object[:id]]
|
52
|
+
|
53
|
+
other_nodes_with_same_id = all_nodes_with_same_id - [resolving_object]
|
54
|
+
|
55
|
+
other_nodes_with_same_id.each do |another_node|
|
56
|
+
next unless another_node[field_name] != resolved_value
|
57
|
+
raise StandardError,
|
58
|
+
"The field #{parent_type.graphql_name}#id must be unique across the graph." \
|
59
|
+
' You have two nodes representing the same object in the graph, but they are returning different values for the same field.' \
|
60
|
+
" #{parent_type.graphql_name}#id = #{resolving_object[:id]}." \
|
61
|
+
" Divergence: #{(another_node[:__metadata__][:parent_path] + [field_name]).join('.')} = #{another_node[field_name].to_json} and #{path.join('.')} = #{resolved_value.to_json}"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
attr_reader :cache
|
70
|
+
|
71
|
+
def comparable?(value)
|
72
|
+
value.is_a?(Numeric) ||
|
73
|
+
value.is_a?(String) ||
|
74
|
+
value.is_a?(Date) ||
|
75
|
+
value.is_a?(DateTime) ||
|
76
|
+
value.is_a?(Time) ||
|
77
|
+
value.is_a?(TrueClass) ||
|
78
|
+
value.is_a?(FalseClass) ||
|
79
|
+
value.is_a?(NilClass) ||
|
80
|
+
value.is_a?(Hash)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: better-graphql-detect-invalid-id-field
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- gtkatakura
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-12-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.4'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop-shopify
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.0.7
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.0.7
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: graphql
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.11'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.11'
|
97
|
+
description: Detect invalid id field for GraphQL types by detecting divergent fields
|
98
|
+
between nodes representing the same object
|
99
|
+
email:
|
100
|
+
- gt.katakura@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".editorconfig"
|
106
|
+
- ".gitignore"
|
107
|
+
- ".rspec"
|
108
|
+
- ".rubocop.yml"
|
109
|
+
- ".travis.yml"
|
110
|
+
- Gemfile
|
111
|
+
- Gemfile.lock
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- better-graphql-detect-invalid-id-field.gemspec
|
116
|
+
- bin/console
|
117
|
+
- bin/setup
|
118
|
+
- lib/better_graphql.rb
|
119
|
+
- lib/better_graphql/detect_invalid_id_field.rb
|
120
|
+
- lib/better_graphql/version.rb
|
121
|
+
homepage: http://github.com/gtkatakura/better-graphql-detect-invalid-id-field
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
metadata:
|
125
|
+
allowed_push_host: https://rubygems.org
|
126
|
+
homepage_uri: http://github.com/gtkatakura/better-graphql-detect-invalid-id-field
|
127
|
+
source_code_uri: http://github.com/gtkatakura/better-graphql-detect-invalid-id-field
|
128
|
+
changelog_uri: http://github.com/gtkatakura/better-graphql-detect-invalid-id-field/blob/main/CHANGELOG.md
|
129
|
+
post_install_message:
|
130
|
+
rdoc_options: []
|
131
|
+
require_paths:
|
132
|
+
- lib
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
requirements: []
|
144
|
+
rubygems_version: 3.0.3
|
145
|
+
signing_key:
|
146
|
+
specification_version: 4
|
147
|
+
summary: Detect invalid id field for GraphQL types
|
148
|
+
test_files: []
|