title: Gremlin query to find the entire sub-graph that a specific node is connected in any way to
body: <p>I am brand new to Gremlin and am using <code>gremlin-python</code> to traverse my graph. The graph is made up of many clusters or sub-graphs which are intra-connected, and not inter-connected with any other cluster in the graph.</p>
  <p>A simple example of this is a graph with 5 nodes and 3 edges:</p>
  <ul>
  <li><code>Customer_1</code> is connected to <code>CreditCard_A</code> with <code>1_HasCreditCard_A</code> edge</li>
  <li><code>Customer_2</code> is connected to <code>CreditCard_B</code> with <code>2_HasCreditCard_B</code> edge</li>
  <li><code>Customer_3</code> is connected to <code>CreditCard_A</code> with <code>3_HasCreditCard_A</code> edge</li>
  </ul>
  <p>I want a query that will return a sub-graph object of all nodes and edges connected (in or out) to the queried node. I can then store this sub-graph as a variable and then run different traversals on it to calculate different things.</p>
  <p>This query would need to be recursive as these clusters could be made up of nodes which are many (inward or outward) hops away from each other. There are also many different types of nodes and edges, and they all must be returned.</p>
  <p>For example:</p>
  <ul>
  <li>If I specified <code>Customer_1</code> in the query, the resulting sub-graph would contain <code>Customer_1</code>, <code>Customer_3</code>, <code>CreditCardA</code>, <code>1_HasCreditCard_A</code>, and <code>3_HasCreditCard_A</code>.</li>
  <li>If I specififed <code>Customer_2</code>, the returned sub-graph would consist of <code>Customer_2</code>, <code>CreditCard_B</code>, <code>2_HasCreditCard_B</code>.</li>
  <li>If I queried <code>Customer_3</code>, the exact same subgraph object as returned from the <code>Customer_1</code> query would be returned.</li>
  </ul>
  <p>I have used both Neo4J with Cypher and Dgraph with GraphQL and found this task quite easy in these two langauges, but am struggling a bit more with understanding gremlin.</p>
  <p><strong>EDIT:</strong></p>
  <p>From, <a href="https://stackoverflow.com/questions/29384300/gremlin-recursively-find-nodes-connected-by-an-edge-type">this question</a>, the selected answer should achieve what I want, but without specifying the edge type by changing <code>.both('created')</code> to just <code>.both()</code>.</p>
  <p>However, the loop syntax: <code>.loop{true}{true}</code> is invalid in Python of course. Is this loop function available in <code>gremlin-python</code>? I cannot find anything.</p>
  <p><strong>EDIT 2:</strong></p>
  <p>I have tried this and it seems to be working as expected, I think.</p>
  <pre><code>g.V(node_id).repeat(bothE().otherV().simplePath()).emit()
  </code></pre>
  <p>Is this a valid solution to what I am looking for? Is it also possible to include the queried node in this result?</p>