Sha256: f3a2e354f7774ee66feb60a8888460fc406d603a28db48bdd61c8362e7dcb0de

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

require "graphql"
require "graphql-preview/instrumenter"
require "graphql-preview/version"
require "graphql-preview/schema_modification"
require "graphql-preview/mask"
require "graphql-preview/extensions/enabled_previews"

module GraphQLPreview
  def self.visible?(member, context:)
    return true unless member.respond_to?(:metadata)
    return true unless member.metadata[:preview_toggled_by]

    unless context.key?(:schema_previews)
      raise ArgumentError, "[GraphQLPreview] `:schema_previews` was not provided to the `context`"
    end

    schema_preview_environment = context[:schema_preview_environment] || SchemaModification::DEFAULT_ENVIRONMENT

    toggled_by_preview = member.metadata[:preview_toggled_by][schema_preview_environment]

    if toggled_by_preview
      context[:schema_previews].include?(toggled_by_preview.toggled_by)
    else
      false
    end
  end

  def self.use(schema_def, enabled_previews: [], possible_environments: [SchemaModification::DEFAULT_ENVIRONMENT])
    unless enabled_previews.is_a?(Array)
      raise ArgumentError, "Expected `enabled_previews` to be an array, but it was `#{enabled_previews.class}`"
    end

    enabled_previews.each do |preview|
      unless preview < GraphQLPreview::SchemaModification
        raise ArgumentError, "Preview #{preview} must inherit from `GraphQLPreview::SchemaModification`"
      end
    end

    schema = schema_def.target
    schema_def.instrument(:field, Instrumenter.new(enabled_previews, possible_environments))
    enabled_previews.each { |preview| preview.apply_to_schema(schema, possible_environments) }
    schema.metadata[:enabled_previews] = enabled_previews
  end
end

module GraphQL
  Schema.class_eval do
    # To support `graphql-ruby`'s class-based API
    extend GraphQLPreview::Extensions::EnabledPreviews
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql-preview-0.2.0 lib/graphql-preview.rb
graphql-preview-0.1.1 lib/graphql-preview.rb