Sha256: da56a24d6dd591bb8173c320d60a89452139dceaad8c05ef860d6647317da8d1

Contents?: true

Size: 1.42 KB

Versions: 10

Compression:

Stored size: 1.42 KB

Contents

module GraphQL
  # Directives are server-defined hooks for modifying execution.
  #
  # Two directives are included out-of-the-box:
  # - `@skip(if: ...)` Skips the tagged field if the value of `if` is true
  # - `@include(if: ...)` Includes the tagged field _only_ if `if` is true
  #
  class Directive
    include GraphQL::Define::InstanceDefinable
    accepts_definitions :locations, :name, :description, :include_proc, argument: GraphQL::Define::AssignArgument

    lazy_defined_attr_accessor :locations, :arguments, :name, :description, :include_proc

    LOCATIONS = [
      QUERY =               :QUERY,
      MUTATION =            :MUTATION,
      SUBSCRIPTION =        :SUBSCRIPTION,
      FIELD =               :FIELD,
      FRAGMENT_DEFINITION = :FRAGMENT_DEFINITION,
      FRAGMENT_SPREAD =     :FRAGMENT_SPREAD,
      INLINE_FRAGMENT =     :INLINE_FRAGMENT,
    ]

    def initialize
      @arguments = {}
    end

    def include?(arguments)
      include_proc.call(arguments)
    end

    def to_s
      "<GraphQL::Directive #{name}>"
    end

    def on_field?
      locations.include?(FIELD)
    end

    def on_fragment?
      locations.include?(FRAGMENT_SPREAD) && locations.include?(INLINE_FRAGMENT)
    end

    def on_operation?
      locations.include?(QUERY) && locations.include?(MUTATION) && locations.include?(SUBSCRIPTION)
    end
  end
end

require "graphql/directive/include_directive"
require "graphql/directive/skip_directive"

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
graphql-0.18.14 lib/graphql/directive.rb
graphql-0.18.13 lib/graphql/directive.rb
graphql-0.18.12 lib/graphql/directive.rb
graphql-0.18.11 lib/graphql/directive.rb
graphql-0.18.10 lib/graphql/directive.rb
graphql-0.18.9 lib/graphql/directive.rb
graphql-0.18.8 lib/graphql/directive.rb
graphql-0.18.7 lib/graphql/directive.rb
graphql-0.18.6 lib/graphql/directive.rb
graphql-0.18.5 lib/graphql/directive.rb