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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gitlab/graphql/queries.rb', line 93

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_node(field)
    @skipped_fragments |= skipped.used_fragments
    @skipped_arguments |= skipped.printed_arguments

    return ''
  end

  ret = super

  @fields_printed += 1 if @in_operation && ret != ''

  ret
end


112
113
114
115
116
117
118
# File 'lib/gitlab/graphql/queries.rb', line 112

def print_fragment_definition(fragment_def, indent: "")
  if skips? && @skipped_fragments.include?(fragment_def.name) && !@used_fragments.include?(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
# File 'lib/gitlab/graphql/queries.rb', line 70

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

  # Do these first, so that we detect any skipped arguments
  dirs = print_directives(op.directives)
  sels = print_selections(op.selections, indent: indent)

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

  if vars.any?
    out << "(#{vars.map { |v| print_variable_definition(v) }.join(", ")})"
  end

  out + dirs + sels
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)


120
121
122
# File 'lib/gitlab/graphql/queries.rb', line 120

def skips?
  @skips
end