Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a simple join in GraphQL?

I am very new in GraphQL and trying to do a simple join query. My sample tables look like below:

{
  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'
    }
  ]
}

I would like to have a query to return a phone object with its brand name in it instead of the brand code.

E.g. If queried for the phone with id = 2, it should return:

{id: 2, brand: 'OnePlus', model: 'OnePlus 6', price: 900}
like image 711
ambar Avatar asked Aug 12 '18 05:08

ambar


People also ask

Can I do joins in GraphQL?

GraphQL Joins allow you to create a unified GraphQL API by joining the data from different GraphQL sources. With GraphQL Joins, you can federate your queries and mutations across multiple GraphQL sources as if they were a single GraphQL schema. You do not have to write extra code or change the underlying APIs.

How do you join a table in GraphQL?

The data coming in from a GraphQL API can be joined with data already existing in a database. This is possible by creating a relationship from a field in the custom GraphQL server to a field in the database table.

What are the 2 most common actions in GraphQL?

From the point of view of the client, the most common GraphQL operations are likely to be queries and mutations.

How do you create a nested query in GraphQL?

You can use the object (one-to-one) or array (one-to-many) relationships defined in your schema to make a nested query i.e. fetch data for a type along with data from a nested or related type. The name of the nested object is the same as the name of the object/array relationship configured in the console.


1 Answers

TL;DR

Yes, GraphQL does support a sort of pseudo-join. You can see the books and authors example below running in my demo project.

Example

Consider a simple database design for storing info about books:

create table Book ( id string, name string, pageCount string, authorId string ); 
create table Author ( id string, firstName string, lastName string );  

Because we know that Author can write many Books that database model puts them in separate tables. Here is the GraphQL schema:

type Query {
    bookById(id: ID): Book
}

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

type Author {
    id: ID
    firstName: String
    lastName: String
}

Notice there is no authorId on the Book type but a type Author. The database authorId column on the book table is not exposed to the outside world. It is an internal detail.

We can pull back a book and it's author using this GraphQL query:

{
  bookById(id:"book-1"){
    id
    title
    pageCount
    author {
      firstName
      lastName
    }
  }
}

Here is a screenshot of it in action using my demo project:

graphql pseudo join

The result nests the Author details:

{
  "data": {
    "book1": {
      "id": "book-1",
      "title": "Harry Potter and the Philosopher's Stone",
      "pageCount": 223,
      "author": {
        "firstName": "Joanne",
        "lastName": "Rowling"
      }
    }
  }
}

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 N+1 problem.

The N+1 Problem

In our case above a book can only have one author. If we only query one book by ID we only get a "read amplification" against our database of 2x. Imaging if you can query books with a title that starts with a prefix:

type Query {
    booksByTitleStartsWith(titlePrefix: String): [Book]
}

Then we call it asking it to fetch the books with a title starting with "Harry":

{
  booksByTitleStartsWith(titlePrefix:"Harry"){
    id
    title
    pageCount
    author {
      firstName
      lastName
    }
  }
}

In this GQL query we will fetch the books by a database query of title like 'Harry%' to get many books including the authorId of each book. It will then make an individual fetch by ID for every author of every book. This is a total of N+1 queries where the 1 query pulls back N records and we then make N separate fetches to build up the full picture.

The easy fix for that example is to not expose a field author on Book and force the person using your API to fetch all the authors in a separate query authorsByIds so we give them two queries:

type Query {
    booksByTitleStartsWith(titlePrefix: String): [Book]  /* <- single database call */
    authorsByIds(authorIds: [ID]) [Author] /* <- single database call */
}

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

type Author {
    id: ID
    firstName: String
    lastName: String
}

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:

query {
  booksByTitleStartsWith(titlePrefix: "Harry") {
    id
    title
  }
  authorsByIds(authorIds: ["author-1","author-2","author-3") {
    id
    firstName
    lastName
  }
}

Here the person writing the query (perhaps using JavaScript in a web browser) sends a single GraphQL post to the server asking for both booksByTitleStartsWith and authorsByIds to be passed back at once. The server can now make two efficient database calls.

This approach shows that there is "no magic bullet" for how to map the "logical model" to the "physical model" when it comes to performance. This is known as the Object–relational impedance mismatch problem. More on that below.

Is Fetch-By-ID So Bad?

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 Author stored in Postgres and Book stored in MongoDB. This is because GraphQL doesn't by default "join in the datastore" it will fetch each type independently and build the response in memory to send back to the client. It may 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 advantages of GraphQL.

What About ORM?

There is a project called Join Monster 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 Object Relational Mapping which sometimes gets a lot of "OrmHate". This is mainly due to Object–relational impedance mismatch problem.

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.

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 third normal form. My recommendation there is to avoid querying the main data model and use the CQRS pattern. See below for an example.

What Is Practical?

If you do want to use pseudo-joins in GraphQL but you hit an N+1 problem you can write code to map specific "field fetches" onto hand-written database queries. Carefully performance test using realist data whenever any fields return an array.

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 CQRS pattern and denormalise some of the data model to allow for fast lookups.

Update: GraphQL Java "Look-Ahead"

In our case we use graphql-java 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 Building efficient data fetchers by looking ahead 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 "look-ahead" 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 N in-memory fetches to our code. The N 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 N complete rows efficiently.

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 "looks-ahead" 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.

like image 97
simbo1905 Avatar answered Oct 02 '22 11:10

simbo1905