<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="212894" data-post-id="212894">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="andreaseriksson" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/andreaseriksson/120/18213_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  andreaseriksson
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I have it in the context because I would use it in controllers and liveviews.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="212894" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-do-you-structure-your-queries/39431/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-212894" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="212894"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="213482" data-post-id="213482">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="cj5" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cj5/120/21890_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  cj5
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Not something that is often recommended (because it is not very explicit and requires model knowledge), but I have been doing something like this for a project (it is an API only project that exposes a GQL API for a SPA).</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def list_users(opts \\ []) do 
  preload = Keyword.get(opts, :preload, [])

  User
  |&gt; Repo.base_query(opts)
  |&gt; Repo.all()
  |&gt; Repo.preload(preload)
end

# ...
list_users(where: %{first_name: "John"}, order_by: %{verified_at: :desc}, preload: [:friends])
</code></pre>
<p>And then magic happens inside <code>Repo.base_query</code> that sends appends the where and orders to the query. Works well and is very concise considering this has to be done for several different schemas and the orders and where clauses could be very different depending on the GQL API usage.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="213482" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-do-you-structure-your-queries/39431/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-213482" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="213482"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="213651" data-post-id="213651">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="tomekowal" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tomekowal/120/2847_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  tomekowal
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I like solutions proposed here but I’d like to suggest one more.<br>
I’ve read an article somewhere that promoted the following idea:<br>
group your functions into module by return type.</p>
<p>E.g. one module returns a User or a list of Users and other module returns Users with Friends.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="213651" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-do-you-structure-your-queries/39431/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-213651" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="213651"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="213688" data-post-id="213688">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="OvermindDL1" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/OvermindDL1/120/2677_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  OvermindDL1
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>For note, I would go with the composable versions nowadays, I only used my more complicated/powerful builder because back when it was made you couldn’t name a join in Ecto like you can now, they were only positional back then, so I had to build up the joins first then encode the positions into a dispatched code flow since I couldn’t dynamically select, it was a pain, but powerful.  Nowadays with named joins it is so much easier!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="213688" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-do-you-structure-your-queries/39431/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-213688" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="213688"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="214154" data-post-id="214154">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="csadewa" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/csadewa/120/24535_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  csadewa
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>For more complicated usecase, pattern that i frequently use is to write composeable query with a decent amount of named join. It produce readable code, with nice behavior of join only when necessary.</p>
<p>It looks like</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule UserQuery do
  def has_minimum_rating(query, rating) do
    query = query_require(query, :user_profile)
    from(query, [user_profile: up], up.rating &gt; ^ rating)
  end

  def name_like(query, name) do
    query = query_require(query, :user_profile)
    from(query, [user_profile: up], ilike(up.name, ^"#{name}*")
  end
  ...
  defp query_require(query, identifiers) when is_list(identifiers) do
    Enum.reduce(identifiers, query, fn identifier, q -&gt;
      query_require(q, identifier)
    end)
  end

  defp query_require(query, identifier) do
    if has_named_binding?(query, identifier) do
      query
    else
      implement_query_require(query, identifier)
    end
  end

  defp implement_query_require(query, :user_profile) do
    join(query, :left, [u], up in assoc(u, :user_profile), as: :user_profile)
  end
end
</code></pre>
<p>and then used like</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">User
|&gt; UserQuery.has_minimum_rating(4)
|&gt; UserQuery.name_like("john")
|&gt; Repo.all()
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="214154" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-do-you-structure-your-queries/39431/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-214154" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="214154"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>