graphql 0.11.1 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/graphql.rb +5 -2
- data/lib/graphql/argument.rb +8 -3
- data/lib/graphql/base_type.rb +5 -3
- data/lib/graphql/boolean_type.rb +6 -1
- data/lib/graphql/define.rb +8 -0
- data/lib/graphql/define/assign_argument.rb +19 -0
- data/lib/graphql/define/assign_enum_value.rb +16 -0
- data/lib/graphql/define/assign_object_field.rb +19 -0
- data/lib/graphql/define/assignment_dictionary.rb +26 -0
- data/lib/graphql/define/defined_object_proxy.rb +32 -0
- data/lib/graphql/define/instance_definable.rb +79 -0
- data/lib/graphql/{definition_helpers → define}/non_null_with_bang.rb +1 -1
- data/lib/graphql/{definition_helpers → define}/type_definer.rb +1 -1
- data/lib/graphql/directive.rb +7 -2
- data/lib/graphql/enum_type.rb +12 -6
- data/lib/graphql/execution_error.rb +1 -1
- data/lib/graphql/field.rb +26 -23
- data/lib/graphql/float_type.rb +2 -3
- data/lib/graphql/id_type.rb +9 -1
- data/lib/graphql/input_object_type.rb +11 -8
- data/lib/graphql/int_type.rb +2 -1
- data/lib/graphql/interface_type.rb +7 -2
- data/lib/graphql/introspection/input_value_type.rb +10 -1
- data/lib/graphql/invalid_null_error.rb +1 -1
- data/lib/graphql/object_type.rb +27 -25
- data/lib/graphql/query.rb +5 -8
- data/lib/graphql/query/serial_execution/field_resolution.rb +0 -10
- data/lib/graphql/scalar_type.rb +4 -3
- data/lib/graphql/schema.rb +1 -1
- data/lib/graphql/static_validation/rules/document_does_not_exceed_max_depth.rb +1 -1
- data/lib/graphql/static_validation/validation_context.rb +5 -4
- data/lib/graphql/static_validation/validator.rb +3 -3
- data/lib/graphql/string_type.rb +2 -1
- data/lib/graphql/union_type.rb +1 -1
- data/lib/graphql/version.rb +1 -1
- data/readme.md +4 -10
- data/spec/graphql/boolean_type_spec.rb +20 -0
- data/spec/graphql/define/instance_definable_spec.rb +55 -0
- data/spec/graphql/field_spec.rb +14 -2
- data/spec/graphql/float_type_spec.rb +15 -0
- data/spec/graphql/id_type_spec.rb +9 -0
- data/spec/graphql/int_type_spec.rb +15 -0
- data/spec/graphql/introspection/input_value_type_spec.rb +36 -0
- data/spec/graphql/introspection/type_type_spec.rb +0 -23
- data/spec/graphql/query_spec.rb +16 -0
- data/spec/graphql/static_validation/complexity_validator_spec.rb +1 -1
- data/spec/graphql/static_validation/rules/argument_literals_are_compatible_spec.rb +4 -3
- data/spec/graphql/static_validation/rules/arguments_are_defined_spec.rb +4 -3
- data/spec/graphql/static_validation/rules/directives_are_defined_spec.rb +4 -3
- data/spec/graphql/static_validation/rules/document_does_not_exceed_max_depth_spec.rb +9 -1
- data/spec/graphql/static_validation/rules/fields_are_defined_on_type_spec.rb +3 -2
- data/spec/graphql/static_validation/rules/fields_have_appropriate_selections_spec.rb +4 -3
- data/spec/graphql/static_validation/rules/fields_will_merge_spec.rb +4 -3
- data/spec/graphql/static_validation/rules/fragment_spreads_are_possible_spec.rb +4 -3
- data/spec/graphql/static_validation/rules/fragment_types_exist_spec.rb +4 -3
- data/spec/graphql/static_validation/rules/fragments_are_finite_spec.rb +4 -3
- data/spec/graphql/static_validation/rules/fragments_are_on_composite_types_spec.rb +4 -3
- data/spec/graphql/static_validation/rules/fragments_are_used_spec.rb +4 -3
- data/spec/graphql/static_validation/rules/required_arguments_are_present_spec.rb +4 -3
- data/spec/graphql/static_validation/rules/variable_default_values_are_correctly_typed_spec.rb +5 -4
- data/spec/graphql/static_validation/rules/variable_usages_are_allowed_spec.rb +4 -3
- data/spec/graphql/static_validation/rules/variables_are_input_types_spec.rb +4 -3
- data/spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb +4 -3
- data/spec/graphql/static_validation/type_stack_spec.rb +3 -2
- data/spec/graphql/static_validation/validator_spec.rb +3 -25
- data/spec/graphql/string_type_spec.rb +15 -0
- data/spec/support/dairy_app.rb +2 -0
- metadata +25 -9
- data/lib/graphql/definition_helpers.rb +0 -4
- data/lib/graphql/definition_helpers/defined_by_config.rb +0 -123
- data/lib/graphql/definition_helpers/string_named_hash.rb +0 -22
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe GraphQL::StaticValidation::RequiredArgumentsArePresent do
|
4
|
-
let(:
|
4
|
+
let(:query_string) {"
|
5
5
|
query getCheese {
|
6
6
|
cheese(id: 1) { source }
|
7
7
|
cheese { source }
|
@@ -12,10 +12,11 @@ describe GraphQL::StaticValidation::RequiredArgumentsArePresent do
|
|
12
12
|
flavor @include(if: true)
|
13
13
|
id @skip
|
14
14
|
}
|
15
|
-
"
|
15
|
+
"}
|
16
16
|
|
17
17
|
let(:validator) { GraphQL::StaticValidation::Validator.new(schema: DummySchema, rules: [GraphQL::StaticValidation::RequiredArgumentsArePresent]) }
|
18
|
-
let(:
|
18
|
+
let(:query) { GraphQL::Query.new(DummySchema, query_string) }
|
19
|
+
let(:errors) { validator.validate(query) }
|
19
20
|
|
20
21
|
it 'finds undefined arguments to fields and directives' do
|
21
22
|
assert_equal(3, errors.length)
|
data/spec/graphql/static_validation/rules/variable_default_values_are_correctly_typed_spec.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe GraphQL::StaticValidation::VariableDefaultValuesAreCorrectlyTyped do
|
4
|
-
let(:
|
4
|
+
let(:query_string) {%|
|
5
5
|
query getCheese(
|
6
6
|
$id: Int = 1,
|
7
|
-
$
|
7
|
+
$int: Int = 3.4e24, # can be coerced
|
8
8
|
$str: String!,
|
9
9
|
$badFloat: Float = true,
|
10
10
|
$badInt: Int = "abc",
|
@@ -14,10 +14,11 @@ describe GraphQL::StaticValidation::VariableDefaultValuesAreCorrectlyTyped do
|
|
14
14
|
) {
|
15
15
|
cheese(id: $id) { source }
|
16
16
|
}
|
17
|
-
|
17
|
+
|}
|
18
18
|
|
19
19
|
let(:validator) { GraphQL::StaticValidation::Validator.new(schema: DummySchema, rules: [GraphQL::StaticValidation::VariableDefaultValuesAreCorrectlyTyped]) }
|
20
|
-
let(:
|
20
|
+
let(:query) { GraphQL::Query.new(DummySchema, query_string) }
|
21
|
+
let(:errors) { validator.validate(query) }
|
21
22
|
|
22
23
|
it "finds default values that don't match their types" do
|
23
24
|
expected = [
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe GraphQL::StaticValidation::VariableUsagesAreAllowed do
|
4
|
-
let(:
|
4
|
+
let(:query_string) {'
|
5
5
|
query getCheese(
|
6
6
|
$goodInt: Int = 1,
|
7
7
|
$okInt: Int!,
|
@@ -30,10 +30,11 @@ describe GraphQL::StaticValidation::VariableUsagesAreAllowed do
|
|
30
30
|
... on Cheese { id }
|
31
31
|
}
|
32
32
|
}
|
33
|
-
'
|
33
|
+
'}
|
34
34
|
|
35
35
|
let(:validator) { GraphQL::StaticValidation::Validator.new(schema: DummySchema, rules: [GraphQL::StaticValidation::VariableUsagesAreAllowed]) }
|
36
|
-
let(:
|
36
|
+
let(:query) { GraphQL::Query.new(DummySchema, query_string) }
|
37
|
+
let(:errors) { validator.validate(query) }
|
37
38
|
|
38
39
|
it "finds variables used as arguments but don't match the argument's type" do
|
39
40
|
assert_equal(4, errors.length)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe GraphQL::StaticValidation::VariablesAreInputTypes do
|
4
|
-
let(:
|
4
|
+
let(:query_string) {'
|
5
5
|
query getCheese(
|
6
6
|
$id: Int = 1,
|
7
7
|
$str: [String!],
|
@@ -11,10 +11,11 @@ describe GraphQL::StaticValidation::VariablesAreInputTypes do
|
|
11
11
|
) {
|
12
12
|
cheese(id: $id) { source }
|
13
13
|
}
|
14
|
-
'
|
14
|
+
'}
|
15
15
|
|
16
16
|
let(:validator) { GraphQL::StaticValidation::Validator.new(schema: DummySchema, rules: [GraphQL::StaticValidation::VariablesAreInputTypes]) }
|
17
|
-
let(:
|
17
|
+
let(:query) { GraphQL::Query.new(DummySchema, query_string) }
|
18
|
+
let(:errors) { validator.validate(query) }
|
18
19
|
|
19
20
|
it "finds variables whose types are invalid" do
|
20
21
|
expected = [
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe GraphQL::StaticValidation::VariablesAreUsedAndDefined do
|
4
|
-
let(:
|
4
|
+
let(:query_string) {'
|
5
5
|
query getCheese(
|
6
6
|
$usedVar: Int,
|
7
7
|
$usedInnerVar: String,
|
@@ -27,10 +27,11 @@ describe GraphQL::StaticValidation::VariablesAreUsedAndDefined do
|
|
27
27
|
source(notDefined: $undefinedFragmentVar)
|
28
28
|
someField(someArg: $usedFragmentVar)
|
29
29
|
}
|
30
|
-
'
|
30
|
+
'}
|
31
31
|
|
32
32
|
let(:validator) { GraphQL::StaticValidation::Validator.new(schema: DummySchema, rules: [GraphQL::StaticValidation::VariablesAreUsedAndDefined]) }
|
33
|
-
let(:
|
33
|
+
let(:query) { GraphQL::Query.new(DummySchema, query_string) }
|
34
|
+
let(:errors) { validator.validate(query) }
|
34
35
|
|
35
36
|
it "finds variables which are used-but-not-defined or defined-but-not-used" do
|
36
37
|
expected = [
|
@@ -20,11 +20,12 @@ describe GraphQL::StaticValidation::TypeStack do
|
|
20
20
|
}
|
21
21
|
fragment edibleFields on Edible { fatContent @skip(if: false)}
|
22
22
|
|}
|
23
|
-
|
23
|
+
|
24
24
|
let(:validator) { GraphQL::StaticValidation::Validator.new(schema: DummySchema, rules: [TypeCheckValidator]) }
|
25
|
+
let(:query) { GraphQL::Query.new(DummySchema, query_string) }
|
25
26
|
|
26
27
|
it 'stores up types' do
|
27
|
-
validator.validate(
|
28
|
+
validator.validate(query)
|
28
29
|
expected = [
|
29
30
|
["Query", "Cheese"],
|
30
31
|
["Query", "Cheese", "Non-Null"],
|
@@ -1,33 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class SchemaErrorValidator
|
4
|
-
def validate(context)
|
5
|
-
context.errors << GraphQL::StaticValidation::Message.new("Something is wrong: #{context.schema}", line: 100, col: 4)
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
class DocumentErrorValidator
|
10
|
-
include
|
11
|
-
def validate(context)
|
12
|
-
context.errors << GraphQL::StaticValidation::Message.new("Something is wrong: #{context.document.name}", line: 1, col: 1)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
3
|
describe GraphQL::StaticValidation::Validator do
|
17
|
-
let(:
|
18
|
-
let(:
|
19
|
-
let(:errors) { validator.validate(
|
20
|
-
it 'uses rules' do
|
21
|
-
expected_errors = [
|
22
|
-
{"message" => "Something is wrong: This is not a schema", "locations" => [{"line" => 100, "column" => 4}]},
|
23
|
-
{"message" => "Something is wrong: This is not a document", "locations" => [{"line" => 1, "column" => 1}]}
|
24
|
-
]
|
25
|
-
assert_equal(expected_errors, errors)
|
26
|
-
end
|
4
|
+
let(:validator) { GraphQL::StaticValidation::Validator.new(schema: DummySchema) }
|
5
|
+
let(:query) { GraphQL::Query.new(DummySchema, query_string) }
|
6
|
+
let(:errors) { validator.validate(query) }
|
27
7
|
|
28
8
|
describe 'validation order' do
|
29
|
-
let(:validator) { GraphQL::StaticValidation::Validator.new(schema: DummySchema) }
|
30
|
-
let(:document) { GraphQL.parse(query_string)}
|
31
9
|
|
32
10
|
describe 'fields & arguments' do
|
33
11
|
let(:query_string) { %|
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe GraphQL::STRING_TYPE do
|
4
|
+
describe "coerce_input" do
|
5
|
+
it "accepts strings" do
|
6
|
+
assert_equal "str", GraphQL::STRING_TYPE.coerce_input("str")
|
7
|
+
end
|
8
|
+
|
9
|
+
it "doesn't accept other types" do
|
10
|
+
assert_equal nil, GraphQL::STRING_TYPE.coerce_input(100)
|
11
|
+
assert_equal nil, GraphQL::STRING_TYPE.coerce_input(true)
|
12
|
+
assert_equal nil, GraphQL::STRING_TYPE.coerce_input(0.999)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/spec/support/dairy_app.rb
CHANGED
@@ -118,6 +118,8 @@ DairyProductInputType = GraphQL::InputObjectType.define {
|
|
118
118
|
description "Where it came from"
|
119
119
|
end
|
120
120
|
|
121
|
+
input_field :originDairy, types.String, "Dairy which produced it", default_value: "Sugar Hollow Dairy"
|
122
|
+
|
121
123
|
input_field :fatContent, types.Float, "How much fat it has"
|
122
124
|
}
|
123
125
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Mosolgo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parslet
|
@@ -142,14 +142,14 @@ dependencies:
|
|
142
142
|
requirements:
|
143
143
|
- - "~>"
|
144
144
|
- !ruby/object:Gem::Version
|
145
|
-
version: '
|
145
|
+
version: '11.0'
|
146
146
|
type: :development
|
147
147
|
prerelease: false
|
148
148
|
version_requirements: !ruby/object:Gem::Requirement
|
149
149
|
requirements:
|
150
150
|
- - "~>"
|
151
151
|
- !ruby/object:Gem::Version
|
152
|
-
version: '
|
152
|
+
version: '11.0'
|
153
153
|
description: A GraphQL server implementation for Ruby. Includes schema definition,
|
154
154
|
query parsing, static validation, type definition, and query execution.
|
155
155
|
email:
|
@@ -163,11 +163,15 @@ files:
|
|
163
163
|
- lib/graphql/argument.rb
|
164
164
|
- lib/graphql/base_type.rb
|
165
165
|
- lib/graphql/boolean_type.rb
|
166
|
-
- lib/graphql/
|
167
|
-
- lib/graphql/
|
168
|
-
- lib/graphql/
|
169
|
-
- lib/graphql/
|
170
|
-
- lib/graphql/
|
166
|
+
- lib/graphql/define.rb
|
167
|
+
- lib/graphql/define/assign_argument.rb
|
168
|
+
- lib/graphql/define/assign_enum_value.rb
|
169
|
+
- lib/graphql/define/assign_object_field.rb
|
170
|
+
- lib/graphql/define/assignment_dictionary.rb
|
171
|
+
- lib/graphql/define/defined_object_proxy.rb
|
172
|
+
- lib/graphql/define/instance_definable.rb
|
173
|
+
- lib/graphql/define/non_null_with_bang.rb
|
174
|
+
- lib/graphql/define/type_definer.rb
|
171
175
|
- lib/graphql/directive.rb
|
172
176
|
- lib/graphql/directive/include_directive.rb
|
173
177
|
- lib/graphql/directive/skip_directive.rb
|
@@ -268,14 +272,19 @@ files:
|
|
268
272
|
- lib/graphql/version.rb
|
269
273
|
- readme.md
|
270
274
|
- spec/graphql/base_type_spec.rb
|
275
|
+
- spec/graphql/boolean_type_spec.rb
|
276
|
+
- spec/graphql/define/instance_definable_spec.rb
|
271
277
|
- spec/graphql/directive_spec.rb
|
272
278
|
- spec/graphql/enum_type_spec.rb
|
273
279
|
- spec/graphql/execution_error_spec.rb
|
274
280
|
- spec/graphql/field_spec.rb
|
281
|
+
- spec/graphql/float_type_spec.rb
|
275
282
|
- spec/graphql/id_type_spec.rb
|
276
283
|
- spec/graphql/input_object_type_spec.rb
|
284
|
+
- spec/graphql/int_type_spec.rb
|
277
285
|
- spec/graphql/interface_type_spec.rb
|
278
286
|
- spec/graphql/introspection/directive_type_spec.rb
|
287
|
+
- spec/graphql/introspection/input_value_type_spec.rb
|
279
288
|
- spec/graphql/introspection/introspection_query_spec.rb
|
280
289
|
- spec/graphql/introspection/schema_type_spec.rb
|
281
290
|
- spec/graphql/introspection/type_type_spec.rb
|
@@ -321,6 +330,7 @@ files:
|
|
321
330
|
- spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
|
322
331
|
- spec/graphql/static_validation/type_stack_spec.rb
|
323
332
|
- spec/graphql/static_validation/validator_spec.rb
|
333
|
+
- spec/graphql/string_type_spec.rb
|
324
334
|
- spec/graphql/union_type_spec.rb
|
325
335
|
- spec/spec_helper.rb
|
326
336
|
- spec/support/calculator_schema.rb
|
@@ -355,14 +365,19 @@ specification_version: 4
|
|
355
365
|
summary: A GraphQL server implementation for Ruby
|
356
366
|
test_files:
|
357
367
|
- spec/graphql/base_type_spec.rb
|
368
|
+
- spec/graphql/boolean_type_spec.rb
|
369
|
+
- spec/graphql/define/instance_definable_spec.rb
|
358
370
|
- spec/graphql/directive_spec.rb
|
359
371
|
- spec/graphql/enum_type_spec.rb
|
360
372
|
- spec/graphql/execution_error_spec.rb
|
361
373
|
- spec/graphql/field_spec.rb
|
374
|
+
- spec/graphql/float_type_spec.rb
|
362
375
|
- spec/graphql/id_type_spec.rb
|
363
376
|
- spec/graphql/input_object_type_spec.rb
|
377
|
+
- spec/graphql/int_type_spec.rb
|
364
378
|
- spec/graphql/interface_type_spec.rb
|
365
379
|
- spec/graphql/introspection/directive_type_spec.rb
|
380
|
+
- spec/graphql/introspection/input_value_type_spec.rb
|
366
381
|
- spec/graphql/introspection/introspection_query_spec.rb
|
367
382
|
- spec/graphql/introspection/schema_type_spec.rb
|
368
383
|
- spec/graphql/introspection/type_type_spec.rb
|
@@ -408,6 +423,7 @@ test_files:
|
|
408
423
|
- spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
|
409
424
|
- spec/graphql/static_validation/type_stack_spec.rb
|
410
425
|
- spec/graphql/static_validation/validator_spec.rb
|
426
|
+
- spec/graphql/string_type_spec.rb
|
411
427
|
- spec/graphql/union_type_spec.rb
|
412
428
|
- spec/spec_helper.rb
|
413
429
|
- spec/support/calculator_schema.rb
|
@@ -1,123 +0,0 @@
|
|
1
|
-
module GraphQL
|
2
|
-
module DefinitionHelpers
|
3
|
-
# Provide a two-step definition process.
|
4
|
-
#
|
5
|
-
# 1. Use a config object to gather definitions
|
6
|
-
# 2. Transfer definitions to an actual instance of an object
|
7
|
-
#
|
8
|
-
module DefinedByConfig
|
9
|
-
def self.included(base)
|
10
|
-
base.extend(ClassMethods)
|
11
|
-
end
|
12
|
-
|
13
|
-
# This object is `instance_eval`'d when defining _any_ object in the schema.
|
14
|
-
# Then, the applicable properties of this object are transfered to the given instance.
|
15
|
-
class DefinitionConfig
|
16
|
-
def self.attr_definable(*names)
|
17
|
-
attr_accessor(*names)
|
18
|
-
names.each do |name|
|
19
|
-
ivar_name = "@#{name}".to_sym
|
20
|
-
define_method(name) do |new_value=nil|
|
21
|
-
new_value && self.instance_variable_set(ivar_name, new_value)
|
22
|
-
instance_variable_get(ivar_name)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
attr_definable :name, :description,
|
28
|
-
:interfaces, # object
|
29
|
-
:deprecation_reason, # field
|
30
|
-
:type, # field / argument
|
31
|
-
:property, # field
|
32
|
-
:resolve, # field / directive
|
33
|
-
:resolve_type, # interface / union
|
34
|
-
:possible_types, # interface / union
|
35
|
-
:default_value, # argument
|
36
|
-
:on, # directive
|
37
|
-
:coerce, #scalar
|
38
|
-
:coerce_input, #scalar
|
39
|
-
:coerce_result #scalar
|
40
|
-
|
41
|
-
attr_reader :fields, :input_fields, :arguments, :values
|
42
|
-
|
43
|
-
def initialize
|
44
|
-
@interfaces = []
|
45
|
-
@possible_types = []
|
46
|
-
@on = []
|
47
|
-
@fields = {}
|
48
|
-
@arguments = {}
|
49
|
-
@values = []
|
50
|
-
@input_fields = {}
|
51
|
-
end
|
52
|
-
|
53
|
-
def types
|
54
|
-
GraphQL::DefinitionHelpers::TypeDefiner.instance
|
55
|
-
end
|
56
|
-
|
57
|
-
def field(name, type = nil, desc = nil, field: nil, property: nil, &block)
|
58
|
-
if block_given?
|
59
|
-
field = GraphQL::Field.define(&block)
|
60
|
-
else
|
61
|
-
field ||= GraphQL::Field.new
|
62
|
-
end
|
63
|
-
type && field.type = type
|
64
|
-
desc && field.description = desc
|
65
|
-
property && field.property = property
|
66
|
-
field.name ||= name.to_s
|
67
|
-
fields[name.to_s] = field
|
68
|
-
end
|
69
|
-
|
70
|
-
# For EnumType
|
71
|
-
def value(name, desc = nil, deprecation_reason: nil, value: name)
|
72
|
-
values << GraphQL::EnumType::EnumValue.new(name: name, description: desc, deprecation_reason: deprecation_reason, value: value)
|
73
|
-
end
|
74
|
-
|
75
|
-
# For InputObjectType
|
76
|
-
def input_field(name, type = nil, desc = nil, default_value: nil, &block)
|
77
|
-
argument = if block_given?
|
78
|
-
GraphQL::Argument.define(&block)
|
79
|
-
else
|
80
|
-
GraphQL::Argument.new
|
81
|
-
end
|
82
|
-
argument.name = name
|
83
|
-
type && argument.type = type
|
84
|
-
desc && argument.description = desc
|
85
|
-
default_value && argument.default_value = default_value
|
86
|
-
input_fields[name.to_s] = argument
|
87
|
-
end
|
88
|
-
|
89
|
-
def argument(name, type, description = nil, default_value: nil)
|
90
|
-
argument = GraphQL::Argument.new
|
91
|
-
argument.name = name.to_s
|
92
|
-
argument.type = type
|
93
|
-
argument.description = description
|
94
|
-
argument.default_value = default_value
|
95
|
-
@arguments[name.to_s] = argument
|
96
|
-
end
|
97
|
-
|
98
|
-
def to_instance(object, attributes)
|
99
|
-
attributes.each do |attr_name|
|
100
|
-
configured_value = self.public_send(attr_name)
|
101
|
-
object.public_send("#{attr_name}=", configured_value)
|
102
|
-
end
|
103
|
-
object
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
module ClassMethods
|
108
|
-
# Pass the block to this class's `DefinitionConfig`,
|
109
|
-
# The return the result of {DefinitionConfig#to_instance}
|
110
|
-
def define(&block)
|
111
|
-
config = DefinitionConfig.new
|
112
|
-
block && config.instance_eval(&block)
|
113
|
-
config.to_instance(self.new, @defined_attrs)
|
114
|
-
end
|
115
|
-
|
116
|
-
def defined_by_config(*defined_attrs)
|
117
|
-
@defined_attrs ||= []
|
118
|
-
@defined_attrs += defined_attrs
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
module GraphQL
|
2
|
-
module DefinitionHelpers
|
3
|
-
# Accepts a hash with symbol keys.
|
4
|
-
# - convert keys to strings
|
5
|
-
# - if the value responds to `name=`, then assign the hash key as `name`
|
6
|
-
#
|
7
|
-
# Used by {ObjectType#fields}, {Field#arguments} and others.
|
8
|
-
class StringNamedHash
|
9
|
-
# Normalized hash for the input
|
10
|
-
# @return [Hash] Hash with string keys
|
11
|
-
attr_reader :to_h
|
12
|
-
|
13
|
-
# @param input_hash [Hash] Hash to be normalized
|
14
|
-
def initialize(input_hash)
|
15
|
-
@to_h = input_hash
|
16
|
-
.reduce({}) { |memo, (key, value)| memo[key.to_s] = value; memo }
|
17
|
-
# Set the name of the value based on its key
|
18
|
-
@to_h.each {|k, v| v.respond_to?("name=") && v.name = k }
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|