GraphQL Query to Get Data Two Different Tables

104 Views Asked by At

I have two tables a and b. a have id and age fields. b have id, pid, and name fields. b.pid has a foreign key relationship with a.id. How can I get age and name from these tables using a GraphQL query? Tables are created using SQL.

1

There are 1 best solutions below

0
ThisaruG On

This seems to be an SQL question rather than a GraphQL question. If you need to get the name and age, your GraphQL query should look something like this:

query MyQuery {
    profile {
        name
        age
    }
}

But the real issue is how you design and implement your GraphQL API so this query works as expected. In that case, you may use a JOIN statement to retrieve the data within your profile resolver.

SELECT a.age, b.name
FROM a INNER JOIN b
ON a.id = b.pid