<commit_before>defmodule SamplePhoenixReactApp.GraphQlUtils do
  require Logger

  import SamplePhoenixReactApp.GraphQl.Debug

  def parse(query) do
    graphql = GraphQL.parse(query)
    normalized = normalize graphql

    #Logger.debug inspect graphql
    #Apex.ap graphql
    Apex.ap normalize_debug graphql
    #Apex.ap normalized

    normalized
  end

  def test_normalize(graphql) do
    normalize(graphql)
  end

  # normalize
  defp normalize([{:kind, _ = :Field} | _ = optional] = item) do
    item = [{:field, to_string(Keyword.get(item, :name))}]

    if Keyword.has_key?(optional, :arguments) do
      item = item ++ [{:arguments, normalize(Keyword.get(optional, :arguments))}]
    end

    if Keyword.has_key?(optional, :selectionSet) do
      # TODO get_into?
      selections = optional
      |> Keyword.get(:selectionSet)
      |> Keyword.get(:selections)

      item = item ++ [{:selections, normalize(selections)}]
    end

    optional = optional
    |> Keyword.delete(:name)
    |> Keyword.delete(:loc)
    |> Keyword.delete(:arguments)
    |> Keyword.delete(:selectionSet)

    unless length(optional) == 0 do
      Apex.ap optional
      raise "unhandled optionals"
    end

    item
  end

  defp normalize([{:kind, :SelectionSet} | _ = optional] = item) do
    selections = optional
    |> Keyword.get(:selections)
    |> normalize
  end

  defp normalize([{:kind, :InlineFragment} | _ = optional] = item) do
    optional
    |> Keyword.get(:selectionSet)
    |> normalize
  end

  defp normalize([{:kind, _ = kind} | _] = item) do
    item = item
    |> remove_kind
    |> Keyword.delete(:loc)

    Enum.map(item,
      fn {key, value} ->
        case key do
          :name -> {:name, to_string(value)}
          :definitions -> hd normalize(value)
          :selectionSet -> {:selections, normalize(Keyword.get(value, :selections))}
          _ -> {key, normalize(value)}
        end
      end)
  end

  defp normalize([hd|tl]) do
    item = [normalize hd]

    unless length(tl) == 0 do
      item = item ++ normalize(tl)
    end

    item
  end

  defp normalize(item) do
    item
  end

  defp remove_kind [{:kind, _ = kind} | _] = item do
      if kind in [:Document, :OperationDefinition, :SelectionSet] do
        item = Keyword.delete(item, :kind)
      end

      item
  end
end
<commit_msg>fix a bug<commit_after>defmodule SamplePhoenixReactApp.GraphQlUtils do
  require Logger

  import SamplePhoenixReactApp.GraphQl.Debug
  import SamplePhoenixReactApp.GraphQl.Fragment

  def parse(query) do
    graphql = GraphQL.parse(query)
    normalized = graphql
    |> spread()
    |> normalize()

    #Logger.debug inspect graphql
    #Apex.ap graphql
    Apex.ap normalize_debug graphql
    #Apex.ap normalized

    normalized
  end

  def test_normalize(graphql) do
    normalize(graphql)
  end

  # normalize
  defp normalize([{:kind, _ = :Field} | _ = optional] = item) do
    item = [{:field, to_string(Keyword.get(item, :name))}]

    if Keyword.has_key?(optional, :arguments) do
      item = item ++ [{:arguments, normalize(Keyword.get(optional, :arguments))}]
    end

    if Keyword.has_key?(optional, :selectionSet) do
      # TODO get_into?
      selections = optional
      |> Keyword.get(:selectionSet)
      |> Keyword.get(:selections)

      item = item ++ [{:selections, normalize(selections)}]
    end

    optional = optional
    |> Keyword.delete(:name)
    |> Keyword.delete(:loc)
    |> Keyword.delete(:arguments)
    |> Keyword.delete(:selectionSet)

    unless length(optional) == 0 do
      Apex.ap optional
      raise "unhandled optionals"
    end

    item
  end

  defp normalize([{:kind, :SelectionSet} | _ = optional] = item) do
    selections = optional
    |> Keyword.get(:selections)
    |> normalize
  end

  defp normalize([{:kind, :InlineFragment} | _ = optional] = item) do
    optional
    |> Keyword.get(:selectionSet)
    |> normalize
  end

  defp normalize([{:kind, _ = kind} | _] = item) do
    item = item
    |> remove_kind
    |> Keyword.delete(:loc)

    Enum.map(item,
      fn {key, value} ->
        case key do
          :name -> {:name, to_string(value)}
          :definitions -> hd normalize(value)
          :selectionSet -> {:selections, normalize(Keyword.get(value, :selections))}
          _ -> {key, normalize(value)}
        end
      end)
  end

  defp normalize([hd|tl]) do
    item = [normalize hd]

    unless length(tl) == 0 do
      item = item ++ normalize(tl)
    end

    item
  end

  defp normalize(item) do
    item
  end

  defp remove_kind [{:kind, _ = kind} | _] = item do
      if kind in [:Document, :OperationDefinition, :SelectionSet] do
        item = Keyword.delete(item, :kind)
      end

      item
  end
end

{'fraction_non_alphanumeric': 0.12237984331992378, 'fraction_numerical': 0.0008469193309337286, 'mean_word_length': 2.7138364779874213, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4009340', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1517, 'n_words': 363}