graphql_query_argument 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +59 -0
- data/graphql_query_argument.gemspec +1 -1
- data/lib/graphql/query_argument/version.rb +5 -0
- data/lib/graphql/query_argument.rb +41 -0
- data/spec/graphql/query_argument_spec.rb +33 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/support/graphql_schema.rb +21 -0
- metadata +7 -3
- data/lib/graphql/resolvers/query_argument/version.rb +0 -7
- data/lib/graphql/resolvers/query_argument.rb +0 -42
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30fa1d9843e491992f2505e94eed4496300257303878d00c320ed3d0117d3e96
|
4
|
+
data.tar.gz: bb067d7c66aa00191b04c98724ce640d1d6baff1fe1e7b2861d59d356188dc07
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8896bbc0007c24e03f3c6c0a21858471d8cdabbe48e7a5c818f9ecf27299e07e2dec3b857694b66cb3716266112ee70bada3456b6d036b72a60fec30f7e96b7a
|
7
|
+
data.tar.gz: 6d574c85f1404925c4af46525c0f849990e6b958de6f8773e1509b86d945406430a8c7ff4eb7eb9c6b9e7d9fb8f3c4d8cbe554c704984f46c6ccaf7e7e68f2f3
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# GraphQL::QueryArgument
|
2
|
+
A simple query argument for resolver module
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'graphql_query_argument'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
```bash
|
13
|
+
$ bundle
|
14
|
+
```
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
```bash
|
18
|
+
$ gem install graphql_query_argument
|
19
|
+
```
|
20
|
+
## Usage
|
21
|
+
```ruby
|
22
|
+
require 'graphql/query_argument'
|
23
|
+
|
24
|
+
# In your field resolver
|
25
|
+
# `Products < Resolvers::Base` & a CategoryQueryInputs & a `CompanyType` GraphQL type:
|
26
|
+
#
|
27
|
+
module Resolvers
|
28
|
+
class Products < Resolvers::Base
|
29
|
+
include GraphQL::QueryArgument
|
30
|
+
|
31
|
+
type [Types::ProductType], null: false
|
32
|
+
|
33
|
+
query_argument :name, type: [String], required: false do |scope, value|
|
34
|
+
scope.where(name: value)
|
35
|
+
end
|
36
|
+
query_argument :categories, type: Types::CategoryQueryInputs, required: false do |scope, value|
|
37
|
+
scope.joins(:categories).where(categieies: { **value })
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
42
|
+
```ruby
|
43
|
+
# `CompanyType`
|
44
|
+
class CompanyType < Types::BaseObject
|
45
|
+
field :products, null: false, resolver: Resolvers::Products
|
46
|
+
|
47
|
+
def products
|
48
|
+
object.products
|
49
|
+
end
|
50
|
+
end
|
51
|
+
```
|
52
|
+
```ruby
|
53
|
+
# `CategoryQueryInputs`
|
54
|
+
module Types
|
55
|
+
class CategoryQueryInputs < Types::BaseInputObject
|
56
|
+
argument :name, [String], required: false
|
57
|
+
end
|
58
|
+
end
|
59
|
+
```
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module GraphQL
|
2
|
+
module QueryArgument
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
base.instance_eval do
|
6
|
+
@config = {
|
7
|
+
query_arguments: {}
|
8
|
+
}
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(object:, context:, field:)
|
13
|
+
puts field.name.class
|
14
|
+
@scope = context[:current_object].send(field.name.underscore)
|
15
|
+
@query_arguments = self.class.config[:query_arguments]
|
16
|
+
super object: object, context: context, field: field
|
17
|
+
end
|
18
|
+
|
19
|
+
def resolve(**args)
|
20
|
+
args.inject(@scope) do |scope, (name, value)|
|
21
|
+
return scope unless @query_arguments[name]
|
22
|
+
|
23
|
+
instance_exec scope, value, &@query_arguments[name]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module ClassMethods
|
28
|
+
attr_reader :config
|
29
|
+
|
30
|
+
def inherited(base)
|
31
|
+
base.instance_variable_set '@config', @config
|
32
|
+
super(base)
|
33
|
+
end
|
34
|
+
|
35
|
+
def query_argument(name, options = {}, &block)
|
36
|
+
argument(name, options)
|
37
|
+
config[:query_arguments][name] = block
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe GraphQL::QueryArgument do
|
4
|
+
class GraphQLSchema < GraphQL::Schema
|
5
|
+
class QueryType < GraphQL::Schema::Object
|
6
|
+
field :products, resolver: ProductsResolver
|
7
|
+
|
8
|
+
def products
|
9
|
+
[OpenStruct.new(name: 'Product A'),
|
10
|
+
OpenStruct.new(name: 'Product B'),
|
11
|
+
OpenStruct.new(name: 'Product C')]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
query(QueryType)
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'query products' do
|
19
|
+
it 'returns specify products' do
|
20
|
+
query_str = <<-GRAPHQL
|
21
|
+
query {
|
22
|
+
products(names: ["Product B", "Product C"]) {
|
23
|
+
name
|
24
|
+
}
|
25
|
+
}
|
26
|
+
GRAPHQL
|
27
|
+
result = GraphQLSchema.execute(query_str)
|
28
|
+
expect(result['data']['products']).to eq([
|
29
|
+
{ 'name' => 'Product B' }, { 'name' => 'Product C'}
|
30
|
+
])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'graphql'
|
2
|
+
require 'graphql/query_argument'
|
3
|
+
require 'support/graphql_schema'
|
4
|
+
|
5
|
+
class String
|
6
|
+
def underscore
|
7
|
+
word = self.dup
|
8
|
+
word.gsub!(/::/, '/')
|
9
|
+
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
10
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
11
|
+
word.tr!("-", "_")
|
12
|
+
word.downcase!
|
13
|
+
word
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'graphql'
|
2
|
+
require 'graphql/query_argument'
|
3
|
+
|
4
|
+
class ProductType < GraphQL::Schema::Object
|
5
|
+
field :id, ID
|
6
|
+
field :name, String
|
7
|
+
|
8
|
+
def name
|
9
|
+
object.name
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class ProductsResolver < GraphQL::Schema::Resolver
|
14
|
+
include GraphQL::QueryArgument
|
15
|
+
|
16
|
+
type [ProductType], null: true
|
17
|
+
|
18
|
+
query_argument :names, type: [String], required: false do |scope, value|
|
19
|
+
scope.select { |product| value.include?(product.name) }
|
20
|
+
end
|
21
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql_query_argument
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shang
|
@@ -44,9 +44,13 @@ executables: []
|
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
|
+
- README.md
|
47
48
|
- graphql_query_argument.gemspec
|
48
|
-
- lib/graphql/
|
49
|
-
- lib/graphql/
|
49
|
+
- lib/graphql/query_argument.rb
|
50
|
+
- lib/graphql/query_argument/version.rb
|
51
|
+
- spec/graphql/query_argument_spec.rb
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
- spec/support/graphql_schema.rb
|
50
54
|
homepage: https://github.com/shangweilin7/graphql_query_argument
|
51
55
|
licenses:
|
52
56
|
- MIT
|
@@ -1,42 +0,0 @@
|
|
1
|
-
module Graphql
|
2
|
-
module Resolvers
|
3
|
-
module QueryArgument
|
4
|
-
def self.included(base)
|
5
|
-
base.extend ClassMethods
|
6
|
-
base.instance_eval do
|
7
|
-
@config = {
|
8
|
-
query_arguments: {}
|
9
|
-
}
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def initialize(object:, context:, field:)
|
14
|
-
@scope = context[:current_object].send(field.name.underscore)
|
15
|
-
@query_arguments = self.class.config[:query_arguments]
|
16
|
-
super object: object, context: context, field: field
|
17
|
-
end
|
18
|
-
|
19
|
-
def resolve(**args)
|
20
|
-
args.inject(@scope) do |scope, (name, value)|
|
21
|
-
return scope unless @query_arguments[name]
|
22
|
-
|
23
|
-
instance_exec scope, value, &@query_arguments[name]
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
module ClassMethods
|
28
|
-
attr_reader :config
|
29
|
-
|
30
|
-
def inherited(base)
|
31
|
-
base.instance_variable_set '@config', @config
|
32
|
-
super(base)
|
33
|
-
end
|
34
|
-
|
35
|
-
def query_argument(name, options = {}, &block)
|
36
|
-
argument(name, options)
|
37
|
-
config[:query_arguments][name] = block
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|