Relationships
GraphQLize identifies the relationships between database tables using their foreign keys, and generate appropriate relationship fields (connections) in the resulting GraphQL schema.
One to One
For the database table relationships like above, GraphQLize infers two one-to-one
relationships.
-
An
address
is associated with acity
viacity_id
column in theaddress
table. -
A
city
is associated with acountry
viacountry_id
column in thecity
table.
GraphQLize then create two object relationships in the Address
and the City
type respectively.
The generated field name is the camelCase version of the corresponding column name with the id suffix (_id
) removed.
Here are some of the examples.
Column Name | GraphQL Field Name |
---|---|
actor_id | actor |
original_language_id | originalLanguage |
reports_to_employee_id | reportsToEmployee |
By default, GraphQLize assumes _id
as the suffix for foreign keys in both Postgres and MySQL.
, GraphQLize will provide a configuration to override this default behavior.
One to Many
For the database table relationships like above, GraphQLize infers two one-to-many
relationships.
-
A
city
has manyaddress
(addresses) viacity_id
column in theaddress
table. -
A
country
has manycity
(cities) viacountry_id
column in thecity
table.
GraphQLize then create two array relationships in the City
and the Country
type respectively.
The generated field name is the camelCase version of the corresponding target table's pluralized name.
If a table has multiple foreign keys to the same table as below,
- Postgres
- MySQL
GraphQLize creates two one-to-many
relationships between language
and films
.
- The
language
has manyfilm
(films) vialanguage_id
column in thefilm
table. - The
language
has manyfilm
(films) viaoriginal_language_id
column in thefilm
table.
The nomenclature used here to generate the field name follows the below logic.
If the column name (language_id
) after the removal of the foreign key suffix (language
) matches the source table name, then the resulting field name is the camelCase version of the pluralized form of the table (films
).
If the column name (original_language_id
) after the removal of the foreign key suffix (original_language
) did not match the source table name, then GraphQLize removes the foreign key suffix and concatenate with the pluralized form of the target table and then convert it to its camelCase version (originalLanguageFilms
).
Many to Many
The many-to-many
relationship is a bit tricky to figure out.
GraphQLize traverses each table's metadata to figure out whether it is an associative table or not.
A table is considered as an associative table if it satisfies the following two criteria
- It should have a primary key made of two columns.
- These primary key columns should be a subset of the foreign key columns present in that table.
For the database table relationships like below,
The film_actor
table is an associative table as
- It has
actor_id
andfilm_id
as the primary keys. - The primary keys
{actor_id, film_id}
is a subset of foreign keys{actor_id, film_id}
of thefilm_actor
table.
In this scenario, GraphQLize creates two array fields for these two many-to-many
relationships.
For the relationships between film
& film_actor
and film_actor
& actor
tables, GraphQLize creates the following fields.
Foreign Key Without the Id Suffix
If the foreign key in question doesn't have the id suffix _id
, then the GraphQLize follows a slightly different approach to name the fields.
Say, we have a below schema
- Postgres
- MySQL
The continent_identifier
column doesn't have the foreign key suffix (_id
).
For the one-to-one
relationship, GraphQLize creates a field with the name continentByContinentIdentifer
. The convention is {targetTableName}By{FKeyColumnNameInPascalCase}
.
On the one-to-many
side, the field name is the concatenation of the camelCase version of the foreign key column name (continentIdentifer
) with the pluralized form of the target table (Countries
).