demarches-normaliennes/app/graphql/api/v2/context.rb

31 lines
642 B
Ruby
Raw Normal View History

2020-08-05 18:40:47 +02:00
class API::V2::Context < GraphQL::Query::Context
2020-12-18 11:16:18 +01:00
def has_fragment?(name)
if self["has_fragment_#{name}"]
true
else
visitor = HasFragment.new(self.query.selected_operation, name)
visitor.visit
self["has_fragment_#{name}"] = visitor.found
self["has_fragment_#{name}"]
end
end
class HasFragment < GraphQL::Language::Visitor
def initialize(document, name)
super(document)
@name = name.to_s
@found = false
end
attr_reader :found
def on_inline_fragment(node, parent)
if node.type.name == @name
@found = true
end
super
end
end
end