Skip to content

Commit 756373a

Browse files
authored
Merge pull request #654 from cpunion/access-types-in-function
Provides shorthand access to GraphQL's built-in types within function classes
2 parents a52eb84 + bddde71 commit 756373a

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

lib/graphql/function.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ def arguments
7878
end
7979
end
8080

81+
# Provides shorthand access to GraphQL's built-in types
82+
def types
83+
GraphQL::Define::TypeDefiner.instance
84+
end
85+
8186
# Get or set the return type for this function class & descendants
8287
# @return [GraphQL::BaseType]
8388
def type(premade_type = nil, &block)

spec/graphql/function_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
describe GraphQL::Function do
55
class TestFunc < GraphQL::Function
66
argument :name, GraphQL::STRING_TYPE
7+
argument :age, types.Int
78
type do
89
name "TestFuncPayload"
910
field :name, types.String, hash_key: :name
@@ -20,7 +21,7 @@ def call(o, a, c)
2021
describe "function API" do
2122
it "exposes required info" do
2223
f = TestFunc.new
23-
assert_equal ["name"], f.arguments.keys
24+
assert_equal ["name", "age"], f.arguments.keys
2425
assert_equal "TestFuncPayload", f.type.name
2526
assert_equal "Returns the string you give it", f.description
2627
assert_equal "It's useless", f.deprecation_reason
@@ -69,7 +70,7 @@ def call(o, a, c)
6970

7071
it "gets attributes from the function" do
7172
field = schema.query.fields["test"]
72-
assert_equal ["name"], field.arguments.keys
73+
assert_equal ["name", "age"], field.arguments.keys
7374
assert_equal "TestFuncPayload", field.type.name
7475
assert_equal "Returns the string you give it", field.description
7576
assert_equal "It's useless", field.deprecation_reason
@@ -115,14 +116,14 @@ def call(o, a, c)
115116
it "can override description" do
116117
field = schema.query.fields["blockOverride"]
117118
assert_equal "I have altered the description", field.description
118-
assert_equal ["name", "anArg", "oneMoreArg"], field.arguments.keys
119+
assert_equal ["name", "age", "anArg", "oneMoreArg"], field.arguments.keys
119120
end
120121

121122
it "can add to arguments" do
122123
field = schema.query.fields["argOverride"]
123124
assert_equal "New Description", field.description
124125
assert_equal GraphQL::STRING_TYPE, field.type
125-
assert_equal ["name"], field.arguments.keys
126+
assert_equal ["name", "age"], field.arguments.keys
126127
end
127128
end
128129
end

0 commit comments

Comments
 (0)