GraphQL: add has_fragment to context

This commit is contained in:
Paul Chavard 2020-12-18 11:16:18 +01:00
parent 20be409446
commit d509fe2edb
2 changed files with 32 additions and 0 deletions

View file

@ -0,0 +1,30 @@
class Api::V2::Context < GraphQL::Query::Context
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

View file

@ -6,6 +6,8 @@ class Api::V2::Schema < GraphQL::Schema
query Types::QueryType
mutation Types::MutationType
context_class Api::V2::Context
def self.id_from_object(object, type_definition, ctx)
object.to_typed_id
end