Module: GraphqlTagPluck

Defined in:
lib/graphql_tag_pluck.rb,
lib/graphql_tag_pluck/config.rb,
lib/graphql_tag_pluck/railtie.rb,
lib/graphql_tag_pluck/version.rb

Defined Under Namespace

Classes: Config, Error, Railtie

Constant Summary collapse

VERSION =
"0.1.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.optionsObject

Returns the value of attribute options.



14
15
16
# File 'lib/graphql_tag_pluck.rb', line 14

def options
  @options
end

Class Method Details

.create_node_from_file(file_path) ⇒ Object



55
56
57
58
# File 'lib/graphql_tag_pluck.rb', line 55

def create_node_from_file(file_path)
  file_string = File.read(file_path)
  Parser::CurrentRuby.parse(file_string)
end

.default_optionsObject



16
17
18
19
20
21
22
# File 'lib/graphql_tag_pluck.rb', line 16

def default_options
  {
    graphql_heredoc_identifiers: %w{GRAPHQL GQL},
    file_glob_pattern: "#{Dir.pwd}/**/*.rb",
    output_path: "__generated__/graphql_operation_list.json"
  }
end

.get_graphql_heredocsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/graphql_tag_pluck.rb', line 24

def get_graphql_heredocs
  files = Dir.glob(GraphqlTagPluck.options[:file_glob_pattern])
  unnamed_operation_count = 0

  {}.tap do |hash|
    files.each do |file|
      node = create_node_from_file(file)
      parsed_node_array = parse_node_recursively(node)
      next if parsed_node_array.nil?

      parsed_node_array.flatten.compact.each do |graphql_doc_string|
        parsed_graphql_doc_string = GraphQL.parse(graphql_doc_string)
        parsed_graphql_doc_string.definitions.each do |definition|
          key = if definition.name.nil?
            unnamed_operation_count += 1
            "unnamed_operation_#{unnamed_operation_count}"
          else
            definition.name
          end

          hash[key] = { 
            name: definition.name,
            source: definition.to_query_string,
            type: definition.instance_of?(GraphQL::Language::Nodes::FragmentDefinition) ? 'fragment' : definition.operation_type 
          }
        end
      end
    end
  end
end

.is_graphql_heredoc_node?(node) ⇒ Boolean



66
67
68
69
# File 'lib/graphql_tag_pluck.rb', line 66

def is_graphql_heredoc_node?(node)
  node.loc.instance_of?(Parser::Source::Map::Heredoc) &&
    GraphqlTagPluck.options[:graphql_heredoc_identifiers].any? {|identifier| node.loc.expression.source.include?(identifier) }
end

.parse_node_recursively(node) ⇒ Object



60
61
62
63
64
# File 'lib/graphql_tag_pluck.rb', line 60

def parse_node_recursively(node)
  return nil unless node.respond_to?(:loc)
  return node.loc.heredoc_body.source if is_graphql_heredoc_node?(node)
  return node.children.map {|child| parse_node_recursively(child) } if node.respond_to?(:children)
end