Class: PlatformosCheck::GraphqlFile

Inherits:
AppFile
  • Object
show all
Defined in:
lib/platformos_check/graphql_file.rb

Constant Summary collapse

DIR_PREFIX =
%r{\A/?((marketplace_builder|app)/(graph_queries|graphql)s?/|(app/)?modules/((\w|-)*)/(private|public)/(graph_queries|graphql)s?/)}

Instance Attribute Summary

Attributes inherited from AppFile

#storage, #version

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AppFile

#==, #build_name, #dir_names, #initialize, #liquid?, #module_name, #module_original_file?, #module_original_file_path, #module_overwrite_file?, #module_overwrite_file_path, #module_prefix, #name, #page?, #partial?, #path, #relative_path, #remove_extension, #source, #translation?, #yaml?

Constructor Details

This class inherits a constructor from PlatformosCheck::AppFile

Class Method Details

.parse(_source) ⇒ Object



64
65
66
# File 'lib/platformos_check/graphql_file.rb', line 64

def self.parse(_source)
  Struct.new(:warnings, :root)
end

Instance Method Details

#defined_argumentsObject



78
79
80
# File 'lib/platformos_check/graphql_file.rb', line 78

def defined_arguments
  @defined_arguments ||= variables.map(&:name)
end

#dir_prefixObject



28
29
30
# File 'lib/platformos_check/graphql_file.rb', line 28

def dir_prefix
  DIR_PREFIX
end

#fragmentsObject



86
87
88
# File 'lib/platformos_check/graphql_file.rb', line 86

def fragments
  @fragments ||= parse&.definitions&.select { |d| d.is_a?(GraphQL::Language::Nodes::FragmentDefinition) } || []
end

#graphql?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/platformos_check/graphql_file.rb', line 90

def graphql?
  true
end

#optional_argumentsObject



74
75
76
# File 'lib/platformos_check/graphql_file.rb', line 74

def optional_arguments
  @optional_arguments ||= defined_arguments - required_arguments
end

#parseObject



52
53
54
# File 'lib/platformos_check/graphql_file.rb', line 52

def parse
  @parse ||= source && source.strip != '' ? GraphQL.parse(source) : nil
end

#required_argumentsObject



68
69
70
71
72
# File 'lib/platformos_check/graphql_file.rb', line 68

def required_arguments
  @required_arguments ||= variables.each_with_object([]) do |v, vars|
    vars << v.name if v.type.is_a?(GraphQL::Language::Nodes::NonNullType)
  end
end

#rewriterObject



24
25
26
# File 'lib/platformos_check/graphql_file.rb', line 24

def rewriter
  @rewriter ||= AppFileRewriter.new(@relative_path, source)
end

#rootObject



60
61
62
# File 'lib/platformos_check/graphql_file.rb', line 60

def root
  parse&.root || {}
end

#selectionsObject



82
83
84
# File 'lib/platformos_check/graphql_file.rb', line 82

def selections
  definition&.selections
end

#source_excerpt(line) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/platformos_check/graphql_file.rb', line 32

def source_excerpt(line)
  original_lines = source.split("\n")
  original_lines[bounded(0, line - 1, original_lines.size - 1)].strip
rescue StandardError => e
  PlatformosCheck.bug(<<~EOS)
    Exception while running `source_excerpt(#{line})`:
    ```
    #{e.class}: #{e.message}
      #{e.backtrace.join("\n  ")}
    ```

    path: #{path}

    source:
    ```
    #{source}
    ```
  EOS
end

#warningsObject



56
57
58
# File 'lib/platformos_check/graphql_file.rb', line 56

def warnings
  parse&.warnings || {}
end

#writeObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/platformos_check/graphql_file.rb', line 9

def write
  content = rewriter.to_s
  return unless source != content

  @storage.write(@relative_path, content.gsub("\n", @eol))
  @source = content
  @rewriter = nil
  @ast = nil
  @variables = nil
  @definition = nil
  @parse = nil
  @required_arguments = nil
  @defined_arguments = nil
end