title: How to get newly created resource to client with CQRS and event sourcing based microservices
body: <p>I'm experimenting with microservices, event sourcing and CQRS. However, I'm a little bit confused about how I go from issuing a command to performing a query to return the new state, specifically with regard to interactions with a web API gateway.</p>
  
  <p>As an example, the simple application I am attempting to write (which probably doesn't actually need any of these; it is just something to aid my learning) creates a random-graph and then performs some long-running calculations on the graph. I've modelled this as two separate services: the <code>GraphService</code> and the <code>ComputationService</code>. The imagined process flow is as follows:</p>
  
  <ol>
  <li>User requests new random graph.</li>
  <li>API gateway constructs <code>CreateGraph</code> command and sends it to the
  <code>graph service</code>.</li>
  <li><code>GraphService command handler</code> creates a graph and publishes a
  <code>GraphCreated</code> event.</li>
  <li><code>GraphService event handler</code> subscribes to topic for graph events,
  processes <code>GraphCreated</code> event and stores graph in persistent read
  storage.</li>
  <li><strong>Client somehow gets the newly created graph.</strong></li>
  <li><code>ComputationService event handler</code> subscribes to topic for graph
  events, processes <code>GraphCreated</code> event and begins potentially
  long-running computation, e.g. calculate diameter.</li>
  <li><code>ComputationService</code> publishes <code>DiameterComputed</code> event.</li>
  <li><code>GraphService</code> event handler subscribes to topic for computation
  events, processed <code>DiameterComputed</code> event and updates the graph in
  persistent read storage.</li>
  <li>Client somehow gets updated - easier than getting the new graph, since already have an ID and can poll for changes / websockets / SSE, etc.</li>
  </ol>
  
  <p>That seems relatively simple. However, my confusion lies in how to go about informing the API gateway, and thus the web client, of the new graph (as highlighted in bold above). In a typical CRUD process, the result of the <code>POST request</code> to create a new graph would be to return the URL of the new resource, for instance. However, with CQRS, commands should return nothing or an exception.</p>
  
  <p>How do I pass information back to the client of the service (in this case the API gateway) about the ID of the new graph so that it can perform a query to get the representation of the new resource and send it to the user? Or at least get an ID so that the web client can ask the API gateway, etc?</p>
  
  <p>As I see it at the moment, after sending a command, everyone is just left hanging. There needs to be some sort of subscription model that can be interrogated for the status of the graph creation. I considered having the API gateway generate a request ID which gets embedded with the <code>CreateGraph</code> command, but this then couples the service to the API.</p>
  
  <p>I'm obviously missing something, but have no idea what. None of the examples I've looked at or discussions I've read address this issue and assume that the ID of whatever resource is already known. I couldn't find any discussions here addressing this issue, but if I've just missed them, please point me there rather than duplicating questions. Any pointers would be hugely welcomed.</p>