title: In gremlin, how to find and sort all triads of connected vertices that a given vertex belongs to?
body: <p>I am making a social app where users can be friends.</p>
  
  <p>For a given user <code>A</code>, I want to find all of the triads of users such that <code>A -isFriends-&gt; B AND B -isFriends-&gt; C AND C -isFriends-&gt; A</code>.</p>
  
  <p>My current approach is as follows: </p>
  
  <pre><code>g.V(A).repeat(__.out('isFriends')).times(3).path().by(id).toList()
  </code></pre>
  
  <p>and then outside of gremlin, I filter out all of the Path objects where the first object is not the same as the last object. I'd prefer to have gremlin do this filtering for me, but I'm unsure how to filter based on the output of <code>path()</code>.</p>
  
  <p>I have tried <code>cyclicPath()</code>, but this simply returns a flat list of Vertex objects, which I do not understand. From this I would expect similar output to <code>path()</code> but with only the paths where the first and last vertices are the same included. Let me know if this expectation is incorrect.</p>
  
  <p>I would also like to then sort these paths based on the results of a sub-traversal (how many mutual friends the three vertices have), but I am unsure how to run a traversal starting on the vertices included in the output of <code>path()</code>, without starting a new gremlin query.</p>
  
  <p>I am using the javascript-gremlin driver (v3.4.4) and am making queries against AWS Neptune, where lambdas are not available. </p>
  
  <p>Please let me know if my approach or understanding is off. </p>