/
deprecated_directive.cr
41 lines (36 loc) · 1.02 KB
/
deprecated_directive.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require "./directive"
module GraphQL
module Directives
#
# Directive that allows to annote
# fields as deprecated in the schema
# definition
#
module IsDeprecated
macro included
@deprecated : Bool?
@deprecation_reason : String?
def _graphql_deprecated
@deprecated ||= directives.any? &.name.==("deprecated")
end
def _graphql_deprecation_reason(schema)
@deprecation_reason ||= (
if dir = directives.find(&.name.==("deprecated"))
dir.arguments.find(&.name = "reason").try(&.value) ||
schema.directive_definitions["deprecated"]
.arguments.find(&.name.==("reason")).try &.default_value
else
nil
end.as(String?)
)
end
field :isDeprecated do
_graphql_deprecated
end
field :deprecationReason do |args, context|
_graphql_deprecation_reason(context.schema)
end
end
end
end
end