class GraphQL::Language::SanitizedPrinter

A custom printer used to print sanitized queries. It inlines provided variables within the query for facilitate logging and analysis of queries.

The printer returns `nil` if the query is invalid.

Since the GraphQL Ruby AST for a GraphQL query doesnt contain any reference on the type of fields or arguments, we have to track the current object, field and input type while printing the query.

@example Printing a scrubbed string

printer = QueryPrinter.new(query)
puts printer.sanitized_query_string

@see {Query#sanitized_query_string}

Constants

REDACTED

Attributes

query[R]

Public Class Methods

new(query, inline_variables: true) click to toggle source
# File lib/graphql/language/sanitized_printer.rb, line 22
def initialize(query, inline_variables: true)
  @query = query
  @current_type = nil
  @current_field = nil
  @current_input_type = nil
  @inline_variables = inline_variables
end

Public Instance Methods

coerce_argument_value_to_list?(type, value) click to toggle source
# File lib/graphql/language/sanitized_printer.rb, line 99
def coerce_argument_value_to_list?(type, value)
  type.list? &&
    !value.is_a?(Array) &&
    !value.nil? &&
    !value.is_a?(GraphQL::Language::Nodes::VariableIdentifier)
end
print_argument(argument) click to toggle source
print_directive(directive) click to toggle source
print_field(field, indent: "") click to toggle source
Calls superclass method GraphQL::Language::Printer#print_field
print_fragment_definition(fragment_def, indent: "") click to toggle source
print_inline_fragment(inline_fragment, indent: "") click to toggle source
print_node(node, indent: "") click to toggle source
Calls superclass method GraphQL::Language::Printer#print_node
print_operation_definition(operation_definition, indent: "") click to toggle source

Print the operation definition but do not include the variable definitions since we will inline them within the query

print_variable_identifier(variable_id) click to toggle source
redact_argument_value?(argument, value) click to toggle source

Indicates whether or not to redact non-null values for the given argument. Defaults to redacting all strings arguments but this can be customized by subclasses.

# File lib/graphql/language/sanitized_printer.rb, line 64
def redact_argument_value?(argument, value)
  # Default to redacting any strings or custom scalars encoded as strings
  type = argument.type.unwrap
  value.is_a?(String) && type.kind.scalar? && (type.graphql_name == "String" || !type.default_scalar?)
end
redacted_argument_value(argument) click to toggle source

Returns the value to use for redacted versions of the given argument. Defaults to the string “<REDACTED>”.

# File lib/graphql/language/sanitized_printer.rb, line 72
def redacted_argument_value(argument)
  REDACTED
end
sanitized_query_string() click to toggle source

@return [String, nil] A scrubbed query string, if the query was valid.

# File lib/graphql/language/sanitized_printer.rb, line 31
def sanitized_query_string
  if query.valid?
    print(query.document)
  else
    nil
  end
end

Private Instance Methods

value_to_ast(value, type) click to toggle source
# File lib/graphql/language/sanitized_printer.rb, line 179
def value_to_ast(value, type)
  type = type.of_type if type.non_null?

  if value.nil?
    return GraphQL::Language::Nodes::NullValue.new(name: "null")
  end

  case type.kind.name
  when "INPUT_OBJECT"
    value = if value.respond_to?(:to_unsafe_h)
      # for ActionController::Parameters
      value.to_unsafe_h
    else
      value.to_h
    end

    arguments = value.map do |key, val|
      sub_type = type.get_argument(key.to_s, @query.context).type

      GraphQL::Language::Nodes::Argument.new(
        name: key.to_s,
        value: value_to_ast(val, sub_type)
      )
    end
    GraphQL::Language::Nodes::InputObject.new(
      arguments: arguments
    )
  when "LIST"
    if value.is_a?(Array)
      value.map { |v| value_to_ast(v, type.of_type) }
    else
      [value].map { |v| value_to_ast(v, type.of_type) }
    end
  when "ENUM"
    GraphQL::Language::Nodes::Enum.new(name: value)
  else
    value
  end
end