After compiling and re-running, this was the output:
stored_fragments: [
fragment1: {"fragment fragment1 on aType { aProperty }", []},
fragment2: {"fragment fragment2 on anotherType { bProperty }", []}
]
fragments_to_add after find_in_query: [: fragment2, : fragment1, : fragment1, : fragment2]
fragments_to_add after List.keyfind stored_fragments: [
fragment2: {"fragment fragment2 on anotherType { bProperty }", []},
fragment1: {"fragment fragment1 on aType { aProperty }", []},
fragment1: {"fragment fragment1 on aType { aProperty }", []},
fragment2: {"fragment fragment2 on anotherType { bProperty }", []}
]
fragments_to_add after Enum.reject: [
fragment2: {"fragment fragment2 on anotherType { bProperty }", []},
fragment1: {"fragment fragment1 on aType { aProperty }", []},
fragment1: {"fragment fragment1 on aType { aProperty }", []},
fragment2: {"fragment fragment2 on anotherType { bProperty }", []}
]
fragments_to_add after Enum.reduce load_missing_fragments: [
fragment2: {"fragment fragment2 on anotherType { bProperty }", []},
fragment1: {"fragment fragment1 on aType { aProperty }", []},
fragment1: {"fragment fragment1 on aType { aProperty }", []},
fragment2: {"fragment fragment2 on anotherType { bProperty }", []}
]
It appears that the duplication is taking place after the query string is passed to find_in_query/2.
This makes sense, considering its function definition contains a regex to find all ... fragments:
lib/neuron/fragment.ex
defp find_in_query(query_string) do
Regex.scan(~r/(?<=\.\.\.)\w+(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/, query_string)
|> List.flatten()
|> Enum.map(&String.to_atom/1)
end
The fix is a simple one: call Enum.uniq/1 after the call to Enum.map/1 to remove any duplicates! After I made this change, the problem was solved, and the query was executed correctly.
The amended function:
defp find_in_query(query_string) do
Regex.scan(~r/(?<=\.\.\.)\w+(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/, query_string)
|> List.flatten()
|> Enum.map(&String.to_atom/1)
|> Enum.uniq
end
The output after making this change:
stored_fragments: [
fragment1: {"fragment fragment1 on aType { aProperty }", []},
fragment2: {"fragment fragment2 on anotherType { bProperty }", []}
]
fragments_to_add after find_in_query: [: fragment2, : fragment1]
fragments_to_add after List.keyfind stored_fragments: [
fragment1: {"fragment fragment1 on aType { aProperty }", []},
fragment2: {"fragment fragment2 on anotherType { bProperty }", []}
]
fragments_to_add after Enum.reject: [
fragment1: {"fragment fragment1 on aType { aProperty }", []},
fragment2: {"fragment fragment2 on anotherType { bProperty }", []}
]
fragments_to_add after Enum.reduce load_missing_fragments: [
fragment1: {"fragment fragment1 on aType { aProperty }", []},
fragment2: {"fragment fragment2 on anotherType { bProperty }", []}
]
Hey, @uesteibar !
First and foremost, thanks for the awesome lib. I've run into an issue with using multiple fragments in the same body.
Example:
This issue is that after registering these fragments and running the query, I receive this error message:
It's clear from this error message that the fragments are likely being duplicated somehow in the
Neuron.Store
.I dug into Neuron's internals to see what I could find, and threw in some print statements:
lib/neuron/fragment.ex
After compiling and re-running, this was the output:
It appears that the duplication is taking place after the query string is passed to
find_in_query/2
.This makes sense, considering its function definition contains a regex to find all
...
fragments:lib/neuron/fragment.ex
The fix is a simple one: call
Enum.uniq/1
after the call toEnum.map/1
to remove any duplicates! After I made this change, the problem was solved, and the query was executed correctly.The amended function:
The output after making this change: