<h2>TL;DR</h2>
<p>Yes, GraphQL does support a sort of pseudo-join. You can see the books and authors example below running in <a href="https://github.com/simbo1905/quarkus-graphql-bigquery" rel="noreferrer">my demo project</a>.</p>
<h2>Example</h2>
<p>Consider a simple database design for storing info about books:</p>
<pre class="lang-sql prettyprint-override"><code>create table Book ( id string, name string, pageCount string, authorId string ); 
create table Author ( id string, firstName string, lastName string );  
</code></pre>
<p>Because we know that Author can write many Books that database model puts them in separate tables. Here is the GraphQL schema:</p>
<pre><code>type Query {
    bookById(id: ID): Book
}

type Book {
    id: ID
    title: String
    pageCount: Int
    author: Author
}

type Author {
    id: ID
    firstName: String
    lastName: String
}
</code></pre>
<p>Notice there is no <code>authorId</code> on the <code>Book</code> type but a type <code>Author</code>. The database <code>authorId</code> column on the book table is not exposed to the outside world. It is an internal detail.</p>
<p>We can pull back a book and it's author using this GraphQL query:</p>
<pre><code>{
  bookById(id:&quot;book-1&quot;){
    id
    title
    pageCount
    author {
      firstName
      lastName
    }
  }
}
</code></pre>
<p>Here is a screenshot of it in action using <a href="https://github.com/simbo1905/quarkus-graphql-bigquery" rel="noreferrer">my demo project</a>:</p>
<p><a href="https://i.stack.imgur.com/XcVGK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/XcVGK.png" alt="graphql pseudo join" /></a></p>
<p>The result nests the Author details:</p>
<pre class="lang-json prettyprint-override"><code>{
  &quot;data&quot;: {
    &quot;book1&quot;: {
      &quot;id&quot;: &quot;book-1&quot;,
      &quot;title&quot;: &quot;Harry Potter and the Philosopher's Stone&quot;,
      &quot;pageCount&quot;: 223,
      &quot;author&quot;: {
        &quot;firstName&quot;: &quot;Joanne&quot;,
        &quot;lastName&quot;: &quot;Rowling&quot;
      }
    }
  }
}
</code></pre>
<p>The single GQL query resulted in two separate fetch-by-id calls into the database. When a single logical query turns into multiple physical queries we can quickly run into the infamous <code>N+1</code> problem.</p>
<h2>The <code>N+1</code> Problem</h2>
<p>In our case above a book can only have one author. If we only query one book by ID we only get a &quot;read amplification&quot; against our database of 2x. Imaging if you can query books with a title that starts with a prefix:</p>
<pre><code>type Query {
    booksByTitleStartsWith(titlePrefix: String): [Book]
}
</code></pre>
<p>Then we call it asking it to fetch the books with a title starting with &quot;Harry&quot;:</p>
<pre><code>{
  booksByTitleStartsWith(titlePrefix:&quot;Harry&quot;){
    id
    title
    pageCount
    author {
      firstName
      lastName
    }
  }
}
</code></pre>
<p>In this GQL query we will fetch the books by a database query of <code>title like 'Harry%'</code> to get many books including the <code>authorId</code> of each book. It will then make an individual fetch by <code>ID</code> for every author of every book. This is a total of <code>N+1</code> queries where the <code>1</code> query pulls back <code>N</code> records and we then make <code>N</code> separate fetches to build up the full picture.</p>
<p>The easy fix for that example is to not expose a field <code>author</code> on <code>Book</code> and force the person using your API to fetch all the authors in a separate query <code>authorsByIds</code> so we give them two queries:</p>
<pre><code>type Query {
    booksByTitleStartsWith(titlePrefix: String): [Book]  /* &lt;- single database call */
    authorsByIds(authorIds: [ID]) [Author] /* &lt;- single database call */
}

type Book {
    id: ID
    title: String
    pageCount: Int
}

type Author {
    id: ID
    firstName: String
    lastName: String
}
</code></pre>
<p>The key thing to note about that last example is that there is no way in that model to walk from one entity type to another. If the person using your API wants to load the books authors the same time they simple call both queries in single post:</p>
<pre><code>query {
  booksByTitleStartsWith(titlePrefix: &quot;Harry&quot;) {
    id
    title
  }
  authorsByIds(authorIds: [&quot;author-1&quot;,&quot;author-2&quot;,&quot;author-3&quot;) {
    id
    firstName
    lastName
  }
}
</code></pre>
<p>Here the person writing the query (perhaps using JavaScript in a web browser) sends a single GraphQL post to the server asking for both <code>booksByTitleStartsWith</code> and <code>authorsByIds</code> to be passed back at once. The server can now make two efficient database calls.</p>
<p>This approach shows that there is &quot;no magic bullet&quot; for how to map the &quot;logical model&quot; to the &quot;physical model&quot; when it comes to performance. This is known as the <a href="https://en.wikipedia.org/wiki/Object%E2%80%93relational_impedance_mismatch" rel="noreferrer">Object–relational impedance mismatch</a> problem. More on that below.</p>
<h2>Is Fetch-By-ID So Bad?</h2>
<p>Note that the default behaviour of GraphQL is still very helpful. You can map GraphQL onto anything. You can map it onto internal REST APIs. You can map some types into a relational database and other types into a NoSQL database. These can be in the same schema and the same GraphQL end-point. There is no reason why you cannot have <code>Author</code> stored in Postgres and <code>Book</code> stored in MongoDB. This is because GraphQL doesn't by default &quot;join in the datastore&quot; it will fetch each type independently and build the response in memory to send back to the client. It <em><strong>may</strong></em> be the case that you can use a model that only joins to a small dataset that gets very good cache hits. You can then add caching into your system and not have a problem and benefit from all the <a href="https://arxiv.org/pdf/2003.04761.pdf" rel="noreferrer">advantages of GraphQL</a>.</p>
<h2>What About ORM?</h2>
<p>There is a project called <a href="https://github.com/join-monster/join-monster" rel="noreferrer">Join Monster</a> which does look at your database schema, looks at the runtime GraphQL query, and tries to generate efficient database joins on-the-fly. That is a form of <a href="https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping" rel="noreferrer">Object Relational Mapping</a> which sometimes gets a lot of &quot;<a href="https://martinfowler.com/bliki/OrmHate.html" rel="noreferrer">OrmHate</a>&quot;. This is mainly due to <a href="https://en.wikipedia.org/wiki/Object%E2%80%93relational_impedance_mismatch" rel="noreferrer">Object–relational impedance mismatch</a> problem.</p>
<p>In my experience, any ORM works if you write the database model to exactly support your object API. In my experience, any ORM tends to fail when you have an existing database model that you try to map with an ORM framework.</p>
<p>IMHO, if the data model is optimised without thinking about ORM or queries then avoid ORM. For example, if the data model is optimised to conserve space in classical <a href="https://en.wikipedia.org/wiki/Third_normal_form" rel="noreferrer">third normal form</a>. My recommendation there is to avoid querying the main data model and use the <a href="https://docs.microsoft.com/en-us/azure/architecture/patterns/cqrs" rel="noreferrer">CQRS pattern</a>. See below for an example.</p>
<h2>What Is Practical?</h2>
<p>If you do want to use pseudo-joins in GraphQL but you hit an <code>N+1</code> problem you can write code to map specific &quot;field fetches&quot; onto hand-written database queries. Carefully performance test using realist data whenever any fields return an array.</p>
<p>Even when you can put in hand written queries you may hit scenarios where those joins don't run fast enough. In which case consider the <a href="https://docs.microsoft.com/en-us/azure/architecture/patterns/cqrs" rel="noreferrer">CQRS pattern</a> and denormalise some of the data model to allow for fast lookups.</p>
<h2>Update: GraphQL Java &quot;Look-Ahead&quot;</h2>
<p>In our case we use <a href="https://github.com/graphql-java/graphql-java" rel="noreferrer">graphql-java</a> and use pure configuration files to map DataFetchers to database queries. There is a some generic logic that looks at the graph query being run and calls parameterized sql queries that are in a custom configuration file. We saw this article <a href="https://www.graphql-java.com/blog/deep-dive-data-fetcher-results/" rel="noreferrer">Building efficient data fetchers by looking ahead</a> which explains that you can inspect at runtime the what the person who wrote the query selected to be returned. We can use that to &quot;look-ahead&quot; at what other entities we would be asked to fetch to satisfy the entire query. At which point we can join the data in the database and pull it all back efficiently in the a single database call. The graphql-java engine will still make <code>N</code> in-memory fetches to our code. The <code>N</code> requests to get the author of each book are satisfied by simply lookups in a hashmap that we loaded out of the single database call that joined the author table to the books table returning <code>N</code> complete rows efficiently.</p>
<p>Our approach might sound a little like ORM yet we did not make any attempt to make it intelligent. The developer creating the API and our custom configuration files has to decide which graphql queries will be mapped to what database queries. Our generic logic just &quot;looks-ahead&quot; at what the runtime graphql query actually selects in total to understand all the database columns that it needs to load out of each row returned by the SQL to build the hashmap. Our approach can only handle parent-child-grandchild style trees of data. Yet this is a very common use case for us. The developer making the API still needs to keep a careful eye on performance. They need to adapt both the API and the custom mapping files to avoid poor performance.</p>
How to do a simple join in GraphQL?
<p>I am very new in GraphQL and trying to do a simple join query. My sample tables look like below:</p>
<pre><code>{
  phones: [
    {
      id: 1,
      brand: 'b1',
      model: 'Galaxy S9 Plus',
      price: 1000,
    },
    {
      id: 2,
      brand: 'b2',
      model: 'OnePlus 6',
      price: 900,
    },
  ],
  brands: [
    {
      id: 'b1',
      name: 'Samsung'
    },
    {
      id: 'b2',
      name: 'OnePlus'
    }
  ]
}
</code></pre>
<p>I would like to have a query to return a <em>phone</em> object with its brand name in it instead of the brand code.</p>
<p>E.g. If queried for the phone with <code>id = 2</code>, it should return:</p>
<pre><code>{id: 2, brand: 'OnePlus', model: 'OnePlus 6', price: 900}
</code></pre>