GraphQL: add has_fragment to context
This commit is contained in:
parent
20be409446
commit
d509fe2edb
2 changed files with 32 additions and 0 deletions
30
app/graphql/api/v2/context.rb
Normal file
30
app/graphql/api/v2/context.rb
Normal 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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue