Skip to content

Commit aae87af

Browse files
authored
Merge pull request #165 from brettchalupa/deprecation-notices
Render deprecation info for queries & mutations
2 parents 27097af + e925f4b commit aae87af

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

lib/graphql-docs/parser.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ def parse
6060
h[:notices] = @options[:notices].call([object.graphql_name, query.graphql_name].join('.'))
6161
h[:name] = query.graphql_name
6262
h[:description] = query.description
63+
if query.respond_to?(:deprecation_reason) && !query.deprecation_reason.nil?
64+
h[:is_deprecated] = true
65+
h[:deprecation_reason] = query.deprecation_reason
66+
end
6367
h[:arguments], = fetch_fields(query.arguments, [object.graphql_name, query.graphql_name].join('.'))
6468

6569
return_type = query.type
@@ -80,6 +84,10 @@ def parse
8084
h[:notices] = @options[:notices].call([object.graphql_name, mutation.graphql_name].join('.'))
8185
h[:name] = mutation.graphql_name
8286
h[:description] = mutation.description
87+
if mutation.respond_to?(:deprecation_reason) && !mutation.deprecation_reason.nil?
88+
h[:is_deprecated] = true
89+
h[:deprecation_reason] = mutation.deprecation_reason
90+
end
8391
h[:input_fields], = fetch_fields(mutation.arguments, [object.graphql_name, mutation.graphql_name].join('.'))
8492

8593
return_type = mutation.type

test/graphql-docs/parser_test.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,44 @@ def test_deprecations
123123
assert fields[1][:is_deprecated]
124124
assert fields[2][:arguments][0][:is_deprecated]
125125
end
126+
127+
def test_query_field_deprecation
128+
schema = MySchema
129+
results = GraphQLDocs::Parser.new(schema, {}).parse
130+
131+
query_types = results[:query_types]
132+
133+
# Find the myField query
134+
my_field = query_types.find { |q| q[:name] == 'myField' }
135+
refute my_field[:is_deprecated], "myField should not be deprecated"
136+
assert_nil my_field[:deprecation_reason], "myField should not have a deprecation reason"
137+
138+
# Find the deprecatedField query
139+
deprecated_field = query_types.find { |q| q[:name] == 'deprecatedField' }
140+
assert deprecated_field[:is_deprecated], "deprecatedField should be marked as deprecated"
141+
assert_equal "Not useful any more", deprecated_field[:deprecation_reason], "deprecatedField should have correct deprecation reason"
142+
143+
# Find the fieldWithDeprecatedArg query
144+
field_with_deprecated_arg = query_types.find { |q| q[:name] == 'fieldWithDeprecatedArg' }
145+
refute field_with_deprecated_arg[:is_deprecated], "fieldWithDeprecatedArg itself should not be deprecated"
146+
assert field_with_deprecated_arg[:arguments][0][:is_deprecated], "myArg should be marked as deprecated"
147+
assert_equal "Not useful any more", field_with_deprecated_arg[:arguments][0][:deprecation_reason], "myArg should have correct deprecation reason"
148+
end
149+
150+
def test_mutation_field_deprecation
151+
schema = MySchema
152+
results = GraphQLDocs::Parser.new(schema, {}).parse
153+
154+
mutation_types = results[:mutation_types]
155+
156+
# Find the createUser mutation
157+
create_user = mutation_types.find { |m| m[:name] == 'createUser' }
158+
refute create_user[:is_deprecated], "createUser should not be deprecated"
159+
assert_nil create_user[:deprecation_reason], "createUser should not have a deprecation reason"
160+
161+
# Find the deprecatedMutation
162+
deprecated_mutation = mutation_types.find { |m| m[:name] == 'deprecatedMutation' }
163+
assert deprecated_mutation[:is_deprecated], "deprecatedMutation should be marked as deprecated"
164+
assert_equal "Use createUser instead", deprecated_mutation[:deprecation_reason], "deprecatedMutation should have correct deprecation reason"
165+
end
126166
end

test/test_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ class QueryType < GraphQL::Schema::Object
3434
end
3535
end
3636

37+
class MutationType < GraphQL::Schema::Object
38+
field :create_user, Int, null: false
39+
40+
field :deprecated_mutation, Int, null: false, deprecation_reason: "Use createUser instead"
41+
end
42+
3743
class MySchema < GraphQL::Schema
3844
query QueryType
45+
mutation MutationType
3946
end

0 commit comments

Comments
 (0)