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 context[:schema_previews].include?(member.metadata[:preview_toggled_by].toggled_by) end def self.use(schema_def, enabled_previews: []) 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)) enabled_previews.each { |preview| preview.apply_to_schema(schema) } 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