Class: Gitlab::Graphql::Queries::ClientFieldRedactor

Inherits:
GraphQL::Language::Printer
  • Object
show all
Defined in:
lib/gitlab/graphql/queries.rb

Overview

We need to re-write queries to remove all @client fields. Ideally we would do that as a source-to-source transformation of the AST, but doing it using a printer is much simpler.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(skips = true) ⇒ ClientFieldRedactor

Returns a new instance of ClientFieldRedactor.



49
50
51
52
53
54
55
56
57
58
# File 'lib/gitlab/graphql/queries.rb', line 49

def initialize(skips = true)
  @skips = skips
  @fields_printed = 0
  @in_operation = false
  @skipped_arguments = [].to_set
  @printed_arguments = [].to_set
  @used_fragments = [].to_set
  @skipped_fragments = [].to_set
  @used_fragments = [].to_set
end

Instance Attribute Details

#fields_printedObject (readonly)

Returns the value of attribute fields_printed.



47
48
49
# File 'lib/gitlab/graphql/queries.rb', line 47

def fields_printed
  @fields_printed
end

#printed_argumentsObject (readonly)

Returns the value of attribute printed_arguments.



47
48
49
# File 'lib/gitlab/graphql/queries.rb', line 47

def printed_arguments
  @printed_arguments
end

#skipped_argumentsObject (readonly)

Returns the value of attribute skipped_arguments.



47
48
49
# File 'lib/gitlab/graphql/queries.rb', line 47

def skipped_arguments
  @skipped_arguments
end

#used_fragmentsObject (readonly)

Returns the value of attribute used_fragments.



47
48
49
# File 'lib/gitlab/graphql/queries.rb', line 47

def used_fragments
  @used_fragments
end

Instance Method Details



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gitlab/graphql/queries.rb', line 103

def print_field(field, indent: '')
  if skips? &&
      (field.directives.any? { |d| d.name == 'client' || d.name == 'persist' } || field.name == '__persist')
    skipped = self.class.new(false)
    skipped.print(field)
    @skipped_fragments |= skipped.used_fragments
    @skipped_arguments |= skipped.printed_arguments

    return
  end

  super

  @fields_printed += 1 if @in_operation
end


119
120
121
122
123
124
125
# File 'lib/gitlab/graphql/queries.rb', line 119

def print_fragment_definition(fragment_def, indent: "")
  if skips? && @skipped_fragments.include?(fragment_def.name) && @used_fragments.exclude?(fragment_def.name)
    return
  end

  super
end


65
66
67
68
# File 'lib/gitlab/graphql/queries.rb', line 65

def print_fragment_spread(fragment_spread, indent: "")
  @used_fragments << fragment_spread.name
  super
end


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gitlab/graphql/queries.rb', line 70

def print_operation_definition(op, indent: "")
  @in_operation = true
  print_string("#{indent}#{op.operation_type}")
  print_string(" #{op.name}") if op.name

  # Do these on a temp instance, so that we detect any skipped arguments
  # without actually printing the output to the current buffer
  temp_printer = self.class.new
  op.directives.each { |d| temp_printer.print(d) }
  op.selections.each { |s| temp_printer.print(s) }
  @skipped_arguments |= temp_printer.skipped_arguments
  @printed_arguments |= temp_printer.printed_arguments

  # remove variable definitions only used in skipped (client) fields
  vars = op.variables.reject do |v|
    @skipped_arguments.include?(v.name) && @printed_arguments.exclude?(v.name)
  end

  if vars.any?
    print_string("(")
    vars.each_with_index do |v, i|
      print_variable_definition(v)
      print_string(", ") if i < vars.size - 1
    end
    print_string(")")
  end

  print_directives(op.directives)
  print_selections(op.selections, indent: indent)
ensure
  @in_operation = false
end


60
61
62
63
# File 'lib/gitlab/graphql/queries.rb', line 60

def print_variable_identifier(variable_identifier)
  @printed_arguments << variable_identifier.name
  super
end

#skips?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/gitlab/graphql/queries.rb', line 127

def skips?
  @skips
end